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
 General SQL Server Forums
 New to SQL Server Programming
 how assign logic expression into a variable

Author  Topic 

korssane
Posting Yak Master

104 Posts

Posted - 2009-11-04 : 09:00:47
hi all,

i have a logic express that calls many ( and / or) operations and i donot want to repeat this over a long query.

here is my code :

declare @hspacategories varchar(max);
set @hspacategories = '(condition1 OR
condition2 OR
conditioAn3 )'

SELECT
COUNT(*) as Total
FROM table
WHERE( Date1 = @Report_Date) AND
@hspacategories
)

the error i am getting is : An expression of non-boolean type specified in a context where a condition is expected.

any help will be appreciated..

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-04 : 09:28:04
To use variable parts in an SQL statement look/search for DYNAMIC SQL.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

korssane
Posting Yak Master

104 Posts

Posted - 2009-11-04 : 09:49:52
do yiouhaveany exampld, the dynamic sql talks about introducing variable in a condition statement..
i am looking to put the condition in 1 variable then use this last one instead of writing the whole expression..

any more infos about this ?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-04 : 10:19:29
exec ('select count(*) as Total from table where date1=@Report_Date and '+@hspacategories)


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -