ms sql从变量插入值并从表中选择

时间:2013-01-29 08:37:11

标签: sql sql-server sql-insert

我想从表中插入一个变量值和一个选择结果。

declare @actual Date
set @actual = getdate()

这是我的尝试:

insert into test (ID, Date) (select ID, @actual from Table)

我收到错误消息!!

3 个答案:

答案 0 :(得分:3)

使用INSERT INTO..SELECT语句,

DECLARE @actual Date
SET @actual = GETDATE()

INSERT INTO test (ID, Date) 
SELECT ID, @actual 
FROM   Tablename

答案 1 :(得分:1)

You can try like this for using a select clause for Insert

insert into test(id,date) select col1,col2 from table;

答案 2 :(得分:0)

INSERT INTO TEST
(ID,DATE)
SELECT COLUMN1,COLUMN2 FROM TABLE