Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I would like to create a temporary table in my application using C#.
First I need to check for a temporary table and then create the table if does not exist.

Can u help me?

Thanks in advance.
Posted
Updated 16-Nov-11 1:26am
v4
Comments
Mehdi Gholam 16-Nov-11 6:27am    
Just drop the table and recreate it.

Temp tables are prefixed with #, or ## for global temp tables, and are accessible by objectid.

Updating a temp table in a Stored Proc[^]
 
Share this answer
 
v2
SQL
CREATE PROCEDURE MakeMyTempTable
AS
BEGIN
IF (SELECT 1 FROM #tempTable /*you can call your table however you want but for a temporary table #*/) IS NULL 
CREATE TABLE #tempTable 
( id int
  --here you define your fields
)
END


Now, for create temp table call the procedure from c# or sql.
 
Share this answer
 
v2
below is the store procedure
which check if table already exists in TempDB.
(Temporary tables are getting created in TempDB in SQLServer.)
Drop that table
and will create a table
USE [sample1]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[temp]
as

Begin
IF EXISTS
(
SELECT * FROM tempdb.dbo.sysobjects WHERE ID = OBJECT_ID(N'tempdb..#Mytemp')

)

DROP TABLE #Mytemp
ELSE

CREATE TABLE #Mytemp(Col1 nvarchar(100), Col2 int)
End
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900