Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

@str help

Status
Not open for further replies.

mossbs

Programmer
Aug 19, 2008
102
0
0
GB
hey all,

first off i'm not the best at sql so bear with me.....
doing a query that has mutiple where clauses in it (using 'and' obviously) however after data has met this long list of where's i need to apply a sort of 'or' clause but all the previous where clauses still need to be adhered to - will it work with just the 'or'?

in the past i used to do something along the lines of
declare @str varchar(1000)

then set @str to be the long list of clauses etc then add my new clauses to end of the @str

however its been a while and i can't remember the syntax for it. or am i way off here?

any help greatly appreciated!
 
Can you provide a small example of the SQL you _think_ you ought to write? Can't quite understand the problem.

Jeff Prenevost
BI Consultant
Ann Arbor, MI
 
Hi Jeff,

i think its something like....

declare @test varchar(1000)
declare @str varchar(1000)

set @str = 'syntax etc where clause and this and this etc'
set @test = @str

then later i believe i can do....

where @str + ' more syntax/ more clauses'


pologies if i'm confusing the hell outta you!! - not the most techie speaker!!

cheers.
 
If you want the previous criteria to be considered then an 'or' I would suggest using parenthesis as in
SET @str = '(' + @str + ') OR new criteria'

I hope this helps.
djj
 
If you have one single OR if that or is satisfied no matter what other part of WHERE has the record will be included in result set.
Code:
SELECT * 
       FROM SomeTable
WHERE (ID = 1 AND OtherField = 1234) OR
       Filed3 = 99999
Will return ALL records where (ID = 1 AND OtherField = 1234) no matter what Filed3 is and ALL records that have Filed3 = 99999 no matter what vaules you have in ID and OtherField.

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
cheers for the replies everyone...

got it sorted in the end - rather than use @str etc i just ran first load of clauses and stuck results into temp table then ran each set of new queries from the #temp table.

cheers though all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top