SQLTeam.com | Weblogs | Forums

Conditional check using the two tables


#1

Hi,

I have the below data

Table #1

ID TASKID TASKSTARTTIME Condition
100 2 07/30/2015 00:00:00 NULL
100 1 07/30/2015 08:00:00 NULL
100 4 07/30/2015 12:00:00 NULL
100 3 07/30/2015 15:00:00 VALUE> 100.000

Table #2

KEY ID TASKID VALUE
199 100 1 1000.000

I want to check the condition of the TASKID 3 from the table #1 by getting the VALUE from the table #2 for the TASKID1. If the condition satisfy, return true else false.

The data in the table #1 is ordered by TASKSTARTTIME. I have to check the previous task contains any VALUE in the table #2, if not then move one task up and check the VALUE exist or not.

Can someone help on achieve this through the sql query.


#2

declare @condition as varchar(100)
declare @qry as varchar(100)
select @condition = condition from #table1 where id = 100 and taskid=3
set @qry = 'select * from #table2 where ' + @condition + ' and id=100'
exec @qry

This will give you the output . But change it as per your requirement


#3

Please use exec sp_executesql to avoid injection attacks