简体   繁体   English

exec存储过程中,如果参数为空,如何返回空结果集? (SQL Server)

[英]How to return an empty result set if parameter is left blank when exec stored procedure? (SQL Server)

I am currently working with this code: 我目前正在使用此代码:

CREATE PROCEDURE Test 
    @char VARCHAR(MAX) = ____
AS
    SELECT * 
    FROM Table1 
    WHERE column ___  =  @char + _____

And I need to make it so that if the user executes the stored procedure without a value for the parameter, it should return an empty result set. 而且我需要这样做,以便如果用户执行存储过程时没有参数值,则它应该返回一个空结果集。 And if they provide a value, then it returns the result set for that value provided. 如果它们提供了一个值,那么它将返回所提供值的结果集。

How would I go about in filling in those blanks? 我将如何填补这些空白?

I figured out how to do it but it would require me to change the code so my way would be: 我想出了办法,但是这需要我更改代码,所以我的方式是:

CREATE PROCEDURE Test 
    @char VARCHAR(MAX) = NULL
AS
    SELECT * 
    FROM Table1 
    WHERE column = ISNULL(@char, NULL)

I believe this way works, but the question wants me to do it their way, does anyone know how to do it with the way the question has it structured? 我相信这种方式有效,但是问题要我按照自己的方式去做,有人知道如何用问题的构造方式来做吗?

Thank you for any help! 感谢您的任何帮助!

---Try this it's Might work for you
-- SET @char NULL If it's '____' 

    DECLARE @char VARCHAR(MAX) = '____'

        IF @char ='____' 
        SET @char= NULL

        SELECT * 
        FROM Table1 
        WHERE column= @char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM