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