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.
| Author |
Topic |
|
carnahant
Starting Member
1 Post |
Posted - 2008-03-26 : 19:35:44
|
| I am a newbie and I was trying to use a local variable to insert a "where clause" into my T-SQL code, but it won't work. Can anyone give me an idea what I am doing wrong. Here is the code:DECLARE @WhereClause varchar(500)SET @WhereClause = ' ([NAME] like ' + CHAR(39) + 'AAA%' + CHAR(39) + ') OR ' + CHAR(13) + ' ([NAME] like ' + CHAR(39) + 'AANEN%' + CHAR(39) + ') OR ' + CHAR(13) + ' ([NAME] like ' + CHAR(39) + 'ABDEL%' + CHAR(39) + ') OR ' + CHAR(13) + ' ([NAME] like ' + CHAR(39) + 'ABENDR%' + CHAR(39) + ') 'SELECT *FROM <my table with the field [NAME] in it> WHERE @WhereClause The @WhereClause evaluates to: ([NAME] like 'AAA%') OR ([NAME] like 'AANEN%') OR ([NAME] like 'ABDEL%') OR ([NAME] like 'ABENDR%') |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-03-26 : 21:51:05
|
| simpler would beDECLARE @WhereClause varchar(500)SET @WhereClause = ' ([NAME] like ''AAA%'') OR ([NAME] like ''AANEN%'') OR ([NAME] like ''ABDEL%'') OR ([NAME] like ''ABENDR%'') 'your select should beexec ('SELECT * FROM <my table with the field [NAME] in it> WHERE ' + @WhereClause)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-04-02 : 06:05:01
|
| Also refer www.sommarskog.se/dyn-search.htmlMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|