2

我有一个存储过程,我在其中选择值并希望插入到表中

SELECT 
    @uid = arf.uid, @report_format = arf.report_format,
    @report_serno = arf.report_serno
FROM 
    dbo.alert_report_format AS arf 
INNER JOIN
    dbo.users AS u ON arf.uid = u.serno 
INNER JOIN
    dbo.alert_reports AS ar ON arf.report_serno = ar.serno

插入表格:

INSERT INTO reports 
VALUES (NULL, @uid, @subject, @body, @report_format, 100, @report_serno, 0, NULL, 10, GETDATE()) 

在选择中,我有 5 个值,但是当我插入另一个表时,仅插入最后一个选定的值,我想插入表中我在选择中拥有的所有值,如何在没有游标的情况下执行此操作?

4

2 回答 2

4

你需要使用类似的东西

INSERT INTO MyTable(Column1, Column2,.... ColumnN)
SELECT C1,C2,... CN
FRom MyTable

看看从其他表中插入数据

也看看INSERT (Transact-SQL)

于 2013-08-16T12:43:04.630 回答
4

试试这个方法:

INSERT INTO reports 
SELECT null,arf.uid, @subject, @body, arf.report_format,  100,arf.report_serno,0,NULL,10,GETDATE()
FROM dbo.alert_report_format AS arf 
INNER JOIN dbo.users AS u ON arf.uid = u.serno 
INNER JOIN dbo.alert_reports AS ar ON arf.report_serno = ar.serno
于 2013-08-16T12:43:14.867 回答