2

I want to bcp all tables into files from a database:

SELECT 'EXEC xp_cmdshell ''bcp '           --bcp
+  QUOTENAME(DB_NAME())+ '.'               --database name
+  QUOTENAME(SCHEMA_NAME(SCHEMA_ID))+ '.'  -- schema
+  QUOTENAME(name)                         -- table
+ ' out c:\temp\'                          -- output directory
+  REPLACE(SCHEMA_NAME(schema_id),' ','') + '_'
+  REPLACE(name,' ','')                    -- file name
+ '.txt -T -c'''   -- extension, security
FROM sys.tables

it produces statements like this:

EXEC xp_cmdshell 'bcp [AdventureWorks2012].[Production].[ScrapReason] out c:\temp\Production_ScrapReason.txt -T -c'

EXEC xp_cmdshell 'bcp [AdventureWorks2012].[HumanResources].[Shift] out c:\temp\HumanResources_Shift.txt -T -c'

so what I want is to iterate over above statements and execute all them. How to do this?

2 Answers 2

4

Note this assumes all commands are unique.

DECLARE @Commands TABLE(CommandText NVARCHAR(4000));
DECLARE @SQL NVARCHAR(4000);

INSERT INTO @Commands
SELECT 'EXEC xp_cmdshell ''bcp '           --bcp
+  QUOTENAME(DB_NAME())+ '.'               --database name
+  QUOTENAME(SCHEMA_NAME(SCHEMA_ID))+ '.'  -- schema
+  QUOTENAME(name)                         -- table
+ ' out c:\temp\'                          -- output directory
+  REPLACE(SCHEMA_NAME(schema_id),' ','') + '_'
+  REPLACE(name,' ','')                    -- file name
+ '.txt -T -c'''   -- extension, security
FROM sys.tables

WHILE (SELECT COUNT(*) FROM @Commands) > 0
BEGIN --Command Processing
    SET @SQL = (SELECT TOP 1 CommandText FROM @Commands)
    --PRINT (@SQL)
    EXEC (@SQL)
    DELETE FROM @Commands WHERE CommandText = @SQL
END
Sign up to request clarification or add additional context in comments.

3 Comments

I have not tried yet because I had not time but the backup (c:\temp\...) done where is IT placed? in my local machine or in the remote machine?
On the server you run the script on. To get it elsewhere us a unc path.
Watch out for Server blocked access to procedure 'sys.xp_cmdshell' which has to be enabled to be able run the above sql. I avoided this by altering the sql to print the bare bcp commands and creating a .bat file from that.
0

Default setting is that you can't execute bcp command from SQL server. This version works from DOS, Powershell and BAT file.

I didn't find setting from BCP which would export the heading line on these CSV files, so note that these CSV files don't include heading.

-- Run this command to the database you wish to export
DECLARE @Commands TABLE(CommandText NVARCHAR(4000));
DECLARE @SQL NVARCHAR(4000);

INSERT INTO @Commands
SELECT 'bcp '           --bcp
+  QUOTENAME(DB_NAME())+ '.'               --database name
+  QUOTENAME(SCHEMA_NAME(SCHEMA_ID))+ '.'  -- schema
+  QUOTENAME(name)                         -- table
+ ' out c:\temp\csvdump\'                  -- output directory
+  REPLACE(SCHEMA_NAME(schema_id),' ','') + '_'
+  REPLACE(name,' ','')                    -- file name
+ '.csv -T -C 65001 -t "|" -k -c -S localhost'   -- extension, security
FROM sys.tables

-- Copy-paste results to DOS or Powershell prompt or create a BAT file from these.
SELECT 'mkdir c:\temp\csvdump'
UNION ALL
SELECT * FROM @Commands

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.