Friday, March 23, 2012

Problem with a where clause

I'd like to set the where clause of my query from a field in a table
ex:
Select TableX.field1
from tableX, TableY
where TableY.field2
Field2 is equal to "fieldW like '%blabla%' "
Thanks for your helpDynamic SQL:
DECLARE @.sqlStmt VARCHAR(2000)
DECLARE @.whereClause VARCHAR(500)
SELECT @.whereClause = field2
FROM tableY
WHERE {tableY.key_column} = {value}
SELECT @.sqlStmt = 'SELECT tableX.field1 FROM tableX, tableY WHERE '
SELECT @.sqlStmt = @.sqlStmt + @.whereClause
EXEC (@.sqlStmt)
Be careful about SQL Injection using this method.
"Epervier" <Epervier@.discussions.microsoft.com> wrote in message
news:D8F4A0A0-37CE-4FA5-B9CC-771BCE61B5FF@.microsoft.com...
> I'd like to set the where clause of my query from a field in a table
> ex:
> Select TableX.field1
> from tableX, TableY
> where TableY.field2
> Field2 is equal to "fieldW like '%blabla%' "
> Thanks for your help

No comments:

Post a Comment