Take a variable value from Sql Server

Asked

Viewed 1,148 times

0

I have this Sql block:

SET NOCOUNT ON;

    Declare 
    @QueryString as NVarchar(4000), @Datazero as varchar(20),  @Datamn as varchar(20),
    @CdUsuario as integer,
    @SitProcesso as int
   ,@DsUsuarioRede varchar(20)
   ,@Processo as int
   ,@Grupo as int
   ,@Cota as smallint
   ,@DtCriacao as varchar(20)

SET @CdUsuario = (SELECT CdUsuario FROM Seguranca.TBUsuario WHERE DsUsuarioRede = @DsUsuarioRede)
SET @Processo = 46

    -- Lista os processos baseado em filtros
    Set @QueryString = N'
SELECT 
            CP.CdProcesso, 
            CP.DtCriacao, 
            (select TA.DtInicio 
               from Processo.TBAnalise TA 
              where TA.CdProcesso = CP.CdProcesso 
                and TA.CdAnalise = (select max(TBA.CdAnalise) 
                                      from Processo.TBAnalise TBA 
                                     where TBA.CdProcesso = TA.CdProcesso)) DtInicio,
            (select TA.DtFim 
               from Processo.TBAnalise TA 
              where TA.CdProcesso = CP.CdProcesso 
                and TA.CdAnalise = (select max(TBA.CdAnalise) 
                                      from Processo.TBAnalise TBA 
                                     where TBA.CdProcesso = TA.CdProcesso)) DtFim,
            CP.CdGrupo, 
            CP.CdCota, 
            ISNULL(AWF.NmWorkFlowItem, ''Processo Iniciado - PV'') NmWorkFlowItem, 
            AWF.CdWorkFlowItem, 
            DTS.NmTipoStatus, 
            WFR.CdUsuario, 
            CASE WHEN (select max(TBA.CdAnalise) 
                         from Processo.TBAnalise TBA 
                        where TBA.CdProcesso = CP.CdProcesso) IS NULL AND AWF.CdTipoUsuario != 3 THEN ''AGUARDANDO ANÁLISE'' 
                 ELSE (CASE WHEN  (select max(TBA.CdAnalise) 
                                     from Processo.TBAnalise TBA 
                                    where TBA.CdProcesso = CP.CdProcesso) IS NULL AND AWF.CdTipoUsuario = 3 THEN ''PONTO DE VENDA'' 
                 ELSE ISNULL((select TBU.NmUsuario 
                                from Processo.TBAnalise TA, 
                                     Seguranca.TBUsuario TBU 
                               where TA.CdProcesso = CP.CdProcesso   
                                 and TA.CdAnalise = (select max(TBA.CdAnalise) 
                                                       from Processo.TBAnalise TBA 
                                                      where TBA.CdProcesso = TA.CdProcesso) 
                                 and TA.CdUsuario = TBU.CdUsuario), TBUP.NmUsuario)END)END NomeUsuario,
            AWF.CdTipoUsuario,
            (select max(TBA.CdAnalise) from Processo.TBAnalise TBA where  TBA.CdProcesso = CP.CdProcesso) CdAnalise,
            (select TA.CdUsuario from Processo.TBAnalise TA where TA.CdProcesso = CP.CdProcesso and TA.CdAnalise = (select max(TBA.CdAnalise) from Processo.TBAnalise TBA where  TBA.CdProcesso = TA.CdProcesso)) CdUsuarioAn,
            CASE 
                WHEN isnull((select TA.IcPriorizado from Processo.TBAnalise TA where TA.CdProcesso = CP.CdProcesso and TA.CdAnalise = (select max(TBA.CdAnalise) from Processo.TBAnalise TBA where  TBA.CdProcesso = TA.CdProcesso)),0) = 0 THEN
                    null
                ELSE
                    ''checked''
                END IcPriorizado
        FROM 
            Processo.TBProcesso AS CP 
                INNER JOIN Dominios.TBTipoProcesso AS TP 
                    ON CP.CdTipoProcesso = TP.CdTipoProcesso 
                INNER JOIN Processo.TBWorkFlowRealizacao AS WFR 
                    ON CP.CdProcesso = WFR.CdProcesso 
                    LEFT JOIN Seguranca.TBUsuario TBUP
                        ON WFR.CdUsuario = TBUP.CdUsuario
                INNER JOIN Dominios.TBTipoStatus AS DTS 
                    ON CP.CdTipoStatus = DTS.CdTipoStatus 
                LEFT OUTER JOIN Processo.TBWorkFlowAtividade AS AWF 
                    ON WFR.CdWorkFlowItem = AWF.CdWorkFlowItem                  
        WHERE 
            WFR.DtCriacao = 
            (
                SELECT MAX(DtCriacao) 
                FROM Processo.TBWorkFlowRealizacao 
                WHERE CdProcesso = CP.CdProcesso
            )
        AND Exists((select 1 
            from ScoCons..Cota as cota, 
                 Seguranca.TBGrupoUsuario as gpusu
            where CP.CdGrupo = cota.Grupo
            and CP.CdCotaMPS = cota.Cod
            and gpusu.CdGrupo = cota.Conces
            and gpusu.CdUsuario = ' + Cast(@CdUsuario as varchar) + ')) ' 

    IF @Processo IS NOT NULL
        BEGIN
            SET @QueryString = @QueryString + ' AND CP.CdProcesso = ' + Cast(@Processo as varchar)
        END

    IF @Grupo IS NOT NULL
        BEGIN
            SET @QueryString = @QueryString + ' AND CP.CdGrupo = ' + Cast(@Grupo as varchar)

        END

    IF @Cota IS NOT NULL
        BEGIN
            SET @QueryString = @QueryString + ' AND CP.CdCota = ' + Cast(@Cota as varchar)
        END

    IF @SitProcesso IS NOT NULL
        BEGIN
            SET @QueryString = @QueryString + ' AND CP.CdTipoStatus = ' + Cast(@SitProcesso as varchar)
        END

--  IF @CdUsuario IS NOT NULL 
--      BEGIN
--          SET @QueryString = @QueryString + ' AND WFR.CdUsuario = ' + Cast(@CdUsuario as varchar)
--      END

    IF @DtCriacao IS NOT NULL
        BEGIN
            set @Datazero   = LEFT(@DtCriacao, 4) + '/' + SUBSTRING(@DtCriacao, 6, 2) + '/' + SUBSTRING(@DtCriacao, 9, 2) + ' 00:00:00' 
            set @Datamn     = LEFT(@DtCriacao, 4) + '/' + SUBSTRING(@DtCriacao, 6, 2) + '/' + SUBSTRING(@DtCriacao, 9, 2) + ' 23:59:59'
            SET @QueryString = @QueryString + ' AND CP.DtCriacao >= ''' + @Datazero + ''' AND CP.DtCriacao <= ''' + @Datamn + ''''  
        END

        SET @QueryString = @QueryString + ' ORDER BY CP.CdProcesso'

        --EXEC sp_executesql @QueryString 
        print @QueryString

How do I get the value of @Querystring at the time of execution? I gave one print as you can see, but there’s nothing on the way out.

3 answers

1


Hi, try to select your variable:

select  @QueryString  

instead of print, :)

  • if I give a select, it will bring me the result and not what is in querystring, the string that the variable generated that must be of type: select... order by CP.CdProcesson. What I want is the string generated for me to see what is wrong, because null is coming when there is data for the past tense.

  • I mean you were right. Since I had not set the Dsusuarionet variable, here was coming NULL. I set the same and managed to get the content with select querystring.

  • vote one up then :)

  • @pnet If to return as 'Querystring' was generated you can mark is as response.

  • I marked as response and gave an upvote on the reply and will pa -1 and not to 1. Strange this, no?

  • thank you very much!!

Show 1 more comment

1

The query being saved in the @Querystring variable exceeds the size. You need to declare the variable to varchar(5000).

Declare 
    @QueryString as varchar(5000)

1

So I understand you want the result of your dynamically generated Query.

If so, try instead of print.

exec(@QueryString)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.