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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Using WHERE Clause Criteria stored in a field

Author  Topic 

diamondgeezer1974
Starting Member

1 Post

Posted - 2012-06-22 : 05:08:02
Hi all,

Is there a SQL command that allows you to use the text string stored in a field as part of the WHERE clause?

Example
Table 1 is a staff table, and Table 2 is a Work Category table.

I'm trying to determine what the correct work category in Table 2 is to assign to each staff member in Table 1.

They both have a field called [Platform]

The Platform values in the staff table could by something like :
DB: MySQL
DB: SQL Server
AS400
CICS

but the values in the [Platform] field in the work category table coul be something like :
NOT LIKE 'DB%'
...so a bit of SQL Query text.

Is it pobbisble to take that text in that field and include it in a WHERE clause using some form of concatenation of text strings so I can filter the saff table using that criteria?

Hope you can help.

Kind Regards

Matt

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-06-22 : 08:34:00
If the operator and the value to be compared is stored as one string in the Work Category table (e.g. NOT LIKE 'DB%'), you would need to use dynamic sql. (i.e., construct the query string and execute it use sp_executesql).

I have seen people use a two column table with the operator in one column and the value in another column and then construct the where clause conditionally using the info in the operator column, thus being able to construct the query without dynamic SQL. In your case where the operator and operand are in one column, doing that would be harder and less reliable.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-22 : 23:13:46
i dont think so this is a recommended desiign approach. why should you store SQL query search clause like this as a column value?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -