garethtnash
asked on
SQL Server Results to Excel File
Hello All,
I have a (MS SQL 2008) Stored Procedure that creates two resultsets -
Success
Error
I would like the SP to automatically save the resultsets to two new excel files,.. (Success(DDMMYY).xlsx & Error(DDMMYY).xlsx) where DDMMYY are the date run....
I'd like the person running it to be able to set the path for each file.
Any thoughts?
Thanks
I have a (MS SQL 2008) Stored Procedure that creates two resultsets -
Success
Error
I would like the SP to automatically save the resultsets to two new excel files,.. (Success(DDMMYY).xlsx & Error(DDMMYY).xlsx) where DDMMYY are the date run....
I'd like the person running it to be able to set the path for each file.
Any thoughts?
Thanks
ASKER
That looks really neat!
My SP generates an output based on data imported into three #TempTables from CSV reports, Using BULK INSERT,..
Could your example take it's input from the filenames + path, put the filename & path into a variable, use the variables to do the BULK INSERT and then output to two worksheets in the Work Book?
Appreciate your thoughts.
Thank you
My SP generates an output based on data imported into three #TempTables from CSV reports, Using BULK INSERT,..
Could your example take it's input from the filenames + path, put the filename & path into a variable, use the variables to do the BULK INSERT and then output to two worksheets in the Work Book?
Appreciate your thoughts.
Thank you
Hi garethtnash,
You can use bcp. Please try like below-
Hope it helps!
You can use bcp. Please try like below-
--
DECLARE @SQL AS VARCHAR(MAX) = 'SELECT * FROM [a]'
DECLARE @S AS VARCHAR(MAX) = ''
DECLARE @FilePath AS VARCHAR(1000) = 'C:\'
DECLARE @Dt AS VARCHAR(6) = FORMAT(GETDATE(),'ddMMyy')
SET @s = 'bcp "'+ @SQL + '" queryout ' + '(Success(' + @Dt + ')).xls' + ' -c -t\t -T -S ' + [ServerName\InstanceName]
EXEC MASTER..xp_cmdshell @s
SET @s = 'bcp "'+ @SQL + '" queryout ' + '(Failure(' + @Dt + ')).xls' + ' -c -t\t -T -S ' + [ServerName\InstanceName]
EXEC MASTER..xp_cmdshell @s
--
Hope it helps!
ASKER
Probably a silly question....! But where do I get the [ServerName\InstanceName] from?
Is the server name the name at the top tree and the instancename the database name?
Thanks Pawan
Is the server name the name at the top tree and the instancename the database name?
Thanks Pawan
Hi garethtnash,
Server name will the name of the server where you are working. If you are working on local system then you can use localhost.
database name is the name of the database on which you are working.
You can get those by running below queries-
Server name will the name of the server where you are working. If you are working on local system then you can use localhost.
database name is the name of the database on which you are working.
You can get those by running below queries-
--
SELECT @@SERVERNAME
SELECT @@servicename
--
Hope it helps!
ASKER
Great, so would --
work?
Thank you
--
DECLARE @SQL AS VARCHAR(MAX) = 'SELECT * FROM [a]'
DECLARE @S AS VARCHAR(MAX) = ''
DECLARE @FilePath AS VARCHAR(1000) = 'C:\'
DECLARE @Dt AS VARCHAR(6) = FORMAT(GETDATE(),'ddMMyy')
SET @s = 'bcp "'+ @SQL + '" queryout ' + '(Success(' + @Dt + ')).xls' + ' -c -t\t -T -S ' + @@SERVERNAME + \ + @@servicename
EXEC MASTER..xp_cmdshell @s
SET @s = 'bcp "'+ @SQL + '" queryout ' + '(Failure(' + @Dt + ')).xls' + ' -c -t\t -T -S ' + @@SERVERNAME + \+ @@servicename
EXEC MASTER..xp_cmdshell @s
--
work?
Thank you
Hi garethtnash,
Yes I think it should work. Please try it out on your dev environment.
Regards,
Pawan
Yes I think it should work. Please try it out on your dev environment.
Regards,
Pawan
ASKER
Hi Pawan,
One question.. you have declared @FilePath but I can't see it used?
Any thoughts
One question.. you have declared @FilePath but I can't see it used?
Any thoughts
Hi,
Updated -
Hope it helps!
Updated -
--
DECLARE @SQL AS VARCHAR(MAX) = 'SELECT * FROM [a]'
DECLARE @S AS VARCHAR(MAX) = ''
DECLARE @FilePath AS VARCHAR(1000) = 'C:\'
DECLARE @Dt AS VARCHAR(6) = FORMAT(GETDATE(),'ddMMyy')
SET @s = 'bcp "'+ @SQL + '" queryout ' + '(Success(' + @FilePath + @Dt + ')).xls' + ' -c -t\t -T -S ' + [ServerName\InstanceName]
EXEC MASTER..xp_cmdshell @s
SET @s = 'bcp "'+ @SQL + '" queryout ' + '(Failure(' + @FilePath + @Dt + ')).xls' + ' -c -t\t -T -S ' + [ServerName\InstanceName]
EXEC MASTER..xp_cmdshell @s
--
Hope it helps!
ASKER
No luck I'm afraid -
the code I use is
Warning: Null value is eliminated by an aggregate or other SET operation.
Msg 214, Level 16, State 201, Procedure xp_cmdshell, Line 1
Procedure expects parameter 'command_string' of type 'varchar'
the code I use is
BEGIN
DECLARE @SQL4 AS NVARCHAR(MAX) = 'Select * from'
DECLARE @S AS NVARCHAR(MAX) = ''
DECLARE @FilePath AS NVARCHAR(1000) = 'C:\SPTEST\'
DECLARE @Dt AS NVARCHAR(6) = /*FORMAT(GETDATE(),*/'ddMMyy'/*)*/
SET @s = 'bcp "'+ @SQL4 + '" queryout ' + '(Success(' + @FilePath + @Dt + ')).xls' + ' -c -t\t -T -S ' + @@SERVERNAME /*+ \ + @@servicename*/
EXEC MASTER..xp_cmdshell @s
/*SET @s = 'bcp "'+ @SQL + '" queryout ' + '(Failure(' + @FilePath + @Dt + ')).xls' + ' -c -t\t -T -S ' + [ServerName\InstanceName]
EXEC MASTER..xp_cmdshell @s*/
END
Query is incomplete.
Select * from
Tablename missing.
Select * from
Tablename missing.
ASKER
Yes, I removed that for posting here,.. with the full select statement I get the error above
Thanks
Thanks
ASKER
Hi Pawan,
Can I send you my SP directly? So you can see what I've done? I can't share any test data though..
Thanks
Can I send you my SP directly? So you can see what I've done? I can't share any test data though..
Thanks
Yes please.
ASKER
En-route - please can you only post the part that needs to be fixed back here :)
Thank you
Thank you
I shall check and reply soon.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I had similar problem, and Pawan solution worked!
As far as your exact requirement I don't have that code handy, but my knee-jerk reaction is that it would be a big hole in security to allow a role/group to execute SP's that write the results out to a file, and most DBA's I know would be very hesitant to grant such privs. This is definitely possible in an SSIS package though. Also possible in SSRS where the user goes into the report and selects Export to Excel.