[英]SQL Server 2016 - Get NULL values from a table using a Stored Procedure
I am trying to use a stored procedure called GetCompanies
and a parameter called @ShowCompaniesWithoutClients
in order to show all companies (from a table called Company
) where the Client
column is NULL
or empty ( ''
).我正在尝试使用一个名为@ShowCompaniesWithoutClients
GetCompanies
参数来显示所有公司(来自名为Company
的表),其中Client
列是NULL
或空( ''
)。
At the moment the solution I found is make an IF/ELSE
statement where validate IF @ShowCompaniesWithoutClients=1
I order to目前我找到的解决方案是制作一个IF/ELSE
语句,其中验证IF @ShowCompaniesWithoutClients=1
我命令
SELECT *
FROM [Company]
WHERE [Client] IS NULL OR [Client] = ''
Otherwise, select all columns (without restrictions).否则,select 所有列(无限制)。
Can anyone help me refactoring this solution replacing it with a solution where I don't need to SELECT
statement twice?谁能帮我重构这个解决方案,用我不需要SELECT
语句两次的解决方案替换它?
Disclaimer: this is just an example of real application, which I have around 20 columns and many more parameters.免责声明:这只是一个实际应用的例子,我有大约 20 列和更多参数。
so @ShowCompaniesWithoutClients as bit
can have 3 possible values, 0, 1 and null.所以@ShowCompaniesWithoutClients as bit
可以有 3 个可能的值,0、1 和 null。 0 and 1 is clear but when you pass @ShowCompaniesWithoutClients = null means everything should be returned, here is how you can do it: 0 和 1 很清楚,但是当您通过 @ShowCompaniesWithoutClients = null 时,意味着所有内容都应该返回,您可以这样做:
SELECT *
FROM [Company]
WHERE (ISNULL([Client],'') = '' and @ShowCompaniesWithoutClients = 1)
OR (ISNULL([Client],'') <> '' and @ShowCompaniesWithoutClients = 0)
OR (@ShowCompaniesWithoutClients IS NULL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.