在将 varchar 值 'select * from soft where id=' 转换成数据类型 int 时失败

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 10:45   99   0

刚开始的存储过程:

create PROCEDURE [dbo].[mytest]
(
@myTable varchar(100),
@myInt int
)
AS
declare @strSQL varchar(1000);
set @strSQL ='select * from ' + @myTable + ' where id=' + @myInt
exec (@strSQL)
执行:exec mytest 'soft',10
显示错误:消息 245,级别 16,状态 1,过程 mytest,第 8 行
在将 varchar 值 'select * from soft where id=' 转换成数据类型 int 时失败。
搜了一下,发现字符串变量和整型变量不能用+,改成:
create PROCEDURE [dbo].[mytest]
(
@myTable varchar(100),
@myInt int
)
AS
declare @strSQL varchar(1000);
set @strSQL ='select * from ' + @myTable + ' where id=' + cast(@myInt as varchar(10))
exec (@strSQL)
通过。
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP