SQL Server 创建带返回值的存储过程

--drop procedure zcstest;
create procedure zcstest
(
    @tableName varchar(max),
    @dataCount int output
 )
 as
 declare @paramName varchar(max)
 set @tableName = LTRIM(@tableName)
 if @tableName is null or @tableName = ''
 begin
  set @paramName = '@tableName'
  goto ArgumentNullException
 end
 declare @finalSql varchar(max)
 declare @getCountSql nvarchar(max)
 set @getCountSql = N'select @dataCount=count(*) from ' + @tableName
 exec sp_executesql @getCountSql, N'@dataCount int output', @dataCount output
 set @finalSql = 'select * from ' + @tableName
 exec(@finalSql)
 print(@finalSql)
 
 return 0
 ArgumentNullException:
    raiserror('参数%s的值不能为空', 10, 1, @tableName)
 return -60001

 declare @dataCount int
 exec [LCMDM9999].[zcstest] 'MDMWLDW',@dataCount output
 print(@dataCount)

 

posted @ 2018-05-25 10:48  zhchsh  阅读(6138)  评论(0编辑  收藏  举报