How to insert into a table from temp table?
21,965
Correct syntax:
insert into MyTable(Id,Name)
select t.ID, t.Name
From #temp t
Always read manual
Author by
Jasmine
Updated on September 03, 2020Comments
-
Jasmine over 3 years
I already have values in a temp table and I want to insert it into my table.
I follow this syntax
IF NOT EXISTS (SELECT 1 FROM ABC abc JOIN #Temp t ON abc.ID = t.ID) insert into MyTable(Id,Name) select values (t.ID, t.Name) From t
I have just the name t as an alias I created in a condition before this insert.
Is this correct? Some people use @ etc. I am confused.
-
Jasmine over 8 yearsThanks, I always see that some people who use @, they use @ in first field and rest all just field name and in your query, you use t in last field and not in first field? Confused..
-
Jasmine over 8 yearsEveryone's answer is different here :( I am confused
-
Lukasz Szozda over 8 years@Deevinee You can't use alias defined elsewhere, Alias always work in one current instruction, so you need
insert into MyTable(Id,Name) select ID, t.Name From #temp t
-
Jasmine over 8 yearsThank you, and my question about t in Name and not in ID ?
-
Lukasz Szozda over 8 years@Deevinee You want ID from parameter? Like
insert into MyTable(Id,Name) select @ID, t.Name From #temp t
-
Jasmine over 8 yearsNo, just confused why you didn't use t.ID? and just ID but in 2nd field you use t.Name and not just name...
-
Lukasz Szozda over 8 years@Deevinee If you have one table and no ambiguous name you can skip alias part.
-
Jasmine over 8 yearsThank you :) I am leaving home. Its 9 PM. I came at 8 AM :( Damn !!! Haha somehow got two SPs down :) Thanks so much for your help, see you tomorrow :) Have a good day....