以下是程序代码:
ALTER PROCEDURE spM2016_ErrorNumberDesignator
-- Add the parameters for the stored procedure here
@procid int = 0,
@debug int = 0,
@errorNum int = 60000 OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @sql nvarchar(max);
DECLARE @params nvarchar(max);
DECLARE @error1List nvarchar(max) = '''spM2016_FullTableName'', ''spM2016_NullCheckQuoteName_SchemaDBTable_Names'''
DECLARE @procResults TABLE ([name] sysname);
--SET @errorNum = 60000; uncomment this line
SET @sql = N'SELECT name ' +
N'FROM sys.objects ' +
N'WHERE OBJECT_NAME(@proc) IS NOT NULL ' +
N'AND ' +
N'name = OBJECT_NAME(@proc) ' +
N'AND ' +
N'OBJECT_NAME(@proc) IN (@error1List); ';
SET @params = N'@proc int';
SET @sql = REPLACE(@sql, '@error1List', @error1List);
IF (@debug = 1)
PRINT REPLACE(@sql, '@proc', @procid);
ELSE
BEGIN
INSERT INTO @procResults EXECUTE sp_executesql @sql, @params, @proc = @procid;
IF (@debug = 1)
BEGIN
SELECT * FROM @procResults;
SET @errorNum = 60000;
END
ELSE
BEGIN
-- Insert statements for procedure here
IF EXISTS (SELECT * FROM @procResults)
BEGIN
SET @errorNum = 60001;
END
END
END
END
GO
有人可以向我解释为什么@berorNum参数在取消注释行SET @errorNum = 60000
时输出其默认值60000,但在注释行时不输出60000?
例如,当我使用@procid = 0
执行该过程并尝试打印@errorNum
时,我得到的似乎是空值。这与我如何撰写我的if语句有关吗?
这是我用于执行的代码:
DECLARE @RC int
DECLARE @procid int
DECLARE @debug int
DECLARE @errorNum int
-- TODO: Set parameter values here.
SET @debug = 0;
SET @procid = 0;
EXECUTE @RC = [dbo].[spM2016_ErrorNumberDesignator]
@procid
,@debug
,@errorNum OUTPUT
PRINT @errorNum;
GO