日期:2014-05-18  浏览次数:20403 次

下面的语句为什么总提示出错?
declare   @ixh   int  
declare   @nbagqty   float
declare   @susertable   varchar(2000)
declare   @username   varchar(50)
declare   @sql1   varchar(2000)
set   @sql1=   'insert   into   tbomlist_log(ixh,suserno,nBagQTY,field,username,type,update_time,confirm)   select   i.ixh,i.suserno,i.nBagQTY, '+ ' ' 'nBagQTY ' ' '+ ', '+ ' ' ' '+@username+ ' ' ' '+ ', '+ ' ' 'update ' ' '+ ',getdate(),0   from '+ ' ' ' '+   @sUserTable+ ' ' ' '+ '   I   where   i.ixh= '+ ' '+@ixh+ ' '+ '   and   convert(varchar(50),i.nBagQTY) <> '+ ' ' ' '+@nBagQTY+ ' ' ' '
select   @sql1  

提示错误:
服务器:   消息   245,级别   16,状态   1,行   6
将   varchar   值   '   and   convert(varchar(50),i.nBagQTY) <> '   转换为数据类型为   int   的列时发生语法错误。


------解决方案--------------------
--try

declare @ixh int
declare @nbagqty float
declare @susertable varchar(2000)
declare @username varchar(50)
declare @sql1 varchar(2000)

select @ixh=1, @nbagqty=100, @susertable= 'table ', @username= 'name '

set @sql1= 'insert into tbomlist_log(ixh,suserno,nBagQTY,field,username,type,update_time,confirm) select i.ixh,i.suserno,i.nBagQTY, '+ ' ' 'nBagQTY ' ' '+ ', '+ ' ' ' '+@username+ ' ' ' '+ ', '+ ' ' 'update ' ' '+ ',getdate(),0 from '+ ' ' ' '+ @sUserTable+ ' ' ' '+ ' I where i.ixh= '+ ' '+cast(@ixh as varchar)+ ' '+ ' and convert(varchar(50),i.nBagQTY) <> '+ ' ' ' '+cast(@nBagQTY as varchar)+ ' ' ' '
select @sql1
------解决方案--------------------
@ixh 是int类型
而 @nBagQTY是float类型
都需要转换
------解决方案--------------------
declare @ixh int
declare @nbagqty float


你这里定义的是浮点行的,但是你在加条件的时候用得 and convert(varchar(50),i.nBagQTY) <> '+ ' ' ' '+@nBagQTY+ ' ' ' ' 肯定报上面的错误了

--上面的能不能改成

declare @ixh int
declare @nbagqty float
declare @susertable varchar(2000)
declare @username varchar(50)
declare @sql1 varchar(2000)
set @sql1= 'insert into tbomlist_log(ixh,suserno,nBagQTY,field,username,type,update_time,confirm)
select i.ixh,i.suserno,i.nBagQTY, '+ ' ' 'nBagQTY ' ' '+ ', '+ ' ' ' '+@username+ ' ' ' '+ ', '+ ' ' 'update ' ' '+ ',getdate(),0 from '+ ' ' ' '+ @sUserTable+ ' ' ' '+ ' I where i.ixh= '+rtrim(@ixh)+ ' and cast(i.nBagQTY as varchar(50)) <> ' ' '+rtrim(@nbagqty)+ ' ' ' '
print @sql1