I am working on a task where i had to enhance the performance of the stored procedure. However, i had stucked in an issue is that i am unable to use SELECT INTO in a conditional statement.
I had gone through some links like INSERT INTO vs SELECT INTO and sql server conditional select into temp table.
The query is as below:
IF(@abc = 7)
BEGIN
SELECT MemberID INTO #TempMember from dbo.Member WHERE ....
END
ELSE
BEGIN
SELECT MemberID INTO #TempMember from dbo.Member WHERE ....
END
The query above ends up showing "there is already an object named '#TempMember' in the database" at the ELSE statement. Worth pointing that i had not create a temp table for #TempMember and the if statement is not in a loop as well.
But just why does the output shows the #TempMember exist but we know that only one SELECT statement will be performed in the query above as it is a conditional?
The reason for me to use SELECT INTO because there will be 500,000 datas to be dealt with in this statement. And INSERT INTO will make the performance bad. I am unable to convert it to dynamic SQL as well because of the practice of my company.
Is there any way that I can do to use both SELECT INTO in the conditional or any better practice to insert data into the #TempTable for performance wise?