Created
November 8, 2017 17:27
-
-
Save karoltheguy/0f3a635200f28ed179c0e1cc69ad39aa to your computer and use it in GitHub Desktop.
Export Column of text into individual files
This file contains 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
EXEC sp_configure 'show advanced options', 1; | |
GO | |
RECONFIGURE; | |
GO | |
EXEC sp_configure 'xp_cmdshell',1; | |
RECONFIGURE | |
GO | |
CREATE TABLE TempProcessStore ([RowNo] [smallint] identity (1, 1) | |
,[name] nvarchar(128) | |
,[ProcessType] nvarchar(50) | |
,[processxml] nvarchar(MAX)) | |
INSERT INTO TempProcessStore ([name], [ProcessType], [processxml]) | |
SELECT name | |
--,ProcessType | |
,CASE | |
WHEN ProcessType = 'O' THEN 'BPA Object - ' | |
WHEN ProcessType = 'P' THEN 'BPA Process - ' | |
END | |
,processxml | |
FROM BPAProcess | |
DECLARE @First [smallint] | |
,@Last [smallint] | |
,@FileName nvarchar(128) | |
,@SQLCommand [varchar](500) | |
,@ProcessType [nvarchar](50) | |
SELECT @First = MIN([RowNo]) FROM TempProcessStore | |
SELECT @Last = MAX([RowNo]) FROM TempProcessStore | |
WHILE @First <= @Last | |
BEGIN | |
SELECT @FileName = [name] FROM TempProcessStore WHERE [RowNo] = @First | |
SELECT @ProcessType = [ProcessType] FROM TempProcessStore WHERE [RowNo] = @First | |
SET @SQLCommand = 'EXEC xp_cmdshell ''bcp "SELECT processxml FROM dbR2D2_BP_Testing.dbo.TempProcessStore WHERE [name] = ''''' | |
+ @FileName + '''''" queryout "C:\tmp\' + @ProcessType + @FileName + '.xml" -T -c -t,''' | |
PRINT @SQLCommand | |
EXEC (@SQLCommand) | |
SET @First = @First + 1 | |
END | |
DROP TABLE TempProcessStore | |
EXEC sp_configure 'xp_cmdshell',0; | |
RECONFIGURE | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment