Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Conditionally create a temp table with IF...ELSE
Message
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Stored procedures, Triggers, UDFs
Divers
Thread ID:
00869123
Message ID:
00869136
Vues:
13
Bruce,

A temporay table exists for the life of SQL session (connection) if not dropped explicitly. Add following code at the beginning and the end of your sproc.
-- Drop temp. table if it exists
IF object_id('tempdb..#SelectedCustomers') IS NOT NULL
	DROP TABLE #SelectedCustomers
>I am creating a temp table inside a store proc. Depending on whether an input
>parameter was passed, I run different code to create the temp table.
>
>The SQL compiler gives me this error when I try to run the code:
> There is already an object named #SelectedCustomers in the database.
>
>What am I doing wrong here?
>
>(This is a simplification of what I'm really working with)
>-- @tcState is an input parameter to the stored proc, and is in scope.
>IF @tcState IS NOT NULL BEGIN
>   SELECT kCustomer
>   INTO #SelectedCustomers
>   FROM Customers
>   WHERE cState = @tcState
>END
>ELSE BEGIN
>   SELECT kCustomer
>   INTO #SelectedCustomers
>   FROM Customers
>END
--sb--
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform