如何检查变量在 SQL Server 2008 存储过程中是否有值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22007641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:10:11  来源:igfitidea点击:

How to check if a variable has a value in a SQL Server 2008 stored procedure

sqlsql-server

提问by Happy

I need to check whether a variable has a value or not.

我需要检查一个变量是否有值。

declare @name varchar(20)

set @name = (SELECT Product_Name 
             FROM tb_new_product_Name_id 
             WHERE Product_Name = @productName)

 if (@name )   // here I need to check it 

How to do it? thanks

怎么做?谢谢

回答by Nagaraj S

Try this

尝试这个

if (@name is null or @value = '') //it will indicate whether it contains a value or not

回答by TomTom

IS NOT NULL

不为空

As NULL would indicate the absense of a value.

因为 NULL 表示没有值。

回答by Damien_The_Unbeliever

Just do the IFdirectly, using EXISTSinstead:

IF直接做,EXISTS改用:

IF EXISTS(SELECT * FROM tb_new_product_Name_id 
        where Product_Name=@productName)
BEGIN
    --Do something because a row existed with that name
END

We may be able to further assist with simplifying your code if you told us more on what you were planning to do having confirmed a row existed in tb_new_product_Name_id. It looks like you're writing very procedural code - first I'll do X, then I'll do Y, then I'll do Z, etc. SQL excels as a language where you tell it "what to do" - for the entire data set you want to compute - not "how to do it" in a step by step, row by row fashion.

如果您告诉我们更多关于您在确认tb_new_product_Name_id. 看起来您正在编写非常程序化的代码 - 首先我会做 X,然后我会做 Y,然后我会做 Z,等等。 SQL 擅长作为一种语言,你告诉它“做什么” - 因为您想要计算的整个数据集 - 而不是“如何做”,一步一步,一行一行的方式。

回答by Saro Ta?ciyan

You can check if null using IS NULLas follows;

您可以使用IS NULL以下方法检查是否为空;

DECLARE @name VARCHAR(20)

SET @name = (SELECT Product_Name FROM tb_new_product_Name_id where Product_Name=@productName)

IF @name IS NULL -- # Here is where you check it
BEGIN
    -- # Handle null @name
END 
ELSE
BEGIN
    -- # @name has value
END

This how i would do, but if you are declaring @namevariable only for null checking then i would go as Demien_The_Unbelieversuggests. Using EXISTSyou don't even need the @namevariable

这是我会怎么做,但如果你@name只为空检查声明变量,那么我会按照Demien_The_Unbeliever 的建议去做。使用EXISTS你甚至不需要@name变量

回答by sateesh

An uninitiated variable always has null value in it. You may use IS NOT NULL to check that..

未初始化的变量中始终具有空值。您可以使用 IS NOT NULL 来检查..

回答by santa029

Recently encountered a situation where a field with no values was being checked with is null, is not null but was not working and on debugging found if no value then the field was getting a 0. So checking for value = '' or value = 0 or value is null, one of the three would check if its empty.

最近遇到这样一种情况,一个没有值的字段被检查为空,不是空但不工作,调试时发现如果没有值则该字段为 0。因此检查 value = '' 或 value = 0或值为空,三者之一将检查其是否为空。