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
 General SQL Server Forums
 New to SQL Server Programming
 how to use operator in where clause

Author  Topic 

Vishal_sql
Posting Yak Master

102 Posts

Posted - 2014-02-13 : 02:21:14
we have a column in table which represents operator like
>,>=,<,<=, =
and we want to use it in sql query depending on the condition for each id column in table

our table is


id int
operator nchar(10)

1,>=
2,<=
3,>
4,<
5,=
.
.
.
.


i need to use the operator from table into where clause for using condition for all the ids instead of single operator.

i tried it using the dynamic query but how can we execute it for each id in single query.

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2014-02-13 : 06:21:33
Sample code

DECLARE @OperatorId int , @Value VARCHAR(10) = 100, @Operator VARCHAR(10) = ''
SELECT @OperatorId = id fROM OPr
WHILE @OperatorId >0
BEGIN
SELECT @Operator = Operator fROM OPr WHERE ID = @OperatorId
DECLARE @sql VARCHAR(MAX) = 'SELECT * FROM EMPLOYEEs WHERE employee_id '+ @Operator + ' ' + @Value +';'
EXEC( @sql)
SET @OperatorId = @OperatorId - 1;
END


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-13 : 08:02:36
this is not a good approach. Why is system designed like this. why cant you determine the operation to be done beforehand. Can you explain your usecase?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -