简体   繁体   English

SQL 服务器 2016 - 使用存储过程从表中获取 NULL 值

[英]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.

相关问题 表值作为SQL Server存储过程的参数 - Table values as parameters for SQL Server stored procedure SQL Server:在存储过程中使用表或@table - SQL Server: using table or @table in stored procedure 如何使用SQL Server从具有多个select语句的存储过程中获取一个数据表 - How to get one data table from Stored procedure that has multiple select statements using sql server 在SQL Server 2016中没有看到“执行存储过程” - Not seeing “Execute stored Procedure” in SQL Server 2016 如何使用ExecuteSqlCommand从SQL存储过程获取值列表 - How to get list of values from SQL stored procedure using ExecuteSqlCommand C#和SQL Server:如何通过使用C#调用存储过程来从SQL表中获取值? - C# and SQL Server: How do I get values from my SQL Table, by calling my stored procedure with C#? SQL 服务器 Null 或存储过程的参数中缺少值 - SQL Server Null or missing values in the stored procedure's parameters 更新时允许空值,使用SQL Server存储过程将记录插入数据库 - Allowing null values when updating, inserting records to database using SQL Server stored procedure 将值从一个表插入到SQL Server上存储过程中的另一个表中 - Insert values from one table into another table in a Stored Procedure on SQL Server 使用LINQ从存储过程中获取值 - Using LINQ to get values from Stored Procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM