Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server存储过程中的数据透视和合并列_Sql_Sql Server_Tsql_Sql Server 2005_Stored Procedures - Fatal编程技术网

SQL Server存储过程中的数据透视和合并列

SQL Server存储过程中的数据透视和合并列,sql,sql-server,tsql,sql-server-2005,stored-procedures,Sql,Sql Server,Tsql,Sql Server 2005,Stored Procedures,我有一个包含以下列的表 Col1(bigint) | Col2(datetime)| Col3(nvarchar(100))| Col4(xml) 第4列是如下所示的xml,它可以包含任意数量的字段 <Fields> <Field1>10</Field1> <Field2>11</Field2> <Field3>10</Field3> <Field4>11</Field4>

我有一个包含以下列的表

Col1(bigint) | Col2(datetime)| Col3(nvarchar(100))| Col4(xml)
第4列是如下所示的xml,它可以包含任意数量的字段

<Fields>
  <Field1>10</Field1>
  <Field2>11</Field2>
  <Field3>10</Field3>
  <Field4>11</Field4>
</Fields>
参数2:
@Fields
(字符串类型以逗号分隔)

我期待以下格式的输出

Col1      |   Col2   |  Col3 | Field1 | Field2 | Field3 | Field4 
-----------------------------------------------------------------
2014051092|2014-05-14|Source1|   10   |   21   |  12    |   43 
2014051093|2014-05-14|Source1|   11   |   22   |  23    |   53 
我已经从存储过程中的第一个参数创建了一个表

INSERT INTO #TempPricing                     
 (                                                                              
    Col1,
    Col2,
    Col3                                                  
 )                                                                              
SELECT                                                                               
    Col1,
    Col2,
    Col3                               
    FROM OPENXML(@handle, '/Rowset/Row', 2)                                                                                 
 WITH                                                                               
 (                                                         
    Col1 bigint,
    Col2 datetime,
    Col3 varchar(50)      
 )  
如何在拆分后透视第二个参数,将其与上表合并并获取数据

编辑 输出也可以是以下格式

Col1      |   Col2   |  Col3 | Fields 
--------------------------------------
2014051092|2014-05-14|Source1|<Fields><Field1>10</Field1><Field1>11</Field1></Fields>
2014051093|2014-05-14|Source1|<Fields><Field1>20</Field1><Field1>21</Field1></Fields>
Col1 | Col2 | Col3 |字段
--------------------------------------
2014051092 | 2014-05-14 |来源1 | 1011
2014051093 | 2014-05-14 |来源1 | 2021

因此,上述任何格式都可以使用

让我们试试下面的脚本,它可能会对您有所帮助

declare @String varchar(max), @Result varchar(max)

select @String= COALESCE(@String+',', '')+ QUOTENAME(Col1) 
FROM (
select distinct Col1 from TableA A Inner Join dbo.Tab C VS on A.AttributeID =VS.AttributeId)AS B
set @Result ='
with pivotData AS
(
select distinct P.Col1, PA.col1,A.Col1,PA.col2 from Tab3 PA join Attribute A
on A.AttributeID=PA.AttributeID join Product P on P.ProductId=PA.ProductId
)
select Col1, Col2,
'+@String+' from pivotData
pivot(
Max(col2) for
Col1in('+@String+')
)as PR order by col3 desc'

exec (@Result)

通过使用问题中的指令,我能够实现上述目标
Col1      |   Col2   |  Col3 | Fields 
--------------------------------------
2014051092|2014-05-14|Source1|<Fields><Field1>10</Field1><Field1>11</Field1></Fields>
2014051093|2014-05-14|Source1|<Fields><Field1>20</Field1><Field1>21</Field1></Fields>
declare @String varchar(max), @Result varchar(max)

select @String= COALESCE(@String+',', '')+ QUOTENAME(Col1) 
FROM (
select distinct Col1 from TableA A Inner Join dbo.Tab C VS on A.AttributeID =VS.AttributeId)AS B
set @Result ='
with pivotData AS
(
select distinct P.Col1, PA.col1,A.Col1,PA.col2 from Tab3 PA join Attribute A
on A.AttributeID=PA.AttributeID join Product P on P.ProductId=PA.ProductId
)
select Col1, Col2,
'+@String+' from pivotData
pivot(
Max(col2) for
Col1in('+@String+')
)as PR order by col3 desc'

exec (@Result)