这是与MS SQL Server有关的一般问题。
我想使用存储过程内部的变量。我声明的所有变量都允许用户输入。有没有办法创建一个不暴露给“用户”的变量?我知道我可以将变量设为可选。
谢谢
答案 0 :(得分:2)
是的,只需在过程主体中声明变量,而不是将其声明为输入参数:
CREATE PROCEDURE SomeProc
--these are input parameters which the user/caller can specify values for
@parameter1 nvarchar(50),
@parameter2 int
AS
BEGIN
DECLARE @parameter3 int; -- this is a variable which is private to the procedure.
SET @parameter3 = 7; -- this sets the value of the variable. Of course you could set its value another way by taking it from a table, or doing a calculation, whatever you need.
SELECT somefield FROM sometable WHERE anotherfield = @parameter3;
END
您可能还想review the relevant T-SQL documentation谈论这个话题