Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 directly from store procedure or how can i create dynamic table with store procedure

Pls help me.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_PIVOT_Seats]
AS
BEGIN
SET NOCOUNT ON;
DECLARE 
    @cols NVARCHAR(MAX),
    @stmt NVARCHAR(MAX)
SELECT @cols = ISNULL(@cols + ', ', '') + '[' + T.showdateformat + ']' FROM 
(SELECT  DISTINCT showdateformat FROM vw_Pivot_Seat_ALL) AS T

SELECT @stmt = ' SELECT * FROM vw_Pivot_Seat_ALL AS T
        pivot (SUM(T.Qty)  FOR T.showdateformat IN (' + @cols + ')) AS P'
END
EXEC sp_executesql @STMT=@STMT


Thank you

maideen
Posted

1 solution

Hello,

I will suggest that let this stored procedure return a resultset and use it to create an Excel file. Simplest way will be create a CSV file. Here[^] is some code to get you started.

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900