Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends, I have a requirement to create a new table from three tables as the sources. Table1 will act as a master table where all the other data tables (Table2 and Table 3) need to be gathered. I also need to add a date as the source for trending.
The below insert command is getting failed
INSERT INTO newtable  
(
  [Todaydate],
	[Name],
	[LOCATION],
	[Type],
)
VALUES
(
( 
SELECT GETDATE()
),
(
SELECT t1.Name, t2.Location, t3.Type
  from table1 t1
  LEFT JOIN table2 t2
  ON t1.Name=t2.Name
  LEFT JOIN table3 t3
  ON t1.Name=t3.Name
)
)


ERROR:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
The multi-part identifier "Name" could not be bound.
There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.


Please help. Thank you

What I have tried:

The SQL query has been changed but no luck
Posted
Updated 11-Mar-21 6:16am

Your syntax is wrong. You have all kinds of extra characters.

Keep it simple:

SQL
INSERT INTO newtable ([Todaydate], [Name],[LOCATION], [Type])
SELECT GETDATE(), t1.Name, t2.Location, t3.Type
from table1 t1
LEFT JOIN table2 t2 ON t1.Name=t2.Name
LEFT JOIN table3 t3 ON t1.Name=t3.Name
 
Share this answer
 
Comments
Maciej Los 12-Mar-21 1:52am    
5ed!
That is not a valid INSERT query.
INSERT (Transact-SQL) - SQL Server | Microsoft Docs[^]
SQL
INSERT INTO newtable  
(
    [Todaydate],
	[Name],
	[LOCATION],
	[Type]
)
SELECT 
    GETDATE(),
    t1.Name, 
    t2.Location, 
    t3.Type
FROM
    table1 t1
    LEFT JOIN table2 t2
    ON t1.Name=t2.Name
    LEFT JOIN table3 t3
    ON t1.Name=t3.Name
;
 
Share this answer
 
Comments
Maciej Los 12-Mar-21 1:52am    
5ed!

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