-1

我正在尝试做一个动态的简单INSERT

SELECT 'INSERT INTO Agency (Agency_Id) VALUES (' + CAST(Agency_Id AS VARCHAR(10)) + ')'   
FROM User 
WHERE User_Id = 1

但是如果我的Agency_IdIS NULL 那么这个查询将只出现NULL在结果中,而不是像INSERT INTO Agency (Agency_Id) VALUES (NULL).

我怎样才能做到这一点 ?

4

2 回答 2

1
SELECT 'INSERT INTO Agency (Agency_Id) VALUES (' + ISNULL(CAST(Agency_Id AS VARCHAR(10)), 'NULL') + ')'   
FROM User 
WHERE User_Id = 1
于 2013-07-08T13:06:26.913 回答
0

只需'NULL'用于空值:

SELECT 'INSERT INTO Agency (Agency_Id) VALUES (' 
        + ISNULL(Agency_Id,'NULL')
        + ')'   
FROM User 
WHERE User_Id = 1
于 2013-07-08T13:12:49.947 回答