SQL 使用 Select 和参数插入

SQL Insert with Select and Parameter

你好我有一个问题

declare @target_date datetime
set @target_date=GETDATE();


insert into table1 ([column1],[column2],[column3])
(select [column1],[column2] from table2 where id=@id), @target_date

我该如何解决这个问题

插入
Table1.Column1=Table2.Column1
Table1.Column2=Table2.Column2
Table1.Column3=@target_date

declare @target_date datetime
set @target_date=GETDATE();


insert into table1 ([column1],[column2],[column3])
select [column1],[column2], @target_date from table2 where id=@id

只需将变量设为计算列的值

插入后 select 命令不需要 (),并且必须在 select 命令中移动 @target_date。