April 14, 2010 at 1:09 pm
Mister Ken (4/14/2010)
I saw a coworker's snippet once, it reminded me vaguely of a similar idea. The statement below works.UPDATEt1
SET t1.ID = NULL
FROM dbo.table1 t1
WHERE EXISTS(SELECT * FROM #table2 t2 WHERE t2.ID = t1.ID)
But I guess for my Select statement, I'll have to take a different approach.
Using the same sort of subquery above as an existence check is different than the derived table method where values are coming from the derived table and from outer tables. If you perform a select using an existence check as you did above (rather than an update), that would work too.
eg
Select *
FROM dbo.table1 t1
WHERE EXISTS(SELECT * FROM #table2 t2 WHERE t2.ID = t1.ID)
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
April 14, 2010 at 2:32 pm
Mister Ken, I think the problem with the original query you posted is that you built a derived table, and properly aliased it, but then when you joined it to the other table your join condition referenced #table2 instead of the "lookup" alias.
Other than that, I don't see anything wrong with the original query you posted. That is the proper way to build a correlated sub-query.
If you are still having problems, can you post a few sample records for each table, so that we can use that to test with?
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
April 14, 2010 at 2:43 pm
The Subquery will also throw an error
The multi-part identifier "#table1.field2" could not be bound.
On the line:
select field1, count(*) line_count from #table2 where field2 > #table1.field2 group by field1
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
April 14, 2010 at 4:10 pm
I have played with this some more to fit into the desired results and requirements.
select t.field1, (
select count(*) line_count from #table2 where field2 > t.field2 group by field1
)
from #table1 t
You'll notice that the inner is referencing the outer now. The difference is that the subquery is moved up into the select list rather than remain in the from clause.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
April 14, 2010 at 7:00 pm
Jason you are king dude! Just awesome!
I've seen that "(select blah bah)" within the field list before, but never really used it. Now I know the power of it.
I should know that anybody that follows Kimberly Tripp has some skill! 🙂
Thanks again!
Thanks also to all the other fellas that chimed in too. If I had more time, I'd be hanging out here too. Lots of good stuff.
April 14, 2010 at 8:10 pm
Mister Ken (4/14/2010)
Jason you are king dude! Just awesome!I've seen that "(select blah bah)" within the field list before, but never really used it. Now I know the power of it.
I should know that anybody that follows Kimberly Tripp has some skill! 🙂
Thanks again!
Thanks also to all the other fellas that chimed in too. If I had more time, I'd be hanging out here too. Lots of good stuff.
You're welcome. And thanks for the compliments.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
Viewing 6 posts - 16 through 20 (of 20 total)
You must be logged in to reply to this topic. Login to reply