SQL Server 2016-使用存储过程从表中获取空值
我试图使用一个名为SQL Server 2016-使用存储过程从表中获取空值,sql,sql-server,database,stored-procedures,refactoring-databases,Sql,Sql Server,Database,Stored Procedures,Refactoring Databases,我试图使用一个名为getcompanys的存储过程和一个名为@ShowCompaniesWithoutClients的参数来显示Client列为NULL或空(')的所有公司(来自名为Company) 目前,我找到的解决方案是做一个IF/ELSE语句,其中validateIF@ShowCompaniesWithoutClients=1I命令 SELECT * FROM [Company] WHERE [Client] IS NULL OR [Client] = '' 否则,选择所有列(无限
getcompanys
的存储过程和一个名为@ShowCompaniesWithoutClients
的参数来显示Client
列为NULL
或空('
)的所有公司(来自名为Company
)
目前,我找到的解决方案是做一个IF/ELSE
语句,其中validateIF@ShowCompaniesWithoutClients=1
I命令
SELECT *
FROM [Company]
WHERE [Client] IS NULL OR [Client] = ''
否则,选择所有列(无限制)
有谁能帮我重构这个解决方案,用一个我不需要两次选择语句的解决方案替换它
免责声明:这只是一个实际应用程序的示例,我有大约20列和更多参数。因此@showCompanywithoutclients as bit
可以有3个可能的值,0、1和null。
0和1是明确的,但当您传递@ShowCompaniesWithoutClients=null时,意味着应该返回所有内容,以下是您可以执行的操作:
SELECT *
FROM [Company]
WHERE (ISNULL([Client],'') = '' and @ShowCompaniesWithoutClients = 1)
OR (ISNULL([Client],'') <> '' and @ShowCompaniesWithoutClients = 0)
OR (@ShowCompaniesWithoutClients IS NULL)
选择*
来自[公司]
其中(ISNULL([Client],“”)=''和@ShowCompaniesWithoutClients=1)
或(ISNULL([Client],“”)和@ShowCompaniesWithoutClients=0)
或(@ShowCompaniesWithoutClients为空)
非常感谢,这正是我需要的!