双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

请各位大大进来看下这两个存储过程,有何改正之处??

BTChinaVeryCD BTChinaVeryCD 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
各位大大分析一下下面两个过程,如果把表名当参数,在第一个里面拼凑SQL语句报错,

if exists( select * from sysobjects where name ='test')
drop procedure test
go
create procedure test

@RecordCount int output

as
select @RecordCount=COUNT(*) from UserTable
go
//下面是调用语句,可以正确得到想要的数量值
declare @RowCount int
exec test

@RecordCount = @RowCount OUTPUT

select @RowCount as RecordCount

二

if exists( select * from sysobjects where name ='test1')
drop procedure test1
go
create procedure test1
@tableName varchar(20),
@RecordCount int output

as
declare @Sqls nvarchar(2000)
set @Sqls='select '+LTRIM(@RecordCount)+' from '+LTRIM(@tableName)
exec(@Sqls)

go
//以下是调用方法,却得不到想要的值,返回的是NULL值
declare @RowCount int
exec test1
N'UserTable',
@RecordCount = @RowCount OUTPUT

select @RowCount as RecordCount


...全文
评论 14
收藏 已收藏
分享
返回顶部
BTChinaVeryCD
疑难问题 22,298 成员 121,727 内容