INSERT INTO SELECT查询包含GETDATE()函数

时间:2015-11-04 02:26:18

标签: sql sql-server tsql

我有INSERT INTO SELECT statement(下面)工作正常,但我无法弄清楚如何在名为rundate的tbl中填充日期时间字段。

换句话说,我想:INSERT INTO tbl (title,invnum,rundate)其中rundate将获得GETDATE()函数返回的值。

INSERT INTO tbl (title,invnum) 
SELECT @title,t1.invnum FROM #InvenNums t1 WHERE NOT EXISTS (
SELECT title,invnum FROM tbl t2 
WHERE t2.invnum=t1.invnum AND t2.title=@title)

1 个答案:

答案 0 :(得分:3)

这是你想要的吗?

INSERT INTO tbl (title, invnum, rundate)
    SELECT @title, t1.invnum, GETDATE()
    FROM #InvenNums t1
    WHERE NOT EXISTS (SELECT 1 FROM tbl t2 WHERE t2.invnum = t1.invnum AND t2.title = @title);