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

I need to insert data in TableA from below query and the Insert and Select should run as single statement , But I am getting error says 'Incorrect syntax near If' for below query.
What is wrong here?

Thanks in advance.

Insert into TableA (Col1, Col2, Col3, Col4)

IF Object_ID ('temp..#Temp') Is not Null
Drop Table #Temp
Create Table #Temp
(
 ColA Varchar (100),
 ColB Int
)

Insert into #Temp
Select ColA, ColB from TableB

Select Col1, Col2, Col3, Col4
From TableC C
Inner join #Temp T On C.Col1=T.ColB


What I have tried:

I tried using braces also , But did not work.
Posted
Updated 10-Dec-22 2:18am
v2

1 solution

The SQL INSERT INTO statement has a very specific format: SQL INSERT INTO Statement[^] which you are not meeting - you have no VALUES supplied, just column names.
 
Share this answer
 
Comments
CPallini 1-Jul-22 8:34am    
5.
Member 10833473 1-Jul-22 8:41am    
Ok, So If I want to insert values, What I should change in that code?
Can You frame it in above code and share. Thanks.
OriginalGriff 1-Jul-22 9:32am    
How do you expect me to have any idea what you are trying to INSERT from where?
Member 10833473 1-Jul-22 10:04am    
I am trying to insert values from Table C, which joins from Temp table and It should insert into Table A. All this statements should run in 1 query. This is the requirement. Thanks.

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