从 Select 查询创建 table
Create a table from a Select query
我有一个查询在 SQL Server 2008 R2 Express 中加入 7 table 总共 852 列。
我正在尝试使用该查询的输出创建一个新的 table
尝试使用 INTO,但由于 table 中的某些列具有重复名称,因此无法正常工作
USE VistaBI
SELECT *
INTO NewTable
FROM dbo.Agente FULL JOIN
dbo.Cte ON dbo.Agente.Agente = dbo.Cte.Agente
FULL JOIN
dbo.CteEnviarA ON dbo.Agente.Agente = dbo.CteEnviarA.Agente
FULL JOIN
dbo.Unidad ON dbo.CteEnviarA.Unidad = dbo.Unidad.Unidad
FULL JOIN
dbo.Venta ON dbo.Agente.Agente = dbo.Venta.Agente
FULL JOIN
dbo.VentaD ON dbo.Agente.Agente = dbo.VentaD.Agente
FULL JOIN
dbo.Art ON dbo.VentaD.Articulo = dbo.Art.Articulo
FULL JOIN
dbo.Alm ON dbo.Venta.Almacen = dbo.Alm.Almacen
有没有办法自动创建 table,而不必为 852 列中的每一列键入唯一的名称和数据类型?
select 列名显式而不是使用 *
下面我用两个表举个例子
select
a.person_id,
a.city,
city.city_id
into
mytable
from
table1 a
inner join
city on a.city = city.city
所以在你的情况下
SELECT table_name.col1,table_nae.col2..... use explicitly column name in selection
INTO NewTable
FROM dbo.Agente FULL JOIN
dbo.Cte ON dbo.Agente.Agente = dbo.Cte.Agente
FULL JOIN
dbo.CteEnviarA ON dbo.Agente.Agente = dbo.CteEnviarA.Agente
FULL JOIN
dbo.Unidad ON dbo.CteEnviarA.Unidad = dbo.Unidad.Unidad
FULL JOIN
dbo.Venta ON dbo.Agente.Agente = dbo.Venta.Agente
FULL JOIN
dbo.VentaD ON dbo.Agente.Agente = dbo.VentaD.Agente
FULL JOIN
dbo.Art ON dbo.VentaD.Articulo = dbo.Art.Articulo
FULL JOIN
dbo.Alm ON dbo.Venta.Almacen = dbo.Alm.Almacen
您可以先创建一个 table,然后使用动态更改语句添加您需要的列数,例如:
CREATE TABLE MYTABLE
(
A INT,
B INT
)
DECLARE @I INT = 1, @COLNAME VARCHAR(20),@SQL VARCHAR(MAX)
WHILE (@I < 852)
BEGIN
SET @COLNAME = CAST(@I AS VARCHAR(20))
SET @SQL ='
ALTER TABLE MYTABLE
ADD [' + @COLNAME + '] VARCHAR(MAX);'
PRINT @SQL
EXEC(@SQL)
SET @I = @I + 1
END
然后只需将插入语句更改为:
INSERT INTO mytable
SELECT *
FROM dbo.agente
FULL JOIN dbo.cte
ON dbo.agente.agente = dbo.cte.agente
FULL JOIN dbo.cteenviara
ON dbo.agente.agente = dbo.cteenviara.agente
FULL JOIN dbo.unidad
ON dbo.cteenviara.unidad = dbo.unidad.unidad
FULL JOIN dbo.venta
ON dbo.agente.agente = dbo.venta.agente
FULL JOIN dbo.ventad
ON dbo.agente.agente = dbo.ventad.agente
FULL JOIN dbo.art
ON dbo.ventad.articulo = dbo.art.articulo
FULL JOIN dbo.alm
ON dbo.venta.almacen = dbo.alm.almacen
这将为您提供列名称为 1、2、3... 等的结果,但它是一个动态名称,使您不必手动命名列。
我是动态做的,试试这个代码:列名会变成表名+列名所以不会有列名重复。
DECLARE @strColumns varchar(max) = ''
SELECT @strColumns = COALESCE(@strColumns + ',', '') + A.TableName + '.' + QuoteName(Column_Name) + ' AS ' + A.Alies
FROM (
SELECT Column_Name, 'Agente' TableName, 'Agente_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Agente' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'Cte' TableName, 'Cte_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Cte' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'CteEnviarA' TableName, 'CteEnviarA_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'CteEnviarA' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'Unidad' TableName, 'Unidad_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Unidad' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'Venta' TableName, 'Venta_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Venta' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'VentaD' TableName, 'VentaD_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'VentaD' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'Art' TableName, 'Art_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Art' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'Alm' TableName, 'Alm_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Alm' AND DATA_TYPE != 'timestamp'
) AS A
SELECT @strColumns =SUBSTRING(@strColumns,2, len(@strColumns) - 1)
SELECT @strColumns
EXEC( 'SELECT
'+ @strColumns +' INTO ##temp
FROM dbo.Agente
FULL JOIN dbo.Cte ON dbo.Agente.Agente = dbo.Cte.Agente
FULL JOIN dbo.CteEnviarA ON dbo.Agente.Agente = dbo.CteEnviarA.Agente
FULL JOIN dbo.Unidad ON dbo.CteEnviarA.Unidad = dbo.Unidad.Unidad
FULL JOIN dbo.Venta ON dbo.Agente.Agente = dbo.Venta.Agente
FULL JOIN dbo.VentaD ON dbo.Agente.Agente = dbo.VentaD.Agente
FULL JOIN dbo.Art ON dbo.VentaD.Articulo = dbo.Art.Articulo
FULL JOIN dbo.Alm ON dbo.Venta.Almacen = dbo.Alm.Almacen')
SELECT * FROM ##temp
DROP TABLE ##temp
我有一个查询在 SQL Server 2008 R2 Express 中加入 7 table 总共 852 列。 我正在尝试使用该查询的输出创建一个新的 table
尝试使用 INTO,但由于 table 中的某些列具有重复名称,因此无法正常工作
USE VistaBI
SELECT *
INTO NewTable
FROM dbo.Agente FULL JOIN
dbo.Cte ON dbo.Agente.Agente = dbo.Cte.Agente
FULL JOIN
dbo.CteEnviarA ON dbo.Agente.Agente = dbo.CteEnviarA.Agente
FULL JOIN
dbo.Unidad ON dbo.CteEnviarA.Unidad = dbo.Unidad.Unidad
FULL JOIN
dbo.Venta ON dbo.Agente.Agente = dbo.Venta.Agente
FULL JOIN
dbo.VentaD ON dbo.Agente.Agente = dbo.VentaD.Agente
FULL JOIN
dbo.Art ON dbo.VentaD.Articulo = dbo.Art.Articulo
FULL JOIN
dbo.Alm ON dbo.Venta.Almacen = dbo.Alm.Almacen
有没有办法自动创建 table,而不必为 852 列中的每一列键入唯一的名称和数据类型?
select 列名显式而不是使用 *
下面我用两个表举个例子
select
a.person_id,
a.city,
city.city_id
into
mytable
from
table1 a
inner join
city on a.city = city.city
所以在你的情况下
SELECT table_name.col1,table_nae.col2..... use explicitly column name in selection
INTO NewTable
FROM dbo.Agente FULL JOIN
dbo.Cte ON dbo.Agente.Agente = dbo.Cte.Agente
FULL JOIN
dbo.CteEnviarA ON dbo.Agente.Agente = dbo.CteEnviarA.Agente
FULL JOIN
dbo.Unidad ON dbo.CteEnviarA.Unidad = dbo.Unidad.Unidad
FULL JOIN
dbo.Venta ON dbo.Agente.Agente = dbo.Venta.Agente
FULL JOIN
dbo.VentaD ON dbo.Agente.Agente = dbo.VentaD.Agente
FULL JOIN
dbo.Art ON dbo.VentaD.Articulo = dbo.Art.Articulo
FULL JOIN
dbo.Alm ON dbo.Venta.Almacen = dbo.Alm.Almacen
您可以先创建一个 table,然后使用动态更改语句添加您需要的列数,例如:
CREATE TABLE MYTABLE
(
A INT,
B INT
)
DECLARE @I INT = 1, @COLNAME VARCHAR(20),@SQL VARCHAR(MAX)
WHILE (@I < 852)
BEGIN
SET @COLNAME = CAST(@I AS VARCHAR(20))
SET @SQL ='
ALTER TABLE MYTABLE
ADD [' + @COLNAME + '] VARCHAR(MAX);'
PRINT @SQL
EXEC(@SQL)
SET @I = @I + 1
END
然后只需将插入语句更改为:
INSERT INTO mytable
SELECT *
FROM dbo.agente
FULL JOIN dbo.cte
ON dbo.agente.agente = dbo.cte.agente
FULL JOIN dbo.cteenviara
ON dbo.agente.agente = dbo.cteenviara.agente
FULL JOIN dbo.unidad
ON dbo.cteenviara.unidad = dbo.unidad.unidad
FULL JOIN dbo.venta
ON dbo.agente.agente = dbo.venta.agente
FULL JOIN dbo.ventad
ON dbo.agente.agente = dbo.ventad.agente
FULL JOIN dbo.art
ON dbo.ventad.articulo = dbo.art.articulo
FULL JOIN dbo.alm
ON dbo.venta.almacen = dbo.alm.almacen
这将为您提供列名称为 1、2、3... 等的结果,但它是一个动态名称,使您不必手动命名列。
我是动态做的,试试这个代码:列名会变成表名+列名所以不会有列名重复。
DECLARE @strColumns varchar(max) = ''
SELECT @strColumns = COALESCE(@strColumns + ',', '') + A.TableName + '.' + QuoteName(Column_Name) + ' AS ' + A.Alies
FROM (
SELECT Column_Name, 'Agente' TableName, 'Agente_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Agente' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'Cte' TableName, 'Cte_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Cte' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'CteEnviarA' TableName, 'CteEnviarA_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'CteEnviarA' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'Unidad' TableName, 'Unidad_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Unidad' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'Venta' TableName, 'Venta_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Venta' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'VentaD' TableName, 'VentaD_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'VentaD' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'Art' TableName, 'Art_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Art' AND DATA_TYPE != 'timestamp'
UNION ALL
SELECT Column_Name, 'Alm' TableName, 'Alm_' + QuoteName(Column_Name) AS Alies FROM INFORMATION_SCHEMA.COLUMNS WHERE Table_Name = 'Alm' AND DATA_TYPE != 'timestamp'
) AS A
SELECT @strColumns =SUBSTRING(@strColumns,2, len(@strColumns) - 1)
SELECT @strColumns
EXEC( 'SELECT
'+ @strColumns +' INTO ##temp
FROM dbo.Agente
FULL JOIN dbo.Cte ON dbo.Agente.Agente = dbo.Cte.Agente
FULL JOIN dbo.CteEnviarA ON dbo.Agente.Agente = dbo.CteEnviarA.Agente
FULL JOIN dbo.Unidad ON dbo.CteEnviarA.Unidad = dbo.Unidad.Unidad
FULL JOIN dbo.Venta ON dbo.Agente.Agente = dbo.Venta.Agente
FULL JOIN dbo.VentaD ON dbo.Agente.Agente = dbo.VentaD.Agente
FULL JOIN dbo.Art ON dbo.VentaD.Articulo = dbo.Art.Articulo
FULL JOIN dbo.Alm ON dbo.Venta.Almacen = dbo.Alm.Almacen')
SELECT * FROM ##temp
DROP TABLE ##temp