我有一个存储过程参数,我想使其可选并从表中指定默认值,我该怎么做?
ALTER PROCEDURE [DBO].[Test]
@planId int = 1 ( This I can do )
但我想做
@planId int = ( Select Id from Table1 Where name = ‘ABC’) ‘ Assign default id from a table
答案 0 :(得分:3)
如果你想这样做,最初只需要分配0,并在程序的第一行中将此值写入该变量。
ALTER PROCEDURE [DBO].[Test]
@planId int = 0
AS
BEGIN
SET @planId = ( Select Id from Table1 Where name = ‘ABC’)
...
...
...
END
答案 1 :(得分:1)
在存储过程中,您可以声明并设置值。在这种情况下,您可以将@planId设置为您想要的任何内容:
CREATE PROCEDURE procedureName
@planId int = null output
AS
IF @planId = null
Begin
SET @planId = ( Select Id from Table1 Where name = ‘ABC’)
End
ELSE --> then they gave you the value