将表名传递给sql存储过程 [英] pass table name to sql store procedure

查看:93
本文介绍了将表名传递给sql存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向所有人问好
我想做以下

hello to All
I want to do the following

declare @no as varchar(50)
declare @TName as varchar(50)='tblOrder';
declare @CName as varchar(50)='OrdId';
declare @qry as varchar(500)
declare @count as int

set @qry='select '+@count+' = count(*) from '+@TName;
print (@qry)
exec(@qry)



因为出现错误:



because it is giving error:

Msg 245, Level 16, State 1, Line 7 - Conversion failed when converting the varchar value ''select '' to data type int.<br />



感谢



thanks

推荐答案

尝试一下

Try this

declare @no as varchar(50)
declare @TName as varchar(50)='tblOrder';
declare @CName as varchar(50)='OrdId';
declare @qry as nvarchar(500) -- varchar(500)
declare @count as int

set @qry='select @count = count(*) from '+@TName;
print (@qry)
--exec(@qry)
exec sp_executesql @qry, N'@count int OUTPUT', @count OUTPUT
select @count


尝试以下操作:

try this:

set @qry=''select ''+cast(@count as varchar(32))+'' = count(*) from ''+@TName;


删除您写"@count"的单引号
它应该起作用.
remove the single quotes where you had written ''@count''
it should work.


这篇关于将表名传递给sql存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆