我正在尝试做一个动态的简单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)
之类的查询。
我该怎么做?
答案 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