forked from ktaranov/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbo.usp_bcpTableUnloadBatchMode.sql
63 lines (52 loc) · 3.47 KB
/
dbo.usp_bcpTableUnloadBatchMode.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
IF OBJECT_ID('dbo.usp_bcpTableUnloadBatchMode', 'P') IS NULL EXECUTE ('CREATE PROCEDURE dbo.usp_bcpTableUnloadBatchMode as select 1');
go
ALTER PROCEDURE dbo.usp_bcpTableUnloadBatchMode (
@path NVARCHAR(900),
@databaseName SYSNAME,
@includeSchemaNames NVARCHAR(MAX) = '',
@excludeSchemaNames NVARCHAR(MAX) = '',
@includeTableNames NVARCHAR(MAX) = '',
@excludeTableNames NVARCHAR(MAX) = '',
@fieldTerminator NVARCHAR(10) = '|',
@fileExtension NVARCHAR(10) = 'txt',
@codePage NVARCHAR(10) = 'C1251',
@excludeColumns NVARCHAR(MAX) = '''''',
@outputColumnHeaders BIT = 1,
@minRowCount INTEGER = 1,
@debug BIT = 0
)
AS
/*
EXECUTE [dbo].[usp_bcpTableUnloadBatchMode] @path = 'd:\',
@databaseName = 'DatabaseName',
@excludeSchemaNames = 'dbo',
@tableName = 'TableName',
@fieldTerminator = '|',
@fileExtension = 'txt',
@excludeColumns = '[CreatedDate],[ModifiedDate],[UserID]',
@outputColumnHeaders = 1,
@minRowCount = 1,
@debug = 0;
*/
BEGIN
IF @debug = 0 SET NOCOUNT ON;
IF @debug = 1 PRINT '/******* Start Debug';
DECLARE @tsqlCommand NVARCHAR(MAX) = '';
DECLARE @tableNames NVARCHAR(MAX) = '';
DECLARE @crlf NVARCHAR(10) = CHAR(13);
SELECT @tableNames = @tableNames + QUOTENAME(@databaseName) + '.' + QUOTENAME(ist.TABLE_SCHEMA) + '.' + QUOTENAME(ist.TABLE_NAME) + ','
FROM SYS.SYSINDEXES AS sind WITH (NOLOCK)
INNER JOIN INFORMATION_SCHEMA.TABLES AS ist ON OBJECT_NAME(sind.id) = ist.TABLE_NAME
WHERE sind.indid IN (0, 1)
AND sind.rowcnt > @minRowCount
AND QUOTENAME(ist.TABLE_NAME) NOT IN ('''' + REPLACE(@excludeTableNames, ',', ''',''') + '''')
AND QUOTENAME(ist.TABLE_SCHEMA) NOT IN ('''' + REPLACE(@excludeSchemaNames, ',', ''',''') + '''')
AND ist.TABLE_NAME NOT LIKE '%tmp%'
;
IF @debug = 1 PRINT CAST(ISNULL('@excludeTableNames = {' + @crlf + '''' + REPLACE(@excludeTableNames, ',', ''',''') + '''' + @crlf + '}', '@tableNames = {Null}' + @crlf) AS TEXT);
IF @debug = 1 PRINT CAST(ISNULL('@tableNames = {' + @crlf + @tableNames + @crlf + '}', '@tableNames = {Null}' + @crlf) AS TEXT);
SET @tsqlCommand = '';
IF @debug = 1 PRINT CAST(ISNULL('@tsqlCommand = {' + @crlf + @tsqlCommand + @crlf + '}', '@tsqlCommand = {Null}' + @crlf) AS TEXT);
IF @debug = 1 PRINT '--End Deubg*********/'
IF @debug = 0 SET NOCOUNT OFF;
END