stored procedure to export to CSV with BCP - sql-server

I need to create an on-demand export of user data on our website. The user clicks an export button, classic ASP code executes a stored procedure that generates a file via BCP, and the user is prompted to download it.
I've created the sproc, and its working flawlessly when executed from SSMS. The catch is getting it to work from the site with the limited privileges granted to the account connecting to SQL from the website. Here is a snippet:
-- INSERT TEMP DATA
INSERT INTO t_users_tempExport
SELECT * FROM #tempExport
-- show advanced options
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
-- enable xp_cmdshell
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE
-- hide advanced options
EXEC sp_configure 'show advanced options', 0
RECONFIGURE
-- EXPORT TO CSV
DECLARE #sql varchar(8000)
SELECT #sql = 'bcp "select * FROM DBNAME.dbo.tempExport WHERE scopeID='''+#randomString+'''" '
+ 'queryout C:\temp\exportResidents_'+CONVERT(varchar(max),#userID)+'.csv -c -t, -T -S'
+ ##servername
EXEC master..xp_cmdshell #sql
-- RETURN FILE NAME
SELECT 'C:\temp\export_'+CONVERT(varchar(max),#userID)+'.csv' AS fileName
The issue is that I cannot enable xp_cmdshell with the privledges granted to the account that is connecting to SQL from the website. Im kind of at a loss as to how to proceed.
Is it possible to include the sysadmin credentials in the call to BCP? Is there some easier option or work around?

I ended up going a completely different route. I created the CSV file from pure ASP code, only using the sproc to return the data.

Related

How to export SQL data records to CSV format using stored procedure?

Is it possible to export records to csv format using SQL script in stored procedure?
I am trying make job schedule, that will export records into a .csv file. I am using SQL Server 2012.
I don't want to make small application for just an exporting. That's why I am trying to make a script and add it in schedule. For example, I have records like
EmpID EmployeeName TotalClasses
---------------------------------
01 Zaraath 55
02 John Wick 97
File destination location is D:/ExportRecords/file.csv
In SSMS you can save the query result in CSV format.
Try This Below Query
-- To allow advanced options to be changed.
EXECUTE sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXECUTE sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
declare #sql varchar(8000)
select #sql = 'bcp "select * from DatabaseName..TableName" queryout d:\FileName.csv -c -t, -T -S' + ##servername
exec master..xp_cmdshell #sql
You have to create Empty FileName.csv in D:\
You could use BCP (Bulk Copy Program) the built-in cli for exporting data from SQL Server. It's low overhead, executes fast, and has lots of feature switches, like -t (which creates CSV file) which make it good for scripting.
Something like this. The Docs are useful as well
BCP dbo.YourTable out D:/ExportRecords/file.csv -c -t

Export the content of a SQL Server table to a CSV without using xp_cmdshell

I need to export the content of a Table into a file CSV.
I tried to use the execution of a xp_cmdshell from a stored procedure but it doesn't work because this component is turned off as part of the security configuration for this server.
Do you know others way to write a file from a stored procedure?
Here are a couple of methods you can try :
1. Using BCP
syntax :
bcp "SELECT * FROM Database.dbo.input" queryout C:\output.csv -c -t',' -T -S .\SQLEXPRESS
microsoft document : BCP Utility
2. Second Method(for excel, but should work for csv too) :
Insert into OPENROWSET
('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;',
'SELECT * FROM [SheetName$]') select * from SQLServerTable
For this, you need to enable adhoc distributed queries by following command :
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
GO
you might encounter a link server error. So you should refer : this stack overflow solution

How to Extract the Query result to directly to .XLSx File

Could you please let me know how to Extract Query result to Excel sheet in SQL server,
My query is batch Job, so I need to keep all my query result in Excel sheet, Later I will do FTP.
Please suggest me is there any way to do in SQL Server.
Note :- Not using Result to File in Management studio, I need to know using any scripts in Sql
Try this,
First Enable Ad Hoc Distributed Queries
sp_configure 'show advanced options', 1;
RECONFIGURE;
sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
GO
Export Result Query to Excel File
DECLARE #STR_QUERY AS NVARCHAR(MAX)
DECLARE #FILE_ATT_PATH NVARCHAR(50) ='D:\MYEXCEL'+REPLACE(CONVERT(VARCHAR,GETDATE(),113),':','')+'.XLS';
SET #STR_QUERY =
N'INSERT INTO OPENROWSET(''Microsoft.Jet.OLEDB.4.0'', ''Excel 8.0;Database='+ #FILE_ATT_PATH +';'',''SELECT CusSName FROM [Sheet1$]'')
SELECT CusSName FROM [dbo].[MasterCustomer]'
EXEC sp_executesql #STR_QUERY

Quicker way to export SQL tables into separate pipe-delimited files?

I will need to export the tables in the DB, but into separate files. Instead of running the Export wizard in SQL Server Management Studio for each table, is there a quicker way to accomplish this? The data will need to be in pipe-delimited form. I found a solution, but it doesn't pull the data, just the table definitions.
Here's one way to do it. You can enable xp_cmdshell
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
Then use the undocumented sp_msforeachtable and bcp to export to a file pipe delimited.
EXECUTE sp_msForEachTable
'EXECUTE master.dbo.xp_cmdshell ''bcp "SELECT * FROM ?" queryout D:\Data\?.txt -t "|" -c -T -S ServerName\InstanceName'''
This works and you'll want to make sure you disable xp_cmdshell if it wasn't already enable as it can be exploited (there's plenty to read on this). Also make sure you have permissions to write the files to wherever they need to go.
You can try using bcp (look here https://msdn.microsoft.com/en-us/library/ms162802.aspx) where you can specify the export format to use pipe for the delimiter. The only drawback is that you will need to specify the table name each time so you will probably need to create a list with the table names and then use a powershell script to loop on the list and execute bcp.
I also found the following which helped, assuming the sp_configure has been set for xp_cmdshell as #SQLChao pointed out in his solution:
Execute sp_MSforeachtable
'Execute master.dbo.xp_cmdshell ''sqlcmd -S DATABASE -E -d mydb -q "SET NOCOUNT ON SELECT * FROM ?" -W -o C:\TEMP\?.bak -s "|"'''
To remove the dashes under column names in the output files:
Execute sp_MSforeachtable
'Execute master.dbo.xp_cmdshell ''findstr /R /C:"^[^-]*$" c:\temp\?.bak > c:\temp\?.txt'''
Finally, remove the .bak files created:
Execute master.dbo.xp_cmdshell 'del c:\temp\*.bak'

SQL Server xp_cmdshell fail to export data

I want to export values from a column (TcpIpAddress) from a table called dbo.DimServere to a plain text (located in the server). I have sysadmin rights.
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1; -- 1 for at enable
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
-- Extracting information from the databse
EXEC xp_cmdshell 'bcp "SELECT TcpIpAddress FROM [SIT-DVH].[dbo].[DimServere]" queryout "C:\Users\b013904\Desktop\Output\bcptest.txt" -T -c -t,'
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To disable the feature.
EXEC sp_configure 'xp_cmdshell', 0; -- 0 for at disable
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
However, when I run this script, I get the following message and no file is been created:
What am I doing wrong?
The path in that bcp statement will be relative to the server since you're executing it on the server.
Does that path exist on the server?
Also, try changing the path to something more accessible like c:\output. .. then you can play around with the permissions on that folder to ensure that is not a os permission that's causing the statement to fail.
Hope that helps

Resources