Skip to main content
5 of 14
deleted 60 characters in body
Ramu
  • 13
  • 2

How to mention declared integer in where condition using dynamic sql?

    I tried with 1,2 and 3 quotes but I am getting
    
    >Conversion failed when converting the varchar value 
    
    I also tried year(s_tran_d) with /without conversion. Can someone please help me?

with conversion:
    
        inner join xxx.xxx.PITM'+@Datemm_yyyy+' pitm 
            on nep.s_pitm_c=pitm.pitm_c
            and  cast(year(s_tran_d) as nchar)='''+@yyyy+''' 
            and cast(month(s_tran_d) as nchar) ='''+@mm+'''
    
        inner join xxx.xxx.PITM'+@Datemm_yyyy1+' pitm1 
            on nep.s_pitm_c=pitm1.pitm_c
            and cast(year(dateadd(m,-1,s_tran_d)) as nchar) =''+ @yyyy1 +'' 
            and cast(month(s_tran_d)-1 as nchar)= '''+@mm1 +'''

 without conversion:
    
    inner join xxx.xx.PITM'+@Datemm_yyyy+' pitm on nep.s_pitm_c=pitm.pitm_c
    and  year(s_tran_d) =''+@yyyy+'' and month(s_tran_d)  =''+@mm+''
    
    inner join xxx.xx.PITM'+@Datemm_yyyy1+' pitm1 on nep.s_pitm_c=pitm1.pitm_c
    and year(dateadd(m,-1,s_tran_d))  =''+ @yyyy1 +'' and month(s_tran_d)-1= ''+@mm1 +''
  
    I also tried by keeping 1 or 2 or 3 single quotes. 
    
    When I put 2 quotes I am getting 
    
    Conversion failed when converting the varchar value '+@yyyy+' to data type int.
Ramu
  • 13
  • 2