操作数数据类型nvarchar对减法运算符无效

时间:2014-06-25 08:50:48

标签: sql

我试图创建一个动态的sql查询。但是在动态查询中,我无法想象如何使用复杂的函数。我尝试了很多组合,但直到给出了eror.it在 包含RTIM LTRIM function.code和错误的列,如下所示。

 declare @karorani decimal          set @karorani=0
 declare @toplamkar decimal         set @toplamkar=0
 declare @toplamciro decimal        set @toplamciro=0
 declare @odemetipi nvarchar(12)    set @odemetipi='Hepsi'
 declare  @columns nvarchar(100)    set @table='new_flightreservation'
 declare @sqlcommand nvarchar(max)  set @columns='new_salesorderid'
 declare @table nvarchar(100)       

  set @sqlcommand='select ' + @columns + ' from ' + @table +' M ' + 'LEFT JOIN '+
 'new_new_paymnet_'+@table +' F '+ ' on ' + ' M.'+@table+'Id'+'=F.'+@table+'Id' +
 ' left join new_payment P '+'on '+ 'F.new_paymentid=P.new_paymentId where 
 '(LTIM(RTRIM((left (P.new_name,(CHARINDEX('-',P.new_name)-1))))=@odemetipi //error 
  or @odemetipi+=Hepsi)'

    execute (@sqlcommand)

  error:  Operand data type nvarchar is invalid for subtract operator.

1 个答案:

答案 0 :(得分:0)

此代码存在很多问题

尝试

declare @karorani decimal           set @karorani=0
declare @toplamkar decimal          set @toplamkar=0
declare @toplamciro decimal         set @toplamciro=0
declare @odemetipi nvarchar(12)     set @odemetipi='Hepsi'
declare @table sysname              set @table='new_flightreservation'  
declare @columns nvarchar(100)      set @columns='new_salesorderid'
declare @sqlcommand nvarchar(max)   

set @sqlcommand='
select ' + @columns + ' 
from ' + @table +' M ' + '
left join new_new_paymnet_' + @table +' F 
on M.' + @table + 'Id=F.' + @table + 'Id
left join new_payment P 
on F.new_paymentid=P.new_paymentId 
where (ltim(rtrim((left(P.new_name,(charindex(''-'',P.new_name)-1))))=''' + @odemetipi + '''
or ''' + @odemetipi + '''=''Hepsi'')'

print @sqlcommand
-- exec(@sqlcommand)