Skip to content

Instantly share code, notes, and snippets.

@luiscelismx
Last active August 18, 2021 14:56
Show Gist options
  • Select an option

  • Save luiscelismx/9272472 to your computer and use it in GitHub Desktop.

Select an option

Save luiscelismx/9272472 to your computer and use it in GitHub Desktop.
Procedimiento almacenado en SQL para leer un archivo y almacenarlo en una variable para su procesamiento.
USE [MYDB]
GO
/****** Object: StoredProcedure [dbo].[ns_txt_file_read] Script Date: 02/28/2013 08:35:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[ns_txt_file_read]
@nombre_archivo_so NVARCHAR(256)
,@archivo_texto VARCHAR(MAX) OUTPUT
-- =============================================
-- Author: Luis Celis
-- Create date: 17/Junio/2013
-- Description: Procedimiento creado para Leer un archivo de texto y almacenarlo en @archivo_texto
--
-- Transacciones: pudiera estar en una transacción, sin embargo, no es afectado por la transacción.
-- Manejo de Errores: Los errores no son atrapados y son lanzados a la llamada.
-- Ejemplo:
-- declare @t varchar(max)
-- exec ns_txt_file_read 'c:\carpeta\ArchivoDeTexto.txt', @t output
-- select @t as [ArchivoDeTexto.txt]
-- =============================================
AS
DECLARE @sql NVARCHAR(MAX)
, @parmsdeclare NVARCHAR(4000)
SET NOCOUNT ON
SET @sql = 'select @archivo_texto=(select * from openrowset (
bulk ''' + @nombre_archivo_so + '''
,SINGLE_CLOB) x
)'
SET @parmsdeclare = '@archivo_texto varchar(max) OUTPUT'
EXEC sp_executesql @stmt = @sql
, @params = @parmsdeclare
, @archivo_texto = @archivo_texto OUTPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment