您的位置:首页 > 其它

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

2009-10-05 02:27 549 查看
刚开始的存储过程:

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)

通过。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐