I need to insert the values of these selects into a table.
They return many rows and for each row I need to insert a column into a new table.
Ex:
select (select id from X where name=Contacts.Company) as IDClient,
FirstName + right(' '+ cast(LastName as varchar(20)), 20) as NewName,
(select id from Y where newid=8 and description=Contacts.JobTitle) as IDRole,
Initials
from Contacts
And I need to do something like this for each row of this select:
insert into TableX (IDClient,NewName,IDRole,'0','0','0',Initials,'XXX',GETDATE(),NULL,NULL)
Ty
EDITED
insert into TableX (IDClient,NewName,IDRole,'0','0','0',Initials,'XXX',GETDATE(),NULL,NULL)
select
(select id from X where nAme=Contacts.Company) as IDClient,
FirstName + right(' '+ cast(LastName as varchar(20)), 20) as NewName,
(select id from Y where newid=8 and description=Contacts.JobTitle) as IDRole,
Initials
from Contacts
When i execute this I’m getting this error:
“Incorrect syntax near ‘0’.”
Unless I am missing something you should be able to do this:
You will just use the
INSERT INTO...SELECT...FROM..syntax.Now if you do not have a table, then you can
SELECT..INTOa new temp table:Or if you want to use joins for this then:
Now your original post shows an insert of this:
You have two fields that do not have names with them, this is not correct syntax. In your insert statement to either have to names no columns and insert into all or name the columns that you want to insert values into. You cannot use an empty string
''ornullas column names. If you want these values to be inserted then you have to provide the names for them:Based on your edit, you need to use the following: