dynamic query sp_executesql

  • ---Works perfect for dynamic querys

    IF OBJECT_ID('spTraerInfoParaFormato') IS NOT NULL

    DROP PROCEDURE spTraerInfoParaFormato

    GO

    /*****************************************************************************************************

    Procedimiento:spTraerInfoParaFormato

    Creado por:Raul Bobadilla

    Fecha:11-02-2009

    Descripcion:Trae la informacion de los datos dependiendo del tipo y del identificador unico

    ******************************************************************************************************/

    create PROCEDURE [dbo].[spTraerInfoParaFormato]

    (

    @identificadornvarchar(50) = null,

    @tipoint

    )

    as

    ---------------------DECLARACION DE TABLAS TEMPORALES--------------------

    -------------------------------------------------------------------------

    DECLARE@temptabletable(rowidint identity(1, 1),

    camponvarchar(100),

    controlnvarchar(100),

    parametronvarchar(100),

    tablanvarchar(100),

    llavenvarchar(100),

    valornvarchar(500)

    )

    DECLARE@temptable1table(rowid1int identity(1, 1),

    valor1nvarchar(500)

    )

    ------------INSERCION DE VALORES EN LAS TABLAS TEMPORALES-----------------

    --------------------------------------------------------------------------

    insert into @temptable

    selectdet.campo_det_datos_gral,

    det.nombre_ctrl_det_datos_gral,

    det.parametro_det_datos_gral,

    dg.tabla_datos_gral,

    dg.pk_datos_gral,

    '' as valor

    fromt_datos_gral dg

    inner join t_det_datos_gral det on dg.id_datos_gral = det.id_datos_gral

    wheredg.tipo_datos_gral = @tipo

    declare @dynquerynvarchar(500)

    declare @camponvarchar(100)

    declare @controlnvarchar(100)

    declare @parametronvarchar(100)

    declare @tablanvarchar(100)

    declare @llavenvarchar(100)

    declare @countint

    declare @irowint

    set @count = @@rowcount

    set @irow = 1

    while @irow <= @count

    begin

    select@campo = campo,

    @control = control,

    @parametro = parametro,

    @tabla = tabla,

    @llave = llave

    from@temptable

    whererowid = @irow

    set @dynquery = 'select ' + @tabla+'.'+@campo + ' from ' + @tabla + ' where ' + @tabla +'.'+ @llave + ' = ' + 'isnull(' + @identificador + ', ' + @tabla +'.'+ @llave +')'

    --PRINT @dynquery

    insert into@temptable1

    execute sp_executesql @dynquery

    set @irow = @irow + 1

    end

    --------CURSOR PARA ACTUALIZAR EL VALOR DE LA TABLA FINAL---------

    ------------------------------------------------------------------

    declare @valorfinal as nvarchar(500)

    declare @rowidfinal as int

    declare todos cursor for

    select rowid1,valor1 from @temptable1

    open todos

    fetch next from todos into @rowidfinal, @valorfinal

    while @@fetch_status = 0

    begin

    update @temptable set valor = @valorfinal where rowid = @rowidfinal

    fetch next from todos into @rowidfinal, @valorfinal

    end

    close todos

    deallocate todos

    select campo, control, parametro, valor from @temptable

    GO

Viewing 0 posts

You must be logged in to reply to this topic. Login to reply