Search code examples
sqlselectinsertsqlparameter

SQL Insert with Select and Parameter


Hello I have a problem

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


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

How can I solve this problem

Insert
Table1.Column1=Table2.Column1
Table1.Column2=Table2.Column2
Table1.Column3=@target_date


Solution

  • declare @target_date datetime
    set @target_date=GETDATE();
    
    
    insert into table1 ([column1],[column2],[column3])
    select [column1],[column2], @target_date from table2 where id=@id
    

    Just make the variable the value of a calculated column