Seq would be Row Number starting at 1
Is that what are you trying to do?
;WITH CTE AS
(
SELECT ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) RN,
Name,
Column1Table2,
Column2Table2
FROM Table2
)
INSERT INTO Table1(Seq, Name, Column1, Column2)
SELECT RN,
Name,
Column1Table2,
Column2Table2
FROM CTE;
Demo:
CREATE TABLE Table1(
Seq INT,
Name VARCHAR(45),
Column1 VARCHAR(45),
Column2 VARCHAR(45)
);
CREATE TABLE Table2(
Name VARCHAR(45),
Column1Table2 VARCHAR(45),
Column2Table2 VARCHAR(45)
);
INSERT INTO Table2 VALUES
('Name1', 'Col1Value1', 'Col2Value1'),
('Name2', 'Col1Value2', 'Col2Value2');
;WITH CTE AS
(
SELECT ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) RN,
Name,
Column1Table2,
Column2Table2
FROM Table2
)
INSERT INTO Table1(Seq, Name, Column1, Column2)
SELECT RN,
Name,
Column1Table2,
Column2Table2
FROM CTE;
SELECT *
FROM Table1;
Returns:
+-----+-------+------------+------------+
| Seq | Name | Column1 | Column2 |
+-----+-------+------------+------------+
| 1 | Name1 | Col1Value1 | Col2Value1 |
| 2 | Name2 | Col1Value2 | Col2Value2 |
+-----+-------+------------+------------+
Live Demo
Update:
I need to insert the Name of each column to the first table
I don't think you are trying to insert the column names of a table in another table, but if so then why you want to do that? what are you trying to do really? and why not just
INSERT INTO Table1 (Seq, Name, Column1, Column2) VALUES
(1, 'Name', 'Table2Column1', 'Table2Column2');
--You can remove <Name> column and <1> value if it's an identity column