forked from ktaranov/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathusp_bcpUnloadSelect.sql
66 lines (58 loc) · 2.84 KB
/
usp_bcpUnloadSelect.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
IF OBJECT_ID('dbo.usp_bcpUnloadSelect', 'P') IS NULL EXECUTE('CREATE PROCEDURE dbo.usp_bcpUnloadSelect AS SELECT 1');
GO
ALTER PROCEDURE dbo.usp_bcpUnloadSelect(
@outputFilePath VARCHAR(255) -- The path can have from 1 through 255 characters, see documentation
, @serverName SYSNAME = @@SERVERNAME
, @sqlCommand VARCHAR(MAX)
, @fileName VARCHAR(300) = ''
, @field_term VARCHAR( 10) = '|'
, @fileExtension VARCHAR( 10) = 'txt'
, @codePage VARCHAR( 10) = 'C1251'
, @row_term VARCHAR( 10) = '\n'
, @debug BIT = 0
)
AS
/*--
Official bcp documentation: http://technet.microsoft.com/en-us/library/ms162802.aspx
In select statement use full table names: DATABASENAME.SCHEMANAME.TABLENAME
EXECUTE [NIIGAZ].[dbo].[usp_bcpUnloadSelect]
@outputFilePath = 'd:\'
, @serverName = ''
, @sqlCommand = 'SELECT * FROM DATABASENAME.SCHEMANAME.TABLENAME1 AS t1 INNER JOIN DATABASENAME.SCHEMANAME.TABLENAME2 AS t2 ON t1.Column1 = t2.Column1'
, @fileName = 'file_name.txt'
, @field_term = '|'
, @row_term = '\n'
, @fileExtension = 'txt'
, @debug = 0;
--*/
BEGIN
BEGIN TRY
DECLARE @filePath VARCHAR(2000) = @outputFilePath +
CASE WHEN @fileName = '' THEN 'bcp_export_' ELSE @fileName END +
QUOTENAME(REPLACE(CONVERT(VARCHAR, GETDATE(), 126 ), ':', '_')) +
'.' + @fileExtension;
DECLARE @tsqlCommand VARCHAR(8000) = '';
DECLARE @crlf VARCHAR(10) = CHAR(13) + CHAR(10);
IF @debug = 0 SET NOCOUNT ON ELSE PRINT '/******* Start Debug' + @crlf;
/* remove break lines from select statement */
SET @sqlCommand = REPLACE(REPLACE(@sqlCommand, CHAR(13), ' '), CHAR(10), ' ');
/* remove duplicate spaces from select statement */
SET @sqlCommand = REPLACE(REPLACE(REPLACE(@sqlCommand,' ','<>'),'><',''),'<>',' ');
IF @debug = 1
PRINT ISNULL('@filePath = {' + @crlf + @filePath + @crlf + '}', '@filePath = {Null}' + @crlf)
PRINT'@sqlCommand = {' + @crlf + @sqlCommand + @crlf + '}';
SET @tsqlCommand = 'bcp "' + REPLACE(@sqlCommand, @crlf, ' ') + '" queryout "' + @filePath +
'" -T -S ' + @serverName + ' -c -' + @codePage + ' -t"' + @field_term + '"' +
' -r"' + @row_term + '"' + @crlf;
IF @debug = 1
PRINT ISNULL( '@tsqlCommand = {' + @crlf + @tsqlCommand + '}' + @crlf, '@tsqlCommand = {Null}');
ELSE
EXECUTE xp_cmdshell @tsqlCommand;
IF @debug = 0 SET NOCOUNT OFF ELSE PRINT @crlf + '--End Deubg*********/';
END TRY
BEGIN CATCH
EXECUTE dbo.usp_LogError;
EXECUTE dbo.usp_PrintError;
END CATCH
END
go