Last active
August 29, 2015 14:12
-
-
Save milafrerichs/6d72bb04a50ff8a3765a to your computer and use it in GitHub Desktop.
Export all Data and Headers from CIP MSSQL Database to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE developmentdataSQL | |
SELECT 'exec master..xp_cmdshell' | |
+ ' ''' | |
+ 'bcp' | |
+ ' "' + TABLE_CATALOG + '.' + TABLE_SCHEMA + '.[' + TABLE_NAME | |
+ ']" out' | |
+ ' "C:\cip\' | |
+ TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME + '.csv"' | |
+ ' -c' | |
+ ' -t,' | |
+ ' -T' | |
+ ' -S' + @@SERVERNAME | |
+ '''' | |
FROM INFORMATION_SCHEMA.TABLES | |
WHERE TABLE_TYPE = 'BASE TABLE' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE developmentdataSQL | |
SELECT 'exec master..xp_cmdshell' | |
+ ' ''' | |
+ 'bcp' | |
+ ' "DECLARE @colnames VARCHAR(max); ' | |
+ 'SELECT @colnames = COALESCE(@colnames + '''','''', '''''''') + column_name from ' | |
+ TABLE_CATALOG+'.INFORMATION_SCHEMA.COLUMNS where ' | |
+ 'TABLE_NAME='''''+ TABLE_NAME + '''''' | |
+ '; select @colnames;"' | |
+ ' queryout' | |
+ ' "C:\cip\' | |
+ TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME + '_header.csv"' | |
+ ' -c' | |
+ ' -t,' | |
+ ' -T' | |
+ ' -S' + @@SERVERNAME | |
+ '''' | |
FROM INFORMATION_SCHEMA.TABLES | |
WHERE TABLE_TYPE = 'BASE TABLE' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment