I want to do a check (if statement) and then create one of two possible procedures.
Right now I am trying to use a IF EXISTS statement inside a CREATE PROCEDURE statement.
CREATE PROCEDURE [dbo].[TestProc]
AS
SET NOCOUNT ON
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER OFF
IF EXISTS (select * from table)
BEGIN
DECLARE @region NVARCHAR(100)
SELECT *
INTO #TempTable
FROM User
…do something with #TempTable etc..
DROP #TempTable
END
ELSE
BEGIN
DECLARE @region NVARCHAR(100)
SELECT *
INTO #TempTable
FROM User
…do something else with #TempTable etc
DROP #TempTable
END
I get the following 2 errors
There is already an object named 'TempTable' in the database.
The variable name '@region' has already been declared. Variable names must be unique within a query batch or stored procedure.
1.You need use two different temp table and before SELECT * INTO statements DROP them
2.Variable need once declared in the body procedure. In your case before IF EXISTS