简体   繁体   English

T-SQL插入列名作为第一行

[英]T-SQL Insert Column Name as First Row

I have the following table (Table_1): 我有下表(Table_1):

Seq  |  Name  |  Column1  |  Column2

There is another table (Table_2) with the following structure: 还有另一个具有以下结构的表(Table_2):

Name  |  Column1Table2  |  Column2Table2

I need to insert the Name of each column to the first table. 我需要将每个列的名称插入第一个表。 So, the result should be (Seq would be Row Number starting at 1): 因此,结果应该是(Seq是从1开始的行号):

+-----+------+---------------+---------------+
| Seq | Name |    Column1    |    Column2    |
+-----+------+---------------+---------------+
|   1 | Name | Column1Table2 | Column2Table2 |
+-----+------+---------------+---------------+

Assuming that Seq in the first table is an auto increment column, you should be able to just omit it during the insert, eg 假设第一个表中的Seq是一个自动增量列,那么您应该能够在插入过程中将其忽略,例如

INSERT INTO Table_1 (Name, Column1, Column2)
SELECT Name, Column1Table2, Column2Table2
FROM Table_2;

Seq would be Row Number starting at 1 Seq将是从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

Thank you Everyone. 谢谢大家。 I ended up using the Stuff function to concatenate the column names coming from the schema table and then Pivot the values into columns and insert them into the Temp table. 我最终使用了Stuff函数来连接来自模式表的列名,然后将值旋转到列中并将它们插入到Temp表中。 And finally insert the Temp table values into the main table. 最后将Temp表的值插入到主表中。 This post was very helpful. 这篇文章非常有帮助。

Efficiently convert rows to columns in sql server 在SQL Server中将行有效地转换为列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM