检查存储过程中参数是否为空或为空 [英] Check if a parameter is null or empty in a stored procedure

查看:405
本文介绍了检查存储过程中参数是否为空或为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何检查参数是否为空,但是我不确定如何检查其是否为空...我有这些参数,我想检查先前的参数为空还是为空,然后像下面那样设置它们

I know how to check if a parameter is null but i am not sure how to check if its empty ... I have these parameters and I want to check the previous parameters are empty or null and then set them like below

ALTER PROCEDURE [dbo].[GetSummary]
    @PreviousStartDate NVARCHAR(50) ,
    @PreviousEndDate NVARCHAR(50) ,
    @CurrentStartDate NVARCHAR(50) ,
    @CurrentEndDate NVARCHAR(50)
AS
  BEGIN
    IF(@PreviousStartDate IS NULL OR EMPTY)
        SET @PreviousStartdate = '01/01/2010'  for example..

我将很感谢您的帮助.

推荐答案

我有时会使用 NULLIF 就像这样...

I sometimes use NULLIF like so...

IF NULLIF(@PreviousStartDate, '') IS NULL

可能没有理由比@Oded和@bluefeet所建议的方式更好,只是风格上的偏爱.

There's probably no reason it's better than the way suggested by @Oded and @bluefeet, just stylistic preference.

@danihp的方法确实很酷,但是当我以为空或空的时候,我疲倦的老大脑就不会灰飞烟灭了:-)

@danihp's method is really cool but my tired old brain wouldn't go to COALESCE when I'm thinking is null or empty :-)

这篇关于检查存储过程中参数是否为空或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆