Writing a log, dumping data, looking for file to be used in Bulk insert…
There are many situations when you would want to have access to files from inside your SQL code on Microsoft SQL Server.
Did you know that you actually can do this? No? Check below for code snippets to perform various operations on files. It is presented in form of the functions but you are actually not limited to that
Prerequisites and assumptions
- Your script should have sufficient rights to perform required access to files (not necessarily local).
- Scripting.FileSystemObject should be present at your SQL Server location and accessible.
Check if file exists
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
|
CREATE FUNCTION [dbo].[ufn_IsFileExists] (@FilePath VARCHAR(255))RETURNS INTASBEGINDECLARE @objFileSystem int, @hr int, @i intEXEC @hr = sp_OACreate 'Scripting.FileSystemObject', @objFileSystem outIF @HR = 0BEGIN EXEC sp_OAMethod @objFileSystem, 'FileExists', @i out, @FilePath EXEC sp_OADestroy @objFileSystemENDELSEBEGIN SET @i = -1ENDRETURN @iEND |
Write string into file
|
01
02
03
04
05
06
07
08
09
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
|
CREATE FUNCTION [dbo].[ufn_WriteStringToFile] ( @CreateFile int, @FilePath varchar(500), @String varchar(4000) )RETURNS varchar(200)ASBEGINDECLARE @objFileSystem int, @objTextStream int, @objErrorObject int, @strErrorMsg varchar(1000), @Command varchar(1000), @HR int, @fileAndPath varchar(80)SET @strErrorMsg = 'opening the File System Object'EXEC @hr = sp_OACreate 'Scripting.FileSystemObject' , @objFileSystem OUTIF @HR = 0BEGIN SET @objErrorObject = @objFileSystem IF (@CreateFile = 1) BEGINSET @strErrorMsg= 'Creating file "' + @FilePath + '"'EXEC @hr = sp_OAMethod @objFileSystem, 'OpenTextFile', @objTextStream OUT, @FilePath, 2, True END ELSE BEGIN SET @strErrorMsg = 'Opening file "' + @FilePath + '"' EXEC @hr = sp_OAMethod @objFileSystem, 'OpenTextFile', @objTextStream OUT, @FilePath, 8, True ENDENDIF @HR = 0BEGIN SET @objErrorObject = @objTextStream SET @strErrorMsg = 'Writing to the file "' + @FilePath + '"' EXEC @hr = sp_OAMethod @objTextStream, 'WriteLine', Null, @StringENDIF @HR=0BEGIN SET @objErrorObject = @objTextStream SET @strErrorMsg = 'Closing the file "' + @FileAndPath + '"' EXEC @hr = sp_OAMethod @objTextStream, 'Close'ENDIF @HR <> 0BEGIN DECLARE @Source varchar(255), @Description varchar(255), @Helpfile varchar(255), @HelpID intEXEC sp_OAGetErrorInfo @objErrorObject, @Source OUTPUT, @Description OUTPUT, @Helpfile OUTPUT, @HelpID OUTPUT SET @strErrorMsg = 'Error: ' + coalesce(@strErrorMsg, 'Unknown') + ', ' + coalesce(@Description, '')ENDEXEC sp_OADestroy @objTextStreamRETURN @strErrorMsgEND |
Read File As Table
|
01
02
03
04
05
06
07
08
09
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
|
CREATE FUNCTION [dbo].[ufn_ReadFileAsTable] (@FilePath VARCHAR(255))RETURNS @File TABLE ([LineNo] int identity(1,1), [Line] varchar(8000))ASBEGINDECLARE@objFileSystem int,@objTextStream int,@objErrorObject int,@strErrorMsg varchar(1000), @hr int, @String VARCHAR(8000),@YesOrNo INTSET @strErrorMsg = 'opening the File System Object'EXEC @hr = sp_OACreate 'Scripting.FileSystemObject', @objFileSystem OUTIF @HR=0BEGINSET @objErrorObject = @objFileSystemSET @strErrorMsg = 'Opening file "' + @FilePath + '"'--Open for reading, FormatASCIIEXEC @HR = sp_OAMethod @objFileSystem, 'OpenTextFile', @objTextStream OUT, @FilePath, 1, False, 0ENDWHILE @HR = 0BEGIN IF @HR=0 BEGINSET @objErrorObject = @objTextStreamSET @strErrorMsg = 'Check if there is more to read in "' + @FilePath + '"'EXEC @HR = sp_OAGetProperty @objTextStream, 'AtEndOfStream', @YesOrNo OUTPUTIF @YesOrNo <> 0 BREAK END IF @HR=0 BEGINSET @objErrorObject = @objTextStreamSET @strErrorMsg = 'Reading from the output file "' + @FilePath + '"'EXEC @HR = sp_OAMethod @objTextStream, 'Readline', @String OUTPUT INSERT INTO @file(line) SELECT @String ENDENDIF @HR=0BEGINSET @objErrorObject = @objTextStreamSET @strErrorMsg = 'Closing the output file "' + @FilePath + '"'EXEC @HR = sp_OAMethod @objTextStream, 'Close'ENDIF @hr <> 0BEGIN DECLARE@Source varchar(255),@Description varchar(255),@Helpfile varchar(255),@HelpID intEXEC sp_OAGetErrorInfo @objErrorObject, @Source OUTPUT, @Description OUTPUT, @Helpfile OUTPUT, @HelpID OUTPUTSET @strErrorMsg = 'Error: ' + coalesce(@strErrorMsg, 'Unknown') + ', ' + coalesce(@Description, '')INSERT INTO @File(line) select @strErrorMsgENDEXEC sp_OADestroy @objTextStream-- Fill the table variable with the rows for your result setRETURNEND |
Enjoy.
https://blog.dragonsoft.us/2008/02/15/sql-manage-files-from-inside-sql-code/