-
Notifications
You must be signed in to change notification settings - Fork 6
/
UTIL ExempTXT.sql
63 lines (51 loc) · 1.59 KB
/
UTIL ExempTXT.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
declare
@id_file_sist int
,@id_arqv int
,@status int
,@src varchar(255)
,@desc varchar(255)
--cria objeto
exec @status = sp_OACreate 'Scripting.FileSystemObject', @id_file_sist OUTPUT
if @status <> 0 or @@error <> 0
begin
exec sp_OAGetErrorInfo @id_file_sist, @src OUT, @desc OUT
select Source = @src, Description = @desc
goto erro
end
-- Abrir arquivo
exec @status = sp_OAMethod @id_file_sist, 'OpenTextFile', @id_arqv OUT, 'C:\teste.txt',2,1
if @status <> 0 or @@error <> 0
begin
exec sp_OAGetErrorInfo @id_file_sist ,@src OUT ,@desc OUT
select Source = @src ,Description = @desc
goto erro
end
-- Grava o xml no arquivo de saida
exec @status = sp_OAMethod @id_arqv, 'WriteLine', Null, 'seu texto'
if @status <> 0 or @@error <> 0
begin
exec sp_OAGetErrorInfo @id_file_sist, @src OUT, @desc OUT
select Source = @src, Description = @desc
goto erro
end
-- Libera o arquivo
exec @status = sp_OADestroy @id_arqv
if @status <> 0 or @@error <> 0
begin
exec sp_OAGetErrorInfo @id_file_sist, @src OUT, @desc OUT
select Source = @src, Description = @desc
goto erro
end
-- Libera o FileSystem
exec @status = sp_OADestroy @id_file_sist
if @status <> 0 or @@error <> 0
begin
exec sp_OAGetErrorInfo @id_file_sist, @src OUT, @desc OUT
select Source = @src, Description = @desc
goto erro
end
goto fim
erro:
print 'Error'
fim:
print 'arquivo criado'