How to export dynamic SQL Server pivot table to excel
NickName:Rinus Ask DateTime:2015-04-22T16:58:19

How to export dynamic SQL Server pivot table to excel

I'm working on a webapp that displays data from a Microsoft SQL Server dynamic pivot table.

Normally I'd try and figure out a way to do the dynamic pivot in c#, but in this case the pivot has to be a SQL Server stored procedure because other apps also need access to the pivot table.

Here's the SQL:

DECLARE @DynamicPivot AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

SELECT 
    @ColumnName = ISNULL(@ColumnName + ',', '') 
                  + QUOTENAME(xml_tag_name)
FROM 
    (SELECT DISTINCT xml_tag_name FROM DataEntries) AS TagValues

SET @DynamicPivot =
N'SELECT DISTINCT capture_id, ' + @ColumnName + '
FROM DataEntries
PIVOT(MAX(xml_tag_value) 
FOR xml_tag_name IN (' + @ColumnName + ')) AS PVTTable'

EXEC sp_executesql @DynamicPivot

All the articles I've gone through deal with normal export to Excel or static pivots. eg: Export Table from SQL Server to Excel 2007 using C#.

How do I go about exporting this dynamic pivot to Excel?

Copyright Notice:Content Author:「Rinus」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29792506/how-to-export-dynamic-sql-server-pivot-table-to-excel

More about “How to export dynamic SQL Server pivot table to excel” related questions

How to export dynamic SQL Server pivot table to excel

I'm working on a webapp that displays data from a Microsoft SQL Server dynamic pivot table. Normally I'd try and figure out a way to do the dynamic pivot in c#, but in this case the pivot has to b...

Show Detail

Create Pivot query in SQL Server like Excel pivot table

I am trying to convert an Excel pivot to a SQL query so instead of extracting the data first from my SQL Server database then create the pivot table manually in Excel, I want to create the pivot qu...

Show Detail

Exporting dynamic column pivot result (Dynamic SQL resultset with varying number of columns) into excel file using SSIS

I have a dynamic pivot sql script (the pivoted columns are dynamic). I wanted to export the result set into excel file and email it with send mail task in ssis. Does anyone know how to do that? Bel...

Show Detail

Create table on SQL Server from dynamic pivot results

Is there a way to directly store the results of a dynamic pivot query into a fixed table? As the result is dynamic I can't create the table by specifying the columnnames and methods like "create ta...

Show Detail

export sqldata into excel from Pivot table store procedure

My Problem: my store procedure have dynamic column. so i cannot insert into tabe. so i need to export directly to excel. Below is my code. Below code is working fine. i need either export excel dir...

Show Detail

SQL Server - Dynamic PIVOT Table - SQL Injection

Sorry for the long question but this contains all the SQL I've used to test the scenario to hopefully make it clear as to what I'm doing. I'm build up some dynamic SQL to produce a PIVOT table in ...

Show Detail

SQL Server database to Excel pivot table

I have a SQL Server database with two tables (there are more, but this is for purpose of the question). Table Projects: ProdID int ProjectName String Table Tasks: TaskId int TaskName String Pr...

Show Detail

Dynamic export from SQL Server Database to Excel

I am building a patient record export system to extract data from SQL Server. The customer would like the data in the form of an Excel workbook. The way that the data is held is such that the table...

Show Detail

How to switch from SQL Server to MySQL for using with Excel pivot

I have a table that contains sales transaction (~20 mil rows). Previously I used SQL Server and export it to an Excel pivot. Data refresh took 10-15 minutes but still do-able. However, after I migr...

Show Detail

Dynamic PIVOT query - How to save it in SQL Server?

I had to built a dynamic PIVOT in SQL Server as described in this article using the following script: DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) DECLARE @ColumnName AS NVARCHAR(MAX) --Get distinct

Show Detail