-
Notifications
You must be signed in to change notification settings - Fork 84
/
Create a text file with specified contents.sql
60 lines (53 loc) · 2.87 KB
/
Create a text file with specified contents.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
-- Create a text file with specified contents
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
-- This script create stored proc usp_WriteToTextFile that uses xp_cmdshell to create a text file in any folder to which SQL Server has access.
/* ***************************************************************************** */
/* Procedure usp_WriteToTextFile - Write directly to a text file from SQL */
/* ***************************************************************************** */
/* */
/* PARAMETERS */
/* @Text VARCHAR(1000) What you want written to the output file. */
/* @File VARCHAR(255) Path and file name to which you wish to write. */
/* @Overwrite BIT = 0 Overwrite flag (0 = Append / 1 = Overwrite) */
/* */
/* RETURNS: NULL */
/* */
/* EXAMPLE */
/* */
/* EXEC dbo.usp_WriteToTextFile */
/* 'Hello World.', */
/* 'C:\Temp\logfile.txt', */
/* 0 */
/* */
/* ***************************************************************************** */
IF EXISTS (SELECT * FROM sys.sysobjects WHERE name = 'usp_WriteToTextFile')
BEGIN
DROP PROC dbo.usp_WriteToTextFile;
END;
GO
CREATE PROC dbo.usp_WriteToTextFile
@text VARCHAR(1000),
@file VARCHAR(255),
@overwrite BIT = 0
AS
BEGIN
EXEC sys.sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sys.sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
SET NOCOUNT ON;
DECLARE @query VARCHAR(255);
SET @query = 'ECHO ' + COALESCE(LTRIM(@text), '-') + CASE
WHEN (@overwrite = 1) THEN
' > '
ELSE
' >> '
END + RTRIM(@file);
EXEC master..xp_cmdshell @query;
SET NOCOUNT OFF;
EXEC sys.sp_configure 'xp_cmdshell', 0;
RECONFIGURE;
END;
GO
GRANT EXEC ON dbo.usp_WriteToTextFile TO PUBLIC;
GO