Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how add variable in dynamic query

Author  Topic 

Rg
Starting Member

10 Posts

Posted - 2011-12-13 : 07:06:15
i am getting error how add this in dynamic query

can you please help me

+'AND (isnull(@Lstatus,'''')='''') or A.Lstatus= @Lstatus)'
+'AND ((isnull(@Dstatus,'''')='''') or A.Dstatus= @Dstatus)'

rg

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 07:33:32
why you need extra braces? isnt below enough?


..
+'AND (isnull(@Lstatus,'''')='''' or A.Lstatus= @Lstatus)'
+'AND (isnull(@Dstatus,'''')='''' or A.Dstatus= @Dstatus)'



i've posted this based on query part posted so cant guarantee that above will work fine unless i see rest of part also

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Rg
Starting Member

10 Posts

Posted - 2011-12-13 : 08:00:10
above query is not working
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-12-13 : 08:01:00
Maybe this?
'AND (isnull('+@Lstatus+','''')='''' or A.Lstatus= @Lstatus)'
+'AND (isnull('+@Dstatus+','''')='''' or A.Dstatus= @Dstatus)'


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

Rg
Starting Member

10 Posts

Posted - 2011-12-13 : 08:10:10
not working,
i am getting error how add this in dynamic query

can you please help me

+'AND (isnull(@Lstatus,'''')='''') or A.Lstatus= @Lstatus)'
+'AND ((isnull(@Dstatus,'''')='''') or A.Dstatus= @Dstatus)'
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-12-13 : 08:46:23
Tell us what the error is that you are getting and please post your full query. Based on what you've told, mine or Visakhs could both be the right answer, or the wrong one.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 08:47:02
what about this?

'AND (isnull('+@Lstatus+','''')='''' or A.Lstatus= ' + @Lstatus + ')'
+'AND (isnull('+@Dstatus+','''')='''' or A.Dstatus= ' + @Dstatus +')'

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -