我有一张主表和几张小表。
Master
表格有C1 | C2 | C3 | C4 | C5 |
C1 | C2 | C3 |
@C1
(一个变量,其值与Master
表中的C1值相匹配。
两个表的列名都匹配。我想创建一个存储过程,将Master
表(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
请帮忙
谢谢
答案 0 :(得分:10)
您需要使用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
答案 1 :(得分:3)
你很亲密!只要C1,C2和C3是相同的数据类型,这应该有效。
Create proc 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
END
答案 2 :(得分:0)
CREATE OR REPLACE PROCEDURE P_INSERT(U_ID NUMBER)
AS
BEGIN
INSERT INTO X(ID,NAME,SALARY)--X IS TABLE NAME
SELECT ABC_ID,NAME,SALARY FROM ABC
WHERE ABC_ID=U_ID;--ABC_ID IS SAME VALUE OF U_ID
END;