I am creating a stored procedure but it throws an error.
Can anybody tell me if there is something i am doing wrong.
CREATE TABLE tbl1 (ID int)
INSERT INTO tbl1 SELECT 1
CREATE TABLE tbl2 (ID int)
INSERT INTO tbl2 SELECT 2
CREATE PROCEDURE QOTD (@source INT)
AS
BEGIN
IF @source = 1
SELECT ID INTO #tmpID FROM tbl1
ELSE
SELECT ID INTO #tmpID FROM tbl2
SELECT ID FROM #tmpID
END
ERROR:There is already an object named
‘#tmpID’ in the database.
Sometimes the T-SQL compiler gets confused, and doesn’t realise that only one side of an IF would be followed.