Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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 tableour table isid intoperator 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 >0BEGIN 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
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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs