首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

简单动态,出错,求改正?该如何处理

2012-02-26 
简单动态,出错,求改正???declare@tablenvarchar(4000),@idint,@sqlnvarchar(4000)select@table bbb ,@id

简单动态,出错,求改正???
declare   @table   nvarchar(4000),
                @id   int,
                @sql   nvarchar(4000)
select   @table= 'bbb ',@id=50
set   @sql= 'select   top   100   *   from   '+@table+ '   where   id <   '+@id

exec(@sql)
============
消息   245,级别   16,状态   1,第   6   行
在将   nvarchar   值   'select   top   100   *   from   bbb   where   id <   '   转换成数据类型   int   时失败。

[解决办法]
declare @table nvarchar(4000),
@id int,
@sql nvarchar(4000)
select @table= 'bbb ',@id=50
set @sql= 'select top 100 * from '+@table+ ' where id < '+Cast(@id As Varchar)

exec(@sql)
[解决办法]
declare @table nvarchar(4000),
@id int,
@sql nvarchar(4000)
select @table= 'bbb ',@id=50
set @sql= 'select top 100 * from ' '+@table+ ' ' where id < '+@id

exec(@sql)

[解决办法]
@id是int型,相加时把前面字符串强制转换成int型,当然出错。

declare @table nvarchar(4000),
@id nvarchar(10),
@sql nvarchar(4000)
select @table= 'bbb ',@id= '50 '
set @sql= 'select top 100 * from '+@table+ ' where id < '+@id

热点排行