I have a temp table and want to check in a where clause wether a certain id/string is contained in the temp table.
Select... WHERE MyId IN MyTempTable
I get a general error in MS SQL Management studio.
is the "In" operator not suited for temp tables?
The global temporary tables are created using the CREATE TABLE statement and their names must be prefixed with the double hashtag (##) sign. These tables can be accessed by all other sessions, unlike local ones.
While NOLOCK can "help" queries against *permanent* tables, it does not quite have the same effect against *temporary* tables - SQL Server knows that it doesn't have to worry about blocking other readers or writers, because #temp tables are scoped to a single, specific session.
Temporary Tables. A temporary table is a base table that is not stored in the database, but instead exists only while the database session in which it was created is active.
Your syntax is wrong:
SELECT ... FROM MyTable WHERE MyID IN (SELECT MyID FROM MyTempTable)
I don't much like the IN operator, so I prefer this:
SELECT ... FROM MyTable WHERE EXISTS (SELECT * FROM MyTempTable WHERE MyTable.MyID = MyID)
But it's largely a matter of taste.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With