Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何修改要执行的查询?_Sql_Sql Server 2008 R2 - Fatal编程技术网

Sql 如何修改要执行的查询?

Sql 如何修改要执行的查询?,sql,sql-server-2008-r2,Sql,Sql Server 2008 R2,我有以下查询,我希望它显示“Not-Yet”,而不是显示数据库中Status列的NULL值怎么做? SELECT dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username, dbo.SafetySuggestionsStatus.Status, CASE WHEN dbo.SafetySuggestions

我有以下查询,我希望它显示“Not-Yet”,而不是显示数据库中Status列的NULL值怎么做?

SELECT     dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username, dbo.SafetySuggestionsStatus.Status,
                 CASE WHEN dbo.SafetySuggestionsStatus.Status  IS NULL THEN 'NOT YET'
FROM         dbo.SafetySuggestionsLog INNER JOIN
                      dbo.employee ON dbo.SafetySuggestionsLog.Username = dbo.employee.Username LEFT OUTER JOIN
                      dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE     (dbo.employee.Username = @Username)
供您参考,该数据库设计如下所示:

Employee Table: Username, Name
SafetySuggestionsLog Table: ID, Title, Description, Username, StatusID
SafetySuggestionsStatus: ID, Status
使用或


你说得很对,可以使用ISNULL或CASE

CASE WHEN dbo.Blah.Status IS NULL THEN 'Not Yet' ELSE dbo.Blah.Status END


您是否检查了CASE-WHEN子句的语法?@PanagiotisKanavos
COALESCE
更好:)并删除格式错误的CASE-WHEN,因为它会阻止查询编译您假设代码不包含
ELSE-Status-END
。(尽管这可能是一个正确的假设)为了防止有人想知道ISNULL和COALESCE之间的区别,COALESCE从传递给它的参数返回第一个非null值,而ISNULL返回原始值或替换值(如果需要)NULL@ypercube-不,对于2个参数,它们并不总是等效的<代码>声明@X字符(1);选择ISNULL(@X,'Foo')、COALESCE(@X,'Foo')返回
(F,Foo)
SELECT     dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username, ISNULL(dbo.SafetySuggestionsStatus.Status, "Not Yet") as 'Status'
FROM         dbo.SafetySuggestionsLog INNER JOIN
                      dbo.employee ON dbo.SafetySuggestionsLog.Username = dbo.employee.Username LEFT OUTER JOIN
                      dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE     (dbo.employee.Username = @Username)
CASE WHEN dbo.Blah.Status IS NULL THEN 'Not Yet' ELSE dbo.Blah.Status END
isnull(dbo.Blah.Status, 'Not Yet')