SQL Server:存储过程中的可选变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1520331/
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 03:53:05  来源:igfitidea点击:

SQL Server: Optional variable in a stored procedure

sqlsql-servertsqloptional-parameters

提问by Roch

I would like to know if there is anyway I can set one of my stored procedure parameter as optional.

我想知道是否可以将我的存储过程参数之一设置为可选。

IF @thing_id <> ''
BEGIN 
 SET @sFiltre = @sFiltre + ' AND OPERES.OPE_THING = ' +  CONVERT(VARCHAR,@thing_id)
END

回答by Raj More

When you create the stored procedure, create it like this

创建存储过程时,像这样创建

Create Proc MyProc
@Param1 VarChar (20),
@Param2 VarChar (20) = NULL
AS

-- Your code here

GO

Param1 is mandatory

参数 1 是强制性的

Param2 is Optional

参数 2 是可选的

回答by shahkalpesh

Providing a default value to the stored procedure parameter will make it optional.

为存储过程参数提供默认值将使其成为可选的。

EDIT:

编辑:

CREATE PROC [ EDURE ] [ owner. ]
procedure_name [ ; number ]
[ { @parameter data_type }
[ VARYING ] [ = default ] [ OUTPUT ]
] [ ,...n ]

default

Is a default value for the parameter. If a default is defined, the procedure can be executed without specifying a value for that parameter. The default must be a constant or it can be NULL. It can include wildcard characters (%, _, [], and [^]) if the procedure uses the parameter with the LIKE keyword.

创建过程 [ EDURE ] [ 所有者。]
程序名称 [ ; number ]
[ { @parameter data_type }
[ VARYING ] [ = default ] [ OUTPUT ]
] [ ,...n ]

默认

是参数的默认值。如果定义了默认值,则无需为该参数指定值即可执行该过程。默认值必须是常量,也可以是 NULL。如果过程使用带有 LIKE 关键字的参数,则它可以包含通配符(%、_、[] 和 [^])。

Please see - http://msdn.microsoft.com/en-us/library/aa258259%28SQL.80%29.aspx

请参阅 - http://msdn.microsoft.com/en-us/library/aa258259%28SQL.80%29.aspx

回答by Joel Coehoorn

Yes. List "optional" parameters at the end of the parameter list and give them a default value (typically NULL):

是的。在参数列表的末尾列出“可选”参数并给它们一个默认值(通常为 NULL):

CREATE PROCEDURE MyProcedure
    @param1 int,
    @param2 varchar(200),
    @thing_id int = NULL
AS

    If @thing_id IS NULL Begin
       /* ... */
    End

END

回答by blowdart

Setting aside the SQL injection joy that code will bring, yes you can. You can set a default value for parameters

抛开代码带来的 SQL 注入乐趣,是的,你可以。您可以为参数设置默认值

CREATE PROCEDURE DoStuff @param1 varchar(20) = null

Then inside the stored procedure

然后在存储过程里面

IF @param1 IS NOT NULL
BEGIN
    ... Do stuff
END

You can set the default value to be anything you like.

您可以将默认值设置为您喜欢的任何值。

回答by Rodrigo

CREATE PROCEDURE SQL_INJECTION(
@MandatoryA int,
@MandatoryB varchar(50),
@MandatoryC datetime,
@OptionalA varchar(50) = NULL
)
AS

-- PUT YOUR DYNAMIC SQL HERE

GO

To call

打电话

EXEC dbo.SQL_INJECTION @MandatoryA = 1, @MandatoryB = 'test', @MandatoryC = '2009-10-05', @OptionalA = DEFAULT

Note1: Dynamic SQL = SQL Injection

注 1:动态 SQL = SQL 注入