如何只将一个表的列名插入SQL中的另一个表中 [英] How to insert only one table's column name into another table in SQL
本文介绍了如何只将一个表的列名插入SQL中的另一个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
how to insert only one table's all column name into another table's only one column in sql
我尝试了什么:
我不知道
sql - 将列名插入表中 - 堆栈溢出 [ ^ ]
推荐答案
尝试:
Try:
INSERT INTO dbo.DestinationTable (ColumnName)
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'SourceTable'
如果我正确理解了这个问题,您希望列表在单行中的单个列。如果是这种情况,请考虑以下事项:
If I understand the question correctly, you want the list into a single column in a single row. If that is the case, consider the following:
WITH ColumnnList (List) AS (
SELECT ';' + c.Column_Name
FROM INFORMATION_SCHEMA.Columns c
WHERE c.Table_Name = 'SOURCETABLENAME'
FOR XML PATH ('')
)
INSERT INTO TARGETTABLENAME (TARGETCOLUMNNAME)
SELECT SUBSTRING(a.List, 2, 99999)
FROM ColumnnList a
请记住更改:
- SOURCETABLENAME到要列出的表的名称列
- TARGETTABLENAME到要插入数据的表的名称
- TARGETCOLUMNNAME到目标表中目标列的名称
- 可选将分隔符更改为您想要的。我使用了分号(
;
) - SOURCETABLENAME to the name of the table for which to list the columns
- TARGETTABLENAME to the name of the table where you want to insert the data
- TARGETCOLUMNNAME to the name of the target column in the target table
- optionally change the separator to what you wish. I've used semicolon (
;
)
Just remember to change:
这篇关于如何只将一个表的列名插入SQL中的另一个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文