我的表中的NULL值

时间:2013-07-08 13:04:32

标签: sql

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

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

但是如果我的Agency_Id为空,那么此查询只会在结果中显示NULL,而不会显示INSERT INTO Agency (Agency_Id) VALUES (NULL)之类的查询。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

只需使用'NULL'作为空值:

SELECT 'INSERT INTO Agency (Agency_Id) VALUES (' 
        + ISNULL(Agency_Id,'NULL')
        + ')'   
FROM User 
WHERE User_Id = 1