forked from MRGhidini/SAP_Queries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPROC ProcedureTXT.sql
133 lines (119 loc) · 3.5 KB
/
PROC ProcedureTXT.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
USE [SBO_NDJ_20140331]
GO
/****** Object: StoredProcedure [dbo].[ga_TP_CriaTexto] Script Date: 04/25/2014 14:36:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[ga_TP_CriaTexto]
@Table as varchar(100),
@DataIni DateTime = null,
@DataFim as DateTime = null
as
Declare @Colunas as nvarchar(max),
@Linhas as nvarchar(max),
@ColName as varchar(200),
@Where as varchar(255),
@id_file_sist int,
@id_arqv int,
@status int,
@src varchar(255),
@desc varchar(255),
@Arq as varchar(255),
@msg as varchar(30),
@Code as int
--Exec ga_TP_CriaTexto GA_TP_MESTRE_NFEN_MERC, '20130120', '20130921'
set @Where = ' Where '
Set @Colunas = 'SELECT '
set @Code = (ISNULL((Select MAX(cast(code as int))+1 From [@GATPLOG]),1))
if @Table not in (select T0.Name From sys.views T0)
Begin
set @src = ''
set @desc = 'View Não existe'
set @msg = 'Erro'
goto Fim
End
set @Arq = 'C:\'+@Table+replace(replace(replace(convert(varchar,GETDATE(),120),'-',''),' ',''),':','')+'.txt'
/*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
set @msg = 'Erro'
goto Fim
end
/* Abrir arquivo*/
Exec @status = sp_OAMethod @id_file_sist, 'OpenTextFile', @id_arqv OUT, @Arq, 2, 1
if @status <> 0 or @@error <> 0
begin
exec sp_OAGetErrorInfo @id_file_sist ,@src OUT ,@desc OUT
set @msg = 'Erro'
goto Fim
end
declare xCur1 Cursor for
select T1.Name from sys.views T0 inner join sys.columns T1 on T0.object_id = T1.object_id
where T0.name = @Table And T1.Name <> 'gaFiltro'
Open xCur1
Fetch Next From xCur1 into @ColName
While @@FETCH_STATUS = 0
Begin
Set @Colunas = @Colunas + '['+ @ColName+']+'
Fetch Next From xCur1 into @ColName
End
Close xCur1
Deallocate xCur1
Set @Colunas = LEFT(@colunas, Len(@Colunas) -1) + ' From ' + @Table
if @DataIni is not null
Begin
set @Where = @Where + ' gaFiltro >= ' + char(39)+replace(CONVERT(varchar,@DataIni,102),'.','')+CHAR(39)
End
if @DataFim is not null
Begin
if LEN(@Where) > 10 begin set @Where = @Where+' And ' End
set @Where = @Where + ' gaFiltro <= '+ char(39)+replace(CONVERT(varchar,@DataFim,102),'.','')+CHAR(39)
End
if @DataFim is not null or @DataIni is not null
Begin
set @Colunas = @Colunas + @Where
End
/*
--Liberar esta parte do código para verificar a query que sera executada
print @Colunas
set @msg = 'Erro'
goto Fim
*/
/*Grava o resultado no arquivo de saida*/
exec('Declare xCur2 cursor for '+@Colunas)
Open xCur2
Fetch Next From xCur2 into @Linhas
While @@FETCH_STATUS = 0
Begin
exec @status = sp_OAMethod @id_arqv, 'WriteLine', Null, @Linhas
if @status <> 0 or @@error <> 0
begin
exec sp_OAGetErrorInfo @id_file_sist, @src OUT, @desc OUT
set @msg = 'Erro'
goto Fim
end
Fetch Next From xCur2 into @Linhas
End
Close xCur2
Deallocate xCur2
/*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
set @msg = 'Erro'
goto Fim
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
set @msg = 'Erro'
end
Set @msg = 'Arquivo Criado'
Fim:
insert into [@GATPLOG] values(@code,@code,@msg, @Table, isnull(@desc, ERROR_MESSAGE()), @Arq)