Oracle - Migrar SP de SQL a Oracle

   
Vista:

Migrar SP de SQL a Oracle

Publicado por David Jaso (1 intervención) el 18/03/2015 20:31:53
Buen dia,

Necesito migrar un procedimiento almacendadode SQL a Oracle, no conozco mucho de Oracle esperoy me puedan ayudar,

Este es el scrip:

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
CREATE PROCEDURE [dbo].[USP_PV_Generar_GuiasDesp_de_Desp]
 
(
    @KeyCom VARCHAR(6),   @KeyEnv VARCHAR(6),   @TieIni VARCHAR(6),    @TieFin VARCHAR(6),      @KeyEmp VARCHAR(10),
    @Generar VARCHAR(2),  @TipPrd VARCHAR(2),   @sFecIni VARCHAR(10),  @sFecFin VARCHAR(10),  @TipPed VARCHAR(2),
    @KeyBod  integer,     @DocIni varchar(15),  @DocFin  varchar(15)
)
AS
 
BEGIN
 
  SET NOCOUNT ON
 
  DECLARE @FolGui numeric,        @KeyRef varchar(25),    @KeyOrd varchar(20),    @OrdenC varchar(20),    @keyprd  varchar(15),
          @PrdAnt varchar(15),    @keytal varchar(3),        @Keytie varchar(6),        @TieEnv varchar(6),        @cantid  numeric,
          @KeyUsu varchar(10),    @Usuario varchar(10),    @User   varchar(10),    @FolFin numeric,        @Increm  numeric,
          @IdxInv numeric,        @FecHoy DateTime,        @NoGuia varchar(25),    @Transp varchar(40),    @OrdRef  varchar(20),
          @OrdAnt varchar(20),    @GuiIni numeric,        @GuiFin numeric,        @Row numeric,            @Sts     varchar(1),
          @Prim   varchar(1),    @Refere varchar(20),    @Desc   varchar(5),        @tier varchar(6),        @tienv   varchar(6),
          @ord    varchar(20),    @ORDEN varchar(20),     @FecOrd varchar(10),    @TipoP VARCHAR(2),      @ReferOC varchar(20),
          @ValidaAparatado int
 
  SET @FecHoy = dbo.GetFecha(getdate())
  SET @User   = SUBSTRING(@KeyEmp,1,10)
  SET @KeyUsu = SUBSTRING(@KeyEmp,1,10)
  SET @KeyEmp = SUBSTRING(@KeyEmp,1,6)
 
  DELETE CA_REPORTE_TALLAS WITH (ROWLOCK)
   WHERE RPT_KEYUSU = @KeyEmp
     AND RPT_KEYCOM = @KeyCom
 
    DECLARE cTdaXRgo CURSOR
    FOR
          SELECT GUI_KEYTIE, GUI_TIEENV
             FROM PV_GUIAS_X_GENERAR WITH (NOLOCK)        -- 1
                  INNER JOIN PV_PRODUCTOS WITH (NOLOCK)
                       ON PRD_KEYCOM = GUI_KEYCOM
                      AND PRD_KEYPRD = GUI_KEYPRD
            WHERE GUI_KEYCOM = @KeyCom
           AND GUI_TIEENV = @KeyEnv
           AND SUBSTRING(GUI_REFERE,1,8) = 'DESPACHO'
           AND CONVERT(NUMERIC,GUI_KEYTIE) BETWEEN CONVERT(NUMERIC,@TieIni) AND CONVERT(NUMERIC,@TieFin)
           AND CONVERT(DATE,GUI_FECSYS) BETWEEN @sFecIni AND @sFecFin
           AND ISNULL(GUI_NUMFAC,'') BETWEEN @DocIni AND @DocFin
           AND PRD_KEYTIP = ( CASE WHEN @TipPrd = 'AM' THEN PRD_KEYTIP ELSE @TipPrd END )
        GROUP BY GUI_KEYTIE, GUI_TIEENV
        ORDER BY GUI_KEYTIE, GUI_TIEENV
    OPEN cTdaXRgo
    FETCH NEXT FROM cTdaXRgo INTO @Keytie, @TieEnv
    WHILE @@FETCH_STATUS = 0
    BEGIN
      IF NOT EXISTS( SELECT FOL_FOLIO FROM PV_FOLIOS_USUARIO WITH (NOLOCK)
                         WHERE FOL_KEYCOM = @KeyCom
                           AND FOL_KEYTIE = @TieEnv
                           AND FOL_KEYFOL = 'DE'
                           AND FOL_KEYUSU = 'ADMIN')
         SET @KeyUsu = 'ADMIN'
 
      SELECT @FolGui = ( CASE WHEN FOL_FOLIO IS NULL THEN 0 ELSE FOL_FOLIO END ) + 1,
             @FolFin = FOL_FOLFIN, @Increm = FOL_INCREM
        FROM PV_FOLIOS_USUARIO WITH (NOLOCK)
       WHERE FOL_KEYCOM = @KeyCom
         AND FOL_KEYTIE = @TieEnv
         AND FOL_KEYFOL = 'DE'
         AND FOL_KEYUSU = 'ADMIN'
 
      IF (@FolGui + 1 ) > @FolFin
         SET @FolGui = ((@FolFin + @Increm) - 1999) + 1
 
      SET @NoGuia = '0'
      SELECT @Transp = (CASE WHEN TRA_DESTRA IS NULL THEN 'N/A' ELSE TRA_DESTRA END)
        FROM PV_TIENDAS WITH (NOLOCK), PV_TRANSPORTES WITH (NOLOCK)
       WHERE TIE_KEYCOM = @KeyCom
         AND TIE_KEYTIE = @KeyTie
         AND TIE_KEYTRA = TRA_KEYTRA
 
 
      IF @KeyCom='004'
      BEGIN
         IF @KeyEnv='099'
            BEGIN
              IF LTRIM(RTRIM(@Refere)) = 'DESPACHO'
                 BEGIN
                    SET @Sts = 'C'
                    SET @Desc = 'DES: '
                 END
              ELSE
                 BEGIN
                    SET @Sts = 'C'
                    SET @Desc = 'OC : '
                 END
            END
 
            IF @KeyEnv<>'099'
               BEGIN
                  IF LTRIM(RTRIM(@Refere)) = 'DESPACHO'
                     BEGIN
                        SET @Sts = 'X'
                        SET @Desc = 'DES: '
                     END
                  ELSE
                     BEGIN
                        SET @Sts = 'X'
                        SET @Desc = 'OC : '
                     END
               END
            END
 
      IF @KeyCom='002'
      BEGIN
         IF LTRIM(RTRIM(@Refere)) = 'DESPACHO'
            BEGIN
               SET @Sts = 'X'
               SET @Desc = 'DES: '
            END
         ELSE
            BEGIN
               SET @Sts = 'X'
               SET @Desc = 'OC : '
            END
      END
 
      IF @KeyCom='001'
      BEGIN
         IF LTRIM(RTRIM(@Refere)) = 'DESPACHO'
            BEGIN
               SET @Sts = 'X'
               SET @Desc = 'DES: '
            END
         ELSE
            BEGIN
               SET @Sts = 'X'
               SET @Desc = 'OC : '
            END
         END
 
      INSERT INTO PV_MCIA_CAMINO WITH (ROWLOCK)
             ( MCE_KEYFOL, MCE_KEYCOM, MCE_KEYTIE, MCE_KEYTIR, MCE_TIPMOV, MCE_FECMOV, MCE_NOGUIA,
               MCE_TRANSP, MCE_KEYEMP, MCE_STATUS, MCE_ACTUAL, MCE_FECACT, MCE_REFERE )
      VALUES ( @FolGui,    @KeyCom,    @TieEnv,    @KeyTie,    'DE',       GETDATE(),  @NoGuia,
               @Transp,    @KeyEmp,    @Sts,       'N',        GETDATE(),  @Desc )
 
      INSERT INTO CA_REPORTE_TALLAS WITH (ROWLOCK)
             ( RPT_KEYCOM, RPT_KEYTIE, RPT_KEYTIR, RPT_OBSER1, RPT_OBSER2, RPT_KEYUSU )
      VALUES ( @KeyCom,    @TieEnv,    @KeyTie,    @FolGui,    @Desc,      @KeyEmp )
 
      SET @OrdRef = @Desc
      SET @OrdAnt = '@@@'
      SET @PrdAnt = '@@@'
      SET @Row = 0
 
      DECLARE cGuias CURSOR
      FOR
      -- Variables del Cursor principal: @ORDEN, @Keytie, @TieEnv, @FecOrd
      SELECT GUI_KEYTIE, GUI_TIEENV, GUI_KEYPRD, GUI_KEYTAL, SUM(GUI_CANTID) GUI_CANTID
        FROM PV_GUIAS_X_GENERAR WITH (NOLOCK)        -- 3
             INNER JOIN PV_PRODUCTOS WITH (NOLOCK) ON PRD_KEYCOM = GUI_KEYCOM
                                     AND PRD_KEYPRD = GUI_KEYPRD
       WHERE GUI_KEYCOM = @KeyCom
         AND GUI_KEYTIE = @KeyTie
         AND GUI_TIEENV = @TieEnv
         AND SUBSTRING(GUI_REFERE,1,8) = 'DESPACHO'
         AND CONVERT(DATE,GUI_FECSYS) BETWEEN @sFecIni AND @sFecFin
         AND ISNULL(GUI_NUMFAC,'') BETWEEN @DocIni AND @DocFin
         AND PRD_KEYTIP = ( CASE WHEN @TipPrd = 'AM' THEN PRD_KEYTIP ELSE @TipPrd END )
      GROUP BY GUI_KEYTIE, GUI_TIEENV, GUI_KEYPRD, GUI_KEYTAL
      ORDER BY GUI_KEYTIE, GUI_TIEENV, GUI_KEYPRD, GUI_KEYTAL
 
      OPEN cGuias
      FETCH NEXT FROM cGuias INTO @tier, @tienv, @keyprd, @keytal, @cantid
      WHILE @@FETCH_STATUS = 0
      BEGIN
        IF @Row > 12
            BEGIN
               SET @OrdRef = @Desc
               SET @OrdAnt = '@@@'
               SET @PrdAnt = '@@@'
               SET @Row    = 0
               SET @FolGui = @FolGui + 1
               INSERT INTO PV_MCIA_CAMINO WITH (ROWLOCK)
                      ( MCE_KEYFOL, MCE_KEYCOM, MCE_KEYTIE, MCE_KEYTIR, MCE_TIPMOV, MCE_FECMOV, MCE_NOGUIA,
                        MCE_TRANSP, MCE_KEYEMP, MCE_STATUS, MCE_ACTUAL, MCE_FECACT, MCE_REFERE )
               VALUES ( @FolGui,    @KeyCom,    @TieEnv,    @KeyTie,    'DE',       GETDATE(),  @NoGuia,
                        @Transp,    @KeyEmp,    @Sts,       'N',        GETDATE(),  @Desc )
 
               INSERT INTO CA_REPORTE_TALLAS WITH (ROWLOCK)
                      ( RPT_KEYCOM, RPT_KEYTIE , RPT_KEYTIR, RPT_OBSER1, RPT_OBSER2, RPT_KEYUSU )
               VALUES ( @KeyCom,    @TieEnv,     @KeyTie,    @FolGui,    @Desc,      @KeyEmp )
           END
 
            SET @ValidaAparatado = 0
            SELECT @ValidaAparatado = isnull((INV_CANAPA - @CANTID),-1)
              FROM PV_INVENTARIOS with (nolock)
             WHERE INV_KEYCOM = @keycom
               AND INV_KEYTIE = @TIENV
               AND INV_KEYPRD = @KEYPRD
               AND INV_KEYTAL = @keytal
 
            IF @ValidaAparatado >= 0
            begin
               SET @TipoP = ''
               -- Llave: MCD_KEYFOL, MCD_KEYCOM, MCD_KEYTIE, MCD_KEYTIR, MCD_TIPMOV, MCD_KEYPRD, MCD_KEYTAL
               INSERT INTO PV_DETALLE_MCIA_CAMINO WITH (ROWLOCK)
                      ( MCD_KEYFOL, MCD_KEYCOM, MCD_KEYTIE, MCD_KEYTIR, MCD_TIPMOV, MCD_KEYPRD, MCD_KEYTAL, MCD_CANTID,
                        MCD_STATUS, MCD_ACTUAL, MCD_FECACT, MCD_TIPPED, MCD_KEYBOD, MCD_REFEOC )
               VALUES ( @FolGui,    @KeyCom,    @tienv,     @tier,      'DE',       @keyprd,    @keytal,    @Cantid,
                        @Sts,       'N',       GETDATE(),   @TipoP,     @KeyBod,    'DPCHO'  )
 
                BEGIN TRANSACTION
                    SELECT @IdxInv = FOL_FOLIO +1 FROM PV_FOLIOS
                     WHERE FOL_KEYCOM = @KeyCom
                       AND FOL_KEYTIE = @TIEENV
                       AND FOL_KEYFOL = 'IV'
 
                UPDATE PV_FOLIOS WITH (ROWLOCK)
                   SET FOL_FOLIO = @IdxInv
                 WHERE FOL_KEYCOM = @KeyCom
                   AND FOL_KEYTIE = @TieEnv
                   AND FOL_KEYFOL = 'IV'
                COMMIT TRANSACTION
 
                UPDATE PV_INVENTARIOS WITH (ROWLOCK)
                   SET INV_CANAPA = INV_CANAPA - @Cantid
                 WHERE INV_KEYCOM = @keycom
                   AND INV_KEYTIE = @tienv
                   AND INV_KEYPRD = @keyprd
                   AND INV_KEYTAL = @keytal
 
                INSERT INTO PV_MOVTOS_INVENTARIO WITH (ROWLOCK)
                       ( MVI_KEYCOM, MVI_KEYTIE, MVI_KEYCAJ, MVI_TIPMOV,     MVI_CVEMOV, MVI_FECMOV, MVI_KEYPRD, MVI_KEYTAL,
                         MVI_CANTID, MVI_PRECIO, MVI_COSTO,  MVI_REFERE,     MVI_KEYTCK, MVI_ACTUAL, MVI_INDEX )
                VALUES ( @KeyCom,    @tienv,     '001',      'S',            'GG',       GETDATE(),  @KeyPrd,    @KeyTal,
                         @Cantid,    0,          0,           'GENERA GUIAS', @FolGui,    'N',        @IdxInv     )
            end
 
        IF @PrdAnt <>  @keyprd
           SET @Row = @Row + 1
 
        IF @Row > 1200
           BEGIN
               IF LEN(@OrdRef) > 20
                    SET @OrdRef = SUBSTRING(@OrdRef,1,20)
               ELSE
                    SET @OrdRef = SUBSTRING(@OrdRef,1,LEN(@OrdRef)-1)
 
               UPDATE PV_MCIA_CAMINO WITH (ROWLOCK)
                  SET MCE_REFERE = @ORDREF
                WHERE MCE_KEYFOL = @FolGui
                  AND MCE_KEYCOM = @KeyCom
                  AND MCE_KEYTIE = @TieEnv
                  AND MCE_KEYTIR = @KeyTie
                  AND MCE_TIPMOV = 'DE'
                  AND (MCE_STATUS = 'C' OR MCE_STATUS = 'X')
           END
 
        SET @PrdAnt = @keyprd
        FETCH NEXT FROM cGuias INTO @tier, @tienv, @keyprd, @keytal, @cantid
      END
 
      CLOSE cGuias
      DEALLOCATE cGuias
 
      IF @@ERROR = 0
      BEGIN
          IF LEN(@OrdRef) > 20
             SET @OrdRef = SUBSTRING(@OrdRef,1,20)
          ELSE
             SET @OrdRef = SUBSTRING(@OrdRef,1,LEN(@OrdRef)-1)
 
          UPDATE PV_MCIA_CAMINO WITH (ROWLOCK)
                 SET MCE_REFERE = @OrdRef
               WHERE MCE_KEYFOL = @FolGui
                 AND MCE_KEYCOM = @KeyCom
                 AND MCE_KEYTIE = @TieEnv
                 AND MCE_KEYTIR = @KeyTie
                 AND MCE_TIPMOV = 'DE'
                 AND (MCE_STATUS = 'C' OR MCE_STATUS='X')
 
          IF @@ERROR = 0
          BEGIN
            DELETE PV_GUIAS_X_GENERAR WITH (ROWLOCK)
              FROM PV_GUIAS_X_GENERAR g
                   INNER JOIN PV_PRODUCTOS ON PRD_KEYCOM = g.GUI_KEYCOM
                                          AND PRD_KEYPRD = g.GUI_KEYPRD
             WHERE GUI_KEYCOM = @KeyCom
               AND GUI_TIEENV = @TieEnv
               AND GUI_KEYTIE = @KeyTie
               AND SUBSTRING(GUI_REFERE,1,8) = 'DESPACHO'
               AND CONVERT(DATE,GUI_FECSYS) BETWEEN @sFecIni AND @sFecFin
               AND ISNULL(GUI_NUMFAC,'') BETWEEN @DocIni AND @DocFin
               AND PRD_KEYTIP = ( CASE WHEN @TipPrd = 'AM' THEN PRD_KEYTIP ELSE @TipPrd END )
 
            IF @@ERROR = 0
            BEGIN
                 --UPDATE PV_FOLIOS WITH (ROWLOCK)
                    --SET FOL_FOLIO = @IdxInv
                 -- WHERE FOL_KEYCOM = @KeyCom
                    --AND FOL_KEYTIE = @TieEnv
                    --AND FOL_KEYFOL = 'IV'
                IF @@ERROR = 0
                BEGIN
                  IF @FolGui = 1
                     BEGIN
                        IF NOT EXISTS( SELECT FOL_FOLIO FROM PV_FOLIOS_USUARIO WITH (NOLOCK)
                                        WHERE FOL_KEYCOM = @KeyCom
                                          AND FOL_KEYTIE = @TieEnv
                                          AND FOL_KEYFOL = 'DE'
                                          AND FOL_KEYUSU = 'ADMIN')
                           BEGIN
                              INSERT INTO PV_FOLIOS_USUARIO WITH (ROWLOCK)
                                     ( FOL_KEYCOM, FOL_KEYTIE, FOL_KEYFOL, FOL_KEYUSU, FOL_FOLIO,  FOL_FOLINI, FOL_FOLFIN, FOL_INCREM )
                              VALUES ( @KeyCom,    @TieEnv,    'DE',       @KeyUsu,    @FolGui,    1,          2000,       10000)
                            END
                        ELSE
                            BEGIN
                               UPDATE PV_FOLIOS_USUARIO WITH (ROWLOCK)
                                  SET FOL_FOLIO  = @FolGui
                                WHERE FOL_KEYCOM = @KeyCom
                                  AND FOL_KEYTIE = @TieEnv
                                  AND FOL_KEYFOL = 'DE'
                                  AND FOL_KEYUSU = 'ADMIN'
                            END
                     END
                  ELSE
                     BEGIN
                        UPDATE PV_FOLIOS_USUARIO WITH (ROWLOCK)
                           SET FOL_FOLIO  = @FolGui
                         WHERE FOL_KEYCOM = @KeyCom
                           AND FOL_KEYTIE = @TieEnv
                           AND FOL_KEYFOL = 'DE'
                           AND FOL_KEYUSU = 'ADMIN'
                     END
                END
            END
          END
      END
 
      FETCH NEXT FROM cTdaXRgo INTO @Keytie, @TieEnv
    END
    CLOSE cTdaXRgo
    DEALLOCATE cTdaXRgo
END
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Migrar SP de SQL a Oracle

Publicado por luis (232 intervenciones) el 19/03/2015 15:04:57
Bueno, tienes que desarrollarlo en oracle usando las estructuras de control de PL/SQL, incluir el tratamiento de excepciones en los bloques PL/SQL y a nivel de procedimiento de BD.

Saludos

Luis
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar