SQL中的链接表

时间:2013-05-02 09:20:29

标签: sql tsql

早上好我有以下查询创建了一个临时表,我想要的是这个表是永久性的,每当其他表更新时,这个表会自动更新到,我觉得有这么简单这样做的方法,但我忘记了。

查询链接3个表

Select S.[Silks_Skey], MC.[MajorColour_Skey] 
from [dbo].[Silks] S 
inner join [dbo].[SubColour] SC on CHARINDEX(SC.[SubColour],S.[SilksName]) <> 0
inner join [dbo].[MajorColour] MC on SC.[MajorColour] = MC.[MajorColour]

UNION ALL

Select S.[Silks_Skey], MC.[MajorColour_Skey] 
from [dbo].[Silks] S 
inner join [dbo].[MajorColour] MC on CHARINDEX(MC.[MajorColour],S.[SilksName]) <> 0

ORDER BY S.[Silks_Skey]

将链接表转换为永久表

Silks_Skey  MajorColour_Skey
1   7
1   8
2   2
2   8
2   9
3   4
3   5

2 个答案:

答案 0 :(得分:0)

关于使用View的问题。既然如此,您希望在更新表时更新此查询结果。将其设为视图。

答案 1 :(得分:0)

CREATE VIEW [silkColours]
AS
Select S.[Silks_Skey], MC.[MajorColour_Skey] 
from [dbo].[Silks] S 
inner join [dbo].[SubColour] SC on CHARINDEX(SC.[SubColour],S.[SilksName]) <> 0
inner join [dbo].[MajorColour] MC on SC.[MajorColour] = MC.[MajorColour]

UNION ALL

Select S.[Silks_Skey], MC.[MajorColour_Skey] 
from [dbo].[Silks] S 
inner join [dbo].[MajorColour] MC on CHARINDEX(MC.[MajorColour],S.[SilksName]) <> 0

ORDER BY S.[Silks_Skey]