Skip to main content
removeu 4987 caracteres do conteúdo
Fonte Link
Rafael Barbosa
  • 2,8mil
  • 1
  • 17
  • 37

Minha procedure

ALTER PROCEDURE [dbo].[SEL_DESP_ExportaArquivoSAP_reembolsavel] 
(   @Codigo VARCHAR(MAX)  ,  @cont as int   , @DataPago as VARCHAR(10) )
AS         
BEGIN               
   
  CREATE TABLE #temp
    (REFERENCIA NVARCHAR(max)
    , CHAVE    NVARCHAR(max)
    , CONTA   NVARCHAR(max)
    , MONTANTE NVARCHAR(max)
    , MOEDA NVARCHAR(max)
    , TEXTO NVARCHAR(max)   
    , DATA_PAGAMENTO NVARCHAR(max)  
    , CENTRO_CUSTO NVARCHAR(max));
    
  IF ( @cont = 1)
  BEGIN
    INSERT INTO #temp
        SELECT 
              'REFERENCIA'  as   REFERENCIA
            , 'CHAVE' as CHAVE    
            , 'CONTA' as CONTA   
            , 'MONTANTE'  AS  MONTANTE
            , 'MOEDA'    AS MOEDA
            , 'TEXTO' as TEXTO
            , 'DATA PAGAMENTO' as DATA_PAGAMENTO
            , 'CENTRO CUSTO' as CENTRO_CUSTO
    UNION ALL
    
        SELECT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(P.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'31' as CHAVE 
            , ISNULL(CAST(F.CENTRO_CUSTO AS NVARCHAR(MAX)), '*SEM CADASTRO DE COD FORNECEDOR*')  as CONTA   
            , convert(varchar(16),Replace(cast( (P.VLR_TOTAL) as decimal(19,2)),'.',','))   AS  MONTANTE
            , (select DESCRICAO from DESP_MOEDAS where CODIGO = P.MOEDA)    AS MOEDA
            , ISNULL(CAST(P.JUSTIFICATIVA AS NVARCHAR(MAX)), '*JUSTIFICATIVA NÃO PREENCHIDA*') as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , '' as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_PRINCIPAL  P   
        INNER JOIN FUNCIONARIOS F ON F.ID = P.COD_SOLICITANTE
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE P.RELATORIO in (@Codigo)

    UNION ALL
    
        SELECT DISTINCT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(L.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'40' as CHAVE 
            , ISNULL(CAST(R.CONTACREDITO  AS NVARCHAR(MAX)), '*SEM CADASTRO DE CONTA CONTABIL*') as CONTA   
            , convert(varchar(16),Replace(cast( (L.VLR_TOTAL) as decimal(19,2)),'.',','))AS  MONTANTE
            , (select DESCRICAO from DESP_MOEDAS where CODIGO = L.MOEDA_LANC)    AS MOEDA
            , CAST(L.LANCAMENTO AS NVARCHAR(MAX)) + ' ' + CAST(L.DESCRICAO AS NVARCHAR(MAX))  as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , CAST(P.CCUSTO AS NVARCHAR(5))  as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_LANCAMENTO L
        INNER JOIN DESP_VW_DESP_PRINCIPAL   P    ON P.RELATORIO = L.RELATORIO
        INNER JOIN FUNCIONARIOS F ON F.ID = L.COD_SOLICITANTE
        INNER JOIN dbo.DESP_RELACTIPODESPESAXCONTACON R ON R.TIPODESPESA = L.COD_TIPO_DESPESA
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE L.RELATORIO in (@Codigo)

  END
  ELSE 
    BEGIN
    INSERT INTO #temp   
        
        SELECT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(P.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'31' as CHAVE 
            , ISNULL(CAST(F.CENTRO_CUSTO AS NVARCHAR(MAX)), '*SEM CADASTRO DE COD FORNECEDOR*')  as CONTA   
            , convert(varchar(16),Replace(cast( (P.VLR_TOTAL) as decimal(19,2)),'.',','))   AS  MONTANTE
            ,(select DESCRICAO from DESP_MOEDAS where CODIGO = P.MOEDA)    AS MOEDA
            , ISNULL(CAST(P.JUSTIFICATIVA AS NVARCHAR(MAX)), '*JUSTIFICATIVA NÃO PREENCHIDA*') as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , '' as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_PRINCIPAL  P   
        INNER JOIN FUNCIONARIOS F ON F.ID = P.COD_SOLICITANTE
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE P.RELATORIO in (@Codigo)

    UNION ALL
    
        SELECT DISTINCT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(L.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'40' as CHAVE 
            , ISNULL(CAST(R.CONTACREDITO  AS NVARCHAR(MAX)), '*SEM CADASTRO DE CONTA CONTABIL*') as CONTA   
            , convert(varchar(16),Replace(cast( (L.VLR_TOTAL) as decimal(19,2)),'.',','))AS  MONTANTE
            ,(select DESCRICAO from DESP_MOEDAS where CODIGO = L.MOEDA)    AS MOEDA
            , CAST(L.LANCAMENTO AS NVARCHAR(MAX)) + ' ' + CAST(L.DESCRICAO AS NVARCHAR(MAX))  as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , CAST(P.CCUSTO AS NVARCHAR(5))  as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_LANCAMENTO L
        INNER JOIN DESP_VW_DESP_PRINCIPAL   P    ON P.RELATORIO = L.RELATORIO
        INNER JOIN FUNCIONARIOS F ON F.ID = L.COD_SOLICITANTE
        INNER JOIN dbo.DESP_RELACTIPODESPESAXCONTACON R ON R.TIPODESPESA = L.COD_TIPO_DESPESA
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE L.RELATORIO in (@Codigo)
        

  END               
    
  SELECT distinct * FROM #temp order by REFERENCIA desc, CENTRO_CUSTO ASC
        
END        
  

Minha procedure

ALTER PROCEDURE [dbo].[SEL_DESP_ExportaArquivoSAP_reembolsavel] 
(   @Codigo VARCHAR(MAX)  ,  @cont as int   , @DataPago as VARCHAR(10) )
AS         
BEGIN               
   
  CREATE TABLE #temp
    (REFERENCIA NVARCHAR(max)
    , CHAVE    NVARCHAR(max)
    , CONTA   NVARCHAR(max)
    , MONTANTE NVARCHAR(max)
    , MOEDA NVARCHAR(max)
    , TEXTO NVARCHAR(max)   
    , DATA_PAGAMENTO NVARCHAR(max)  
    , CENTRO_CUSTO NVARCHAR(max));
    
  IF ( @cont = 1)
  BEGIN
    INSERT INTO #temp
        SELECT 
              'REFERENCIA'  as   REFERENCIA
            , 'CHAVE' as CHAVE    
            , 'CONTA' as CONTA   
            , 'MONTANTE'  AS  MONTANTE
            , 'MOEDA'    AS MOEDA
            , 'TEXTO' as TEXTO
            , 'DATA PAGAMENTO' as DATA_PAGAMENTO
            , 'CENTRO CUSTO' as CENTRO_CUSTO
    UNION ALL
    
        SELECT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(P.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'31' as CHAVE 
            , ISNULL(CAST(F.CENTRO_CUSTO AS NVARCHAR(MAX)), '*SEM CADASTRO DE COD FORNECEDOR*')  as CONTA   
            , convert(varchar(16),Replace(cast( (P.VLR_TOTAL) as decimal(19,2)),'.',','))   AS  MONTANTE
            , (select DESCRICAO from DESP_MOEDAS where CODIGO = P.MOEDA)    AS MOEDA
            , ISNULL(CAST(P.JUSTIFICATIVA AS NVARCHAR(MAX)), '*JUSTIFICATIVA NÃO PREENCHIDA*') as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , '' as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_PRINCIPAL  P   
        INNER JOIN FUNCIONARIOS F ON F.ID = P.COD_SOLICITANTE
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE P.RELATORIO in (@Codigo)

    UNION ALL
    
        SELECT DISTINCT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(L.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'40' as CHAVE 
            , ISNULL(CAST(R.CONTACREDITO  AS NVARCHAR(MAX)), '*SEM CADASTRO DE CONTA CONTABIL*') as CONTA   
            , convert(varchar(16),Replace(cast( (L.VLR_TOTAL) as decimal(19,2)),'.',','))AS  MONTANTE
            , (select DESCRICAO from DESP_MOEDAS where CODIGO = L.MOEDA_LANC)    AS MOEDA
            , CAST(L.LANCAMENTO AS NVARCHAR(MAX)) + ' ' + CAST(L.DESCRICAO AS NVARCHAR(MAX))  as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , CAST(P.CCUSTO AS NVARCHAR(5))  as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_LANCAMENTO L
        INNER JOIN DESP_VW_DESP_PRINCIPAL   P    ON P.RELATORIO = L.RELATORIO
        INNER JOIN FUNCIONARIOS F ON F.ID = L.COD_SOLICITANTE
        INNER JOIN dbo.DESP_RELACTIPODESPESAXCONTACON R ON R.TIPODESPESA = L.COD_TIPO_DESPESA
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE L.RELATORIO in (@Codigo)

  END
  ELSE 
    BEGIN
    INSERT INTO #temp   
        
        SELECT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(P.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'31' as CHAVE 
            , ISNULL(CAST(F.CENTRO_CUSTO AS NVARCHAR(MAX)), '*SEM CADASTRO DE COD FORNECEDOR*')  as CONTA   
            , convert(varchar(16),Replace(cast( (P.VLR_TOTAL) as decimal(19,2)),'.',','))   AS  MONTANTE
            ,(select DESCRICAO from DESP_MOEDAS where CODIGO = P.MOEDA)    AS MOEDA
            , ISNULL(CAST(P.JUSTIFICATIVA AS NVARCHAR(MAX)), '*JUSTIFICATIVA NÃO PREENCHIDA*') as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , '' as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_PRINCIPAL  P   
        INNER JOIN FUNCIONARIOS F ON F.ID = P.COD_SOLICITANTE
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE P.RELATORIO in (@Codigo)

    UNION ALL
    
        SELECT DISTINCT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(L.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'40' as CHAVE 
            , ISNULL(CAST(R.CONTACREDITO  AS NVARCHAR(MAX)), '*SEM CADASTRO DE CONTA CONTABIL*') as CONTA   
            , convert(varchar(16),Replace(cast( (L.VLR_TOTAL) as decimal(19,2)),'.',','))AS  MONTANTE
            ,(select DESCRICAO from DESP_MOEDAS where CODIGO = L.MOEDA)    AS MOEDA
            , CAST(L.LANCAMENTO AS NVARCHAR(MAX)) + ' ' + CAST(L.DESCRICAO AS NVARCHAR(MAX))  as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , CAST(P.CCUSTO AS NVARCHAR(5))  as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_LANCAMENTO L
        INNER JOIN DESP_VW_DESP_PRINCIPAL   P    ON P.RELATORIO = L.RELATORIO
        INNER JOIN FUNCIONARIOS F ON F.ID = L.COD_SOLICITANTE
        INNER JOIN dbo.DESP_RELACTIPODESPESAXCONTACON R ON R.TIPODESPESA = L.COD_TIPO_DESPESA
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE L.RELATORIO in (@Codigo)
        

  END               
    
  SELECT distinct * FROM #temp order by REFERENCIA desc, CENTRO_CUSTO ASC
        
END        
  
adicionou 4887 caracteres ao conteúdo
Fonte Link
Rafael Barbosa
  • 2,8mil
  • 1
  • 17
  • 37

O que posso fazer para conseguir declarar o valor deMinha @Codigoprocedure de forma que funcione regularmente?

ALTER PROCEDURE [dbo].[SEL_DESP_ExportaArquivoSAP_reembolsavel] 
(   @Codigo VARCHAR(MAX)  ,  @cont as int   , @DataPago as VARCHAR(10) )
AS         
BEGIN               
   
  CREATE TABLE #temp
    (REFERENCIA NVARCHAR(max)
    , CHAVE    NVARCHAR(max)
    , CONTA   NVARCHAR(max)
    , MONTANTE NVARCHAR(max)
    , MOEDA NVARCHAR(max)
    , TEXTO NVARCHAR(max)   
    , DATA_PAGAMENTO NVARCHAR(max)  
    , CENTRO_CUSTO NVARCHAR(max));
    
  IF ( @cont = 1)
  BEGIN
    INSERT INTO #temp
        SELECT 
              'REFERENCIA'  as   REFERENCIA
            , 'CHAVE' as CHAVE    
            , 'CONTA' as CONTA   
            , 'MONTANTE'  AS  MONTANTE
            , 'MOEDA'    AS MOEDA
            , 'TEXTO' as TEXTO
            , 'DATA PAGAMENTO' as DATA_PAGAMENTO
            , 'CENTRO CUSTO' as CENTRO_CUSTO
    UNION ALL
    
        SELECT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(P.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'31' as CHAVE 
            , ISNULL(CAST(F.CENTRO_CUSTO AS NVARCHAR(MAX)), '*SEM CADASTRO DE COD FORNECEDOR*')  as CONTA   
            , convert(varchar(16),Replace(cast( (P.VLR_TOTAL) as decimal(19,2)),'.',','))   AS  MONTANTE
            , (select DESCRICAO from DESP_MOEDAS where CODIGO = P.MOEDA)    AS MOEDA
            , ISNULL(CAST(P.JUSTIFICATIVA AS NVARCHAR(MAX)), '*JUSTIFICATIVA NÃO PREENCHIDA*') as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , '' as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_PRINCIPAL  P   
        INNER JOIN FUNCIONARIOS F ON F.ID = P.COD_SOLICITANTE
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE P.RELATORIO in (@Codigo)

    UNION ALL
    
        SELECT DISTINCT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(L.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'40' as CHAVE 
            , ISNULL(CAST(R.CONTACREDITO  AS NVARCHAR(MAX)), '*SEM CADASTRO DE CONTA CONTABIL*') as CONTA   
            , convert(varchar(16),Replace(cast( (L.VLR_TOTAL) as decimal(19,2)),'.',','))AS  MONTANTE
            , (select DESCRICAO from DESP_MOEDAS where CODIGO = L.MOEDA_LANC)    AS MOEDA
            , CAST(L.LANCAMENTO AS NVARCHAR(MAX)) + ' ' + CAST(L.DESCRICAO AS NVARCHAR(MAX))  as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , CAST(P.CCUSTO AS NVARCHAR(5))  as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_LANCAMENTO L
        INNER JOIN DESP_VW_DESP_PRINCIPAL   P    ON P.RELATORIO = L.RELATORIO
        INNER JOIN FUNCIONARIOS F ON F.ID = L.COD_SOLICITANTE
        INNER JOIN dbo.DESP_RELACTIPODESPESAXCONTACON R ON R.TIPODESPESA = L.COD_TIPO_DESPESA
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE L.RELATORIO in (@Codigo)

  END
  ELSE 
    BEGIN
    INSERT INTO #temp   
        
        SELECT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(P.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'31' as CHAVE 
            , ISNULL(CAST(F.CENTRO_CUSTO AS NVARCHAR(MAX)), '*SEM CADASTRO DE COD FORNECEDOR*')  as CONTA   
            , convert(varchar(16),Replace(cast( (P.VLR_TOTAL) as decimal(19,2)),'.',','))   AS  MONTANTE
            ,(select DESCRICAO from DESP_MOEDAS where CODIGO = P.MOEDA)    AS MOEDA
            , ISNULL(CAST(P.JUSTIFICATIVA AS NVARCHAR(MAX)), '*JUSTIFICATIVA NÃO PREENCHIDA*') as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , '' as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_PRINCIPAL  P   
        INNER JOIN FUNCIONARIOS F ON F.ID = P.COD_SOLICITANTE
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE P.RELATORIO in (@Codigo)

    UNION ALL
    
        SELECT DISTINCT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(L.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'40' as CHAVE 
            , ISNULL(CAST(R.CONTACREDITO  AS NVARCHAR(MAX)), '*SEM CADASTRO DE CONTA CONTABIL*') as CONTA   
            , convert(varchar(16),Replace(cast( (L.VLR_TOTAL) as decimal(19,2)),'.',','))AS  MONTANTE
            ,(select DESCRICAO from DESP_MOEDAS where CODIGO = L.MOEDA)    AS MOEDA
            , CAST(L.LANCAMENTO AS NVARCHAR(MAX)) + ' ' + CAST(L.DESCRICAO AS NVARCHAR(MAX))  as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , CAST(P.CCUSTO AS NVARCHAR(5))  as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_LANCAMENTO L
        INNER JOIN DESP_VW_DESP_PRINCIPAL   P    ON P.RELATORIO = L.RELATORIO
        INNER JOIN FUNCIONARIOS F ON F.ID = L.COD_SOLICITANTE
        INNER JOIN dbo.DESP_RELACTIPODESPESAXCONTACON R ON R.TIPODESPESA = L.COD_TIPO_DESPESA
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE L.RELATORIO in (@Codigo)
        

  END               
    
  SELECT distinct * FROM #temp order by REFERENCIA desc, CENTRO_CUSTO ASC
        
END        
  

O que posso fazer para conseguir declarar o valor de @Codigo de forma que funcione regularmente?

Minha procedure

ALTER PROCEDURE [dbo].[SEL_DESP_ExportaArquivoSAP_reembolsavel] 
(   @Codigo VARCHAR(MAX)  ,  @cont as int   , @DataPago as VARCHAR(10) )
AS         
BEGIN               
   
  CREATE TABLE #temp
    (REFERENCIA NVARCHAR(max)
    , CHAVE    NVARCHAR(max)
    , CONTA   NVARCHAR(max)
    , MONTANTE NVARCHAR(max)
    , MOEDA NVARCHAR(max)
    , TEXTO NVARCHAR(max)   
    , DATA_PAGAMENTO NVARCHAR(max)  
    , CENTRO_CUSTO NVARCHAR(max));
    
  IF ( @cont = 1)
  BEGIN
    INSERT INTO #temp
        SELECT 
              'REFERENCIA'  as   REFERENCIA
            , 'CHAVE' as CHAVE    
            , 'CONTA' as CONTA   
            , 'MONTANTE'  AS  MONTANTE
            , 'MOEDA'    AS MOEDA
            , 'TEXTO' as TEXTO
            , 'DATA PAGAMENTO' as DATA_PAGAMENTO
            , 'CENTRO CUSTO' as CENTRO_CUSTO
    UNION ALL
    
        SELECT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(P.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'31' as CHAVE 
            , ISNULL(CAST(F.CENTRO_CUSTO AS NVARCHAR(MAX)), '*SEM CADASTRO DE COD FORNECEDOR*')  as CONTA   
            , convert(varchar(16),Replace(cast( (P.VLR_TOTAL) as decimal(19,2)),'.',','))   AS  MONTANTE
            , (select DESCRICAO from DESP_MOEDAS where CODIGO = P.MOEDA)    AS MOEDA
            , ISNULL(CAST(P.JUSTIFICATIVA AS NVARCHAR(MAX)), '*JUSTIFICATIVA NÃO PREENCHIDA*') as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , '' as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_PRINCIPAL  P   
        INNER JOIN FUNCIONARIOS F ON F.ID = P.COD_SOLICITANTE
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE P.RELATORIO in (@Codigo)

    UNION ALL
    
        SELECT DISTINCT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(L.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'40' as CHAVE 
            , ISNULL(CAST(R.CONTACREDITO  AS NVARCHAR(MAX)), '*SEM CADASTRO DE CONTA CONTABIL*') as CONTA   
            , convert(varchar(16),Replace(cast( (L.VLR_TOTAL) as decimal(19,2)),'.',','))AS  MONTANTE
            , (select DESCRICAO from DESP_MOEDAS where CODIGO = L.MOEDA_LANC)    AS MOEDA
            , CAST(L.LANCAMENTO AS NVARCHAR(MAX)) + ' ' + CAST(L.DESCRICAO AS NVARCHAR(MAX))  as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , CAST(P.CCUSTO AS NVARCHAR(5))  as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_LANCAMENTO L
        INNER JOIN DESP_VW_DESP_PRINCIPAL   P    ON P.RELATORIO = L.RELATORIO
        INNER JOIN FUNCIONARIOS F ON F.ID = L.COD_SOLICITANTE
        INNER JOIN dbo.DESP_RELACTIPODESPESAXCONTACON R ON R.TIPODESPESA = L.COD_TIPO_DESPESA
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE L.RELATORIO in (@Codigo)

  END
  ELSE 
    BEGIN
    INSERT INTO #temp   
        
        SELECT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(P.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'31' as CHAVE 
            , ISNULL(CAST(F.CENTRO_CUSTO AS NVARCHAR(MAX)), '*SEM CADASTRO DE COD FORNECEDOR*')  as CONTA   
            , convert(varchar(16),Replace(cast( (P.VLR_TOTAL) as decimal(19,2)),'.',','))   AS  MONTANTE
            ,(select DESCRICAO from DESP_MOEDAS where CODIGO = P.MOEDA)    AS MOEDA
            , ISNULL(CAST(P.JUSTIFICATIVA AS NVARCHAR(MAX)), '*JUSTIFICATIVA NÃO PREENCHIDA*') as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , '' as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_PRINCIPAL  P   
        INNER JOIN FUNCIONARIOS F ON F.ID = P.COD_SOLICITANTE
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE P.RELATORIO in (@Codigo)

    UNION ALL
    
        SELECT DISTINCT 
             'RD ' + UPPER(LEFT(F.USERNAME,1)+substring( F.USERNAME, charindex( '.', F.USERNAME )+1, 1 ) ) + CAST(L.RELATORIO AS NVARCHAR(5))  as   REFERENCIA
            ,'40' as CHAVE 
            , ISNULL(CAST(R.CONTACREDITO  AS NVARCHAR(MAX)), '*SEM CADASTRO DE CONTA CONTABIL*') as CONTA   
            , convert(varchar(16),Replace(cast( (L.VLR_TOTAL) as decimal(19,2)),'.',','))AS  MONTANTE
            ,(select DESCRICAO from DESP_MOEDAS where CODIGO = L.MOEDA)    AS MOEDA
            , CAST(L.LANCAMENTO AS NVARCHAR(MAX)) + ' ' + CAST(L.DESCRICAO AS NVARCHAR(MAX))  as TEXTO
            , @DataPago as DATA_PAGAMENTO
            , CAST(P.CCUSTO AS NVARCHAR(5))  as CENTRO_CUSTO
        FROM
        DESP_VW_DESP_LANCAMENTO L
        INNER JOIN DESP_VW_DESP_PRINCIPAL   P    ON P.RELATORIO = L.RELATORIO
        INNER JOIN FUNCIONARIOS F ON F.ID = L.COD_SOLICITANTE
        INNER JOIN dbo.DESP_RELACTIPODESPESAXCONTACON R ON R.TIPODESPESA = L.COD_TIPO_DESPESA
        INNER JOIN DESP_PRINCIPAL D ON D.CODIGO in (@Codigo)
        WHERE L.RELATORIO in (@Codigo)
        

  END               
    
  SELECT distinct * FROM #temp order by REFERENCIA desc, CENTRO_CUSTO ASC
        
END        
  
Fonte Link
Rafael Barbosa
  • 2,8mil
  • 1
  • 17
  • 37

Receber valor de variável em 'where in'

Em minha procedure tenho as seguintes variáveis:

@Codigo VARCHAR(MAX),@cont as int, @DataPago as VARCHAR(10)

Via aplicação C# estou passando os valores da seguinte forma:

y1.Parameters.Add("@Codigo", SqlDbType.VarChar).Value = "2,4";
y1.Parameters.Add("@cont", SqlDbType.Int).Value = 1;
y1.Parameters.Add("@DataPago", SqlDbType.VarChar, 10).Value = "20/05/2015";

O problema é que eu estou obtendo o seguinte erro:

Conversion failed when converting the varchar value '2,4' to data type int.

Porém, quando coloco o valor direto na minha procedure, funciona, como abaixo:

WHERE P.RELATORIO in (2,4)

O que posso fazer para conseguir declarar o valor de @Codigo de forma que funcione regularmente?