我有一个主表和几个较小的表。
Master
C1 | C2 | C3 | C4 | C5 |
C1 | C2 | C3 |
另外@C1(具有与Master表中C1的值匹配的值的变量。
@C1
列名与两个表都匹配。我想创建一个存储过程从插入值Master表(C1,C2,和C3更小的表)( C1, C2, C3)。
C1
C2
C3
C1, C2, C3
我的努力:
Create proc Schema.Proc (@C1 int) AS BEGIN INSERT INTO SmallTable (C1, C2, C3) --- Columns of smaller table Values (SELECT C1, C2, C3 ---Columns of Master table FROM MasterTable) WHERE C1 = @C1 --- Where value of C1 of Master table matches the value of @C1 END
请帮忙
谢谢
您需要使用INSERT INTO ... SELECT .....语法-不VALUES涉及任何关键字:
INSERT INTO ... SELECT .....
VALUES
CREATE PROCEDURE Schema.Proc (@C1 int) AS BEGIN INSERT INTO SmallTable(C1, C2, C3) --- Columns of smaller table SELECT C1, C2, C3 ---Columns of Master table FROM MasterTable WHERE C1 = @C1 --- Where value of C1 of Master table matches the value of @C1 END