Showing posts with label temporary. Show all posts
Showing posts with label temporary. Show all posts

Thursday, March 22, 2012

Check if temporary table exists

Hello.

How can I check if a temporary table exists in the current context?

With normal tables I'd do a

EXISTS ( SELECT name FROM sysobjects

WHERE name='myTableName' AND type='U')

However, I can't do that with a temporary table. I'd have to go look at the sysobjects table in the tempdb database.

The problem is that for temporary tables, a suffix is added to the name to make it unique for each scope. I can't change the WHERE clause to name LIKE 'myTableName%' because this would return true if a temporary table with the same name exists in a different scope.

Any ideas?

Carlos

You can try the following

IF object_id('tempdb..#MyTempTable') IS NOT NULL
BEGIN
DROP TABLE #MyTempTable
END

CREATE TABLE #MyTempTable
(
ID int IDENTITY(1,1),
SomeValue varchar(100)
)
GO

|||IF OBJECT_ID('tempdb..#MyTempTable', 'U') IS NOT NULL
Print 'Yes'
Else
Print 'No'sql

Sunday, March 11, 2012

Check data Availbility between two table in Sqlserver 2005 using C#

I have Two table.One is MainTable and other is Temporary table.Both have Same Field

Before entering data into main table I want to check weather these data's are already there or not.

Here i am checking only three column of temporary table to the MainTable.

INSERT INTO MailTable(A, B, C)
SELECT (A, B, C) FROM TemporaryTable WHERE TemporaryTable.A NOT IN (SELECT A FROM MailTable)

|||

Thank you Thank you Thank you Thank you Very Much