Last active
August 29, 2015 13:56
-
-
Save luiscelismx/9273143 to your computer and use it in GitHub Desktop.
Procedimiento almacenado en SQL para procesar archivos EDI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| USE [MYDB] | |
| GO | |
| SET ANSI_NULLS ON | |
| GO | |
| SET QUOTED_IDENTIFIER ON | |
| GO | |
| -- ============================================= | |
| -- Author: Luis Antonio Celis Molina | |
| -- Create date: 20/06/2013 | |
| -- Description: Procedimiento para procesar el archivo EDI correspondiente a las ordenes de compra, ordenes de pago y controles | |
| -- | |
| -- ============================================= | |
| CREATE PROCEDURE [dbo].[Procesar_EDIs] | |
| AS | |
| BEGIN | |
| -- SET NOCOUNT ON added to prevent extra result sets from | |
| -- interfering with SELECT statements. | |
| SET NOCOUNT ON; | |
| --- Declarar variables EDI generales a utilizar | |
| DECLARE @return_value int, @archivo varchar(5000), @files_Id char( 11 ), @ruta varchar(3000), @comando_Obtener_Listado varchar(3000), | |
| @nombre_Archivo varchar(3000), @os_File_Name varchar(max), @puntero_Inicio int, @puntero_Fin int, @tipo_Documento varchar(20), | |
| @text_File varchar(max), @puntero_Final int | |
| -- Declarar tabla temporal que contendra la lista de archivos que hay en la carpeta de recibidas | |
| DECLARE @files table (ID int IDENTITY, FileName varchar(100)) | |
| -- Definir ruta de acuerdo a la carpeta donde se encuntran los archivos .edi a procesar | |
| SELECT @ruta = 'U:\Incoming\' | |
| SELECT @comando_Obtener_Listado = 'dir '+@ruta+' /b' | |
| -- Obtener el listado de archivos de la ruta C:\EDI\recibidas\ | |
| INSERT INTO @files EXECUTE xp_cmdshell @comando_Obtener_Listado | |
| --ps SELECT * FROM @files | |
| /* Obtener la información del primer archivo de tipo .edi */ | |
| SELECT @files_Id = min( ID ) FROM @files WHERE FileName LIKE '%.edi' | |
| /* Bucle que recorre el listado de archivos */ | |
| WHILE @files_Id is not null | |
| BEGIN | |
| --ps select @files_Id Archivo | |
| SELECT * from @files WHERE ID = @files_Id | |
| /* Obtener el nombre del archivo .edi a procesar */ | |
| SELECT @nombre_Archivo = FileName FROM @files WHERE ID = @files_Id ; | |
| -- Leer el archivo | |
| -- Establecer nombre y ruta del archivo | |
| SELECT @text_File = @nombre_Archivo | |
| SELECT @os_File_Name = @ruta+@nombre_Archivo | |
| --Ejecutar Procedimiento almacenado | |
| EXEC @return_value = [master].[dbo].[ns_txt_file_read] | |
| @os_File_Name = @os_File_Name, | |
| @text_File = @text_File OUTPUT | |
| --ps SELECT @text_File as N'edi' | |
| /* Iniciar puntero */ | |
| SET @puntero_Inicio = 1 | |
| /* Obtener el tipo de documento del que se trata */ | |
| SET @puntero_Inicio = CHARINDEX('UNH', @text_File, @puntero_Inicio); | |
| SET @puntero_Fin = CHARINDEX(':', @text_File,@puntero_Inicio+6); | |
| SET @tipo_Documento = SUBSTRING(@text_File, @puntero_Inicio + 6,@puntero_Fin - (@puntero_Inicio + 6)); | |
| /* Bucle para recorrer todo el archivo, cuando el valor del puntero no encuentre el segmento BGM | |
| significa que ya no hay otra orden de compra en el archivo */ | |
| /* Procesar orden de Compra */ | |
| IF(@tipo_Documento = 'ORDERS') | |
| BEGIN | |
| WHILE @puntero_Inicio <> 0 | |
| BEGIN | |
| EXEC @return_value = [master].[dbo].[Procesar_Orders_EDI] | |
| @text_File = @text_File, @puntero_Inicio = @puntero_Inicio, @puntero_Final = @puntero_Final OUTPUT | |
| SET @puntero_Fin = @puntero_Final | |
| /* Recorremos el puntero a la siguiente orden de compra */ | |
| SET @puntero_Inicio = CHARINDEX('BGM', @text_File, @puntero_Fin); | |
| SET @puntero_Fin = CHARINDEX('UNT', @text_File, @puntero_Inicio); | |
| END | |
| END | |
| /* Procesar Pago */ | |
| IF(@tipo_Documento = 'REMADV') | |
| BEGIN | |
| EXEC @return_value = [master].[dbo].[Procesar_Remadv_EDI] | |
| @text_File = @text_File | |
| END | |
| /* Procesar Contrl */ | |
| IF(@tipo_Documento = 'CONTRL') | |
| BEGIN | |
| EXEC @return_value = [master].[dbo].[Procesar_Contrl_EDI] | |
| @text_File = @text_File | |
| END | |
| /* Mover el archivo a la carpeta de procesados*/ | |
| SET @archivo = ('move U:\Incoming\'+@nombre_Archivo+' U:\Procesados\'); | |
| SELECT @archivo archivo | |
| EXEC master..xp_CMDShell @archivo; | |
| /* Cambiar de fila, moverse al siguiente archivo*/ | |
| SELECT @files_Id = min( ID ) FROM @files WHERE ID > @files_Id AND FileName is not null | |
| END | |
| END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment