Reputation: 569
I have my sql query like this
if not exists(select RowId from dbo.Cache where StringSearched = @FirstName and colName = 'FirstName')
begin
--some code here
end
The purpose of above if statement is not to execute the piece of code inside of it if value of StringSearched
is already present in Cache table which means it has been looked up before and so no need to make calculations again. The code inside of if statement if executed returns row number of rows from Table B
and those are then inserted into Cache table to continue maintaining the cache. anyway .I need the records to be picked from Cache only if ModifiedAt
column of Cache
table is latest than ModifiedAt
column of rows of Table B
.
Note: I understand that I may need to use a subquery
in where
clause but in where
clause itself, I need to check ModifiedAt
column of Table B
only for RowId'
s returned by Outer select
query .
How can I proceed without making it much complex ?
Upvotes: 0
Views: 69
Reputation: 8892
You can use the subquery
in the current query along with the Where
clause.You didn't specified what are the columns to know for figure out which rows to get value so I assumed your tableB
also has StringSearched
and colName
to get max(ModifiedAt)
for that string vlaue.
IF NOT EXISTS (SELECT * from dbo.Cache as c WHERE StringSearched = @FirstName
AND colName = 'FirstName'
AND ModifiedAt > (Select MAX(ModifiedAt) FROM tableB as tabB WHERE tabB.RowID = c.RowID ))
BEGIN
--your query
END
Upvotes: 1