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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add dynamic where clause

Status
Not open for further replies.

simco

Programmer
May 25, 2001
41
0
0
CA
Hi,

I am trying to dynamically add a where clause to any existing SQL SELECT statement (any SELECT that comes from the user). The purpose is to allow them to select only the records flagged in a certain way. And, since the flag field is the same in all tables, I was thinking that a solution might be adding a where clause to any incoming SELECT statement. My problem is where to place this WHERE, since the SELECT can be in any format (from multiple table, with/without join, subqueries, groupings etc).

Any idea is appreciated,
Thank you

S
 
delcare @SQLCommand VarChar(4000)

Set @SQLCommand = @TheirInputQuery + 'WHERE X = 123'

EXEC (@SQLCommand)

Thanks

J. Kusch
 
What if the Select statement looks like:
"SELECT field1 from table1 inner join table2 on table1.id=table2.id group by field1"

and I need to restrict both table1 and table2 values. (adding it to the end is going to cause an error in this case)

See, that's my problem, it has to work in any situation, for all the tables involved.

Thank you anyway :)
 
You would have to do some pretty heavy, tricky parsing of their command to be able to build your WHERE clause. Almost a query building tool.

Thanks

J. Kusch
 
JayKusck is right. Try finding the WHERE clause in the SQL string, and after that word, insert your clause.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top