sql - what's wrong in my query? -
@tablen1 varchar(32), @tablen2 varchar(32) declare @sqlcommand varchar(1000) declare @table1 varchar(max) declare @table2 varchar(max) declare @table3 varchar(max) set @table1 = @tablen1 set @table2 = @tablen2 set @table3 = 'ab_dispensing' set @sqlcommand = 'select ' +@table1+'.atm_id,'+@table1+'.le,'+@table2+'.le,'+@table3+'.dispensed'' '+@table1+','+@table2+','+@table3+' '+@table1+'.atm_id = '+@table2+'.atm_id''''and''' +@table3+'.atm_id = '+@table1+'.atm_id' exec (@sqlcommand)
when run query :
exec ab_dif _3_0_pm_14_may,_3_2_pm_14_may
i message
incorrect syntax near 'ab_dispensing'.
it's handy print dynamic sql , check if valid.
if pass 't1'
, 't2'
input code , print @sqlcommand
get:
select t1.atm_id,t1.le,t2.le,ab_dispensing.dispensed' t1,t2,ab_dispensing t1.atm_id = t2.atm_id''and'ab_dispensing.atm_id = t1.atm_id
notice '
causing sql invalid. need remove unnecessary '
used in .dispensed''
, ''''and'''
this sql should be.
declare @tablen1 varchar(32) = 't1', @tablen2 varchar(32) = 't2' declare @sqlcommand varchar(1000) declare @table1 varchar(max) declare @table2 varchar(max) declare @table3 varchar(max) set @table1 = @tablen1 set @table2 = @tablen2 set @table3 = 'ab_dispensing' set @sqlcommand = 'select ' +@table1+'.atm_id,'+@table1+'.le,'+@table2+'.le,'+@table3+'.dispensed '+@table1+','+@table2+','+@table3+' '+@table1+'.atm_id = '+@table2+'.atm_id , ' +@table3+'.atm_id = '+@table1+'.atm_id' --print @sqlcommand exec (@sqlcommand)
Comments
Post a Comment