Perguntas Ajuda no Select

Mais
7 anos 5 meses atrás #32218 por silvana
Ajuda no Select foi criado por silvana
Bom dia,

Estou tentando fazer uma rotina, para gerar informação de 2 tabelas em todas as empresas (a partir da empresa 41) unindo em um arquivo todas as informações. Mas não estou conseguindo ir para outra empresa. Alguém poderia me ajudar?

#Include "PROTHEUS.CH"
#Include "TOPCONN.CH"
#INCLUDE "rwmake.ch"




User Function TESTE() // nome da funcao no menu
Local nOrdem // publica variavel
Private cDirDocs := MsDocPath() // priva variavel com o caminho do arquivo temporario na rotina
Private cNomeArq := CriaTrab(,.F.)
Private cEmpresa := '410'
Private Faz := 1

While Faz = 1

cQuery := "SELECT LTRIM(STR(cEmpresa ,3,0))AS EMPRESA,CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112)+'01' AS CSC_DTINI, MV.CT7_CONTA AS CSC_CONTA, ISNULL(CT.CT1_DESC01,'') AS CS3_NOMECT, '' AS CSC_CCUSTO,'' AS CS5_NOME, "
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT7_DATA,1,6) <CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT7_DEBITO - MV.CT7_CREDIT ELSE 0 END),18,2)) AS CSC_VALINI, 'D' AS CSC_INDINI,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT7_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT7_DEBITO ELSE 0 END),18,2)) AS CSC_VALDEB,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT7_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT7_CREDIT ELSE 0 END),18,2)) AS CSC_VALCRE,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT7_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT7_DEBITO- MV.CT7_CREDIT ELSE 0 END),18,2)) AS CSC_VALMOV,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT7_DATA,1,6) <=CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT7_DEBITO - MV.CT7_CREDIT ELSE 0 END),18,2)) AS CSC_VALFIN, 'D' AS CSCINDFIM"
cQuery += " FROM CT7"+cEmpresa +" MV LEFT JOIN CT1"+cEmpresa+" CT"
cQuery += " ON CT.CT1_CONTA = MV.CT7_CONTA"
cQuery += " WHERE SUBSTRING(MV.CT7_DATA,1,6) <=CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112)"
cQuery += " AND MV.D_E_L_E_T_ = ''"
cQuery += " AND (MV.CT7_CONTA LIKE '1%' OR MV.CT7_CONTA LIKE '2%')"
cQuery += " GROUP BY MV.CT7_CONTA, ISNULL(CT.CT1_DESC01,'')"
cQuery += " UNION ALL"
cQuery += " SELECT LTRIM(Str(cEmpresa ,3,0)) AS EMPRESA, CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112)+'01' AS CSC_DTINI, MV.CT3_CONTA AS CSC_CONTA, ISNULL(CT.CT1_DESC01,'') AS CS3_NOMECT, MV.CT3_CUSTO AS CSC_CCUSTO,'' AS CS5_NOME,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT3_DATA,1,6) <CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT3_DEBITO - MV.CT3_CREDIT ELSE 0 END),18,2)) AS CSC_VALINI, 'D' AS CSC_INDINI,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT3_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT3_DEBITO ELSE 0 END),18,2)) AS CSC_VALDEB,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT3_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT3_CREDIT ELSE 0 END),18,2)) AS CSC_VALCRE,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT3_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT3_DEBITO- MV.CT3_CREDIT ELSE 0 END),18,2)) AS CSC_VALMOV,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT3_DATA,1,6) <=CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT3_DEBITO - MV.CT3_CREDIT ELSE 0 END),18,2)) AS CSC_VALFIN, 'D' AS CSCINDFIM"
cQuery += " FROM CT3"+cEmpresa +" MV LEFT JOIN CT1"+cEmpresa +" CT"
cQuery += " ON CT.CT1_CONTA = MV.CT3_CONTA"
cQuery += " WHERE SUBSTRING(MV.CT3_DATA,1,6) <=CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) AND isnull(MV.D_E_L_E_T_,'') = ''"
cQuery += " AND (MV.CT3_CONTA NOT LIKE '1%' AND MV.CT3_CONTA NOT LIKE '2%')"
cQuery += " GROUP BY MV.CT3_CONTA, ISNULL(CT.CT1_DESC01,''), MV.CT3_CUSTO"
cQuery += " UNION ALL"

cQuery += " cEmpresa:= rtrim(ltrim(convert(nvarchar(4),convert(numeric,cEmpresa)+10)))"



IF cEmpresa = '430'
cQuery := "SELECT LTRIM(STR(cEmpresa ,3,0))AS EMPRESA,CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112)+'01' AS CSC_DTINI, MV.CT7_CONTA AS CSC_CONTA, ISNULL(CT.CT1_DESC01,'') AS CS3_NOMECT, '' AS CSC_CCUSTO,'' AS CS5_NOME, "
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT7_DATA,1,6) <CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT7_DEBITO - MV.CT7_CREDIT ELSE 0 END),18,2)) AS CSC_VALINI, 'D' AS CSC_INDINI,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT7_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT7_DEBITO ELSE 0 END),18,2)) AS CSC_VALDEB,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT7_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT7_CREDIT ELSE 0 END),18,2)) AS CSC_VALCRE,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT7_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT7_DEBITO- MV.CT7_CREDIT ELSE 0 END),18,2)) AS CSC_VALMOV,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT7_DATA,1,6) <=CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT7_DEBITO - MV.CT7_CREDIT ELSE 0 END),18,2)) AS CSC_VALFIN, 'D' AS CSCINDFIM"
cQuery += " FROM CT7"+cEmpresa +" MV LEFT JOIN CT1"+cEmpresa+" CT"
cQuery += " ON CT.CT1_CONTA = MV.CT7_CONTA"
cQuery += " WHERE SUBSTRING(MV.CT7_DATA,1,6) <=CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112)"
cQuery += " AND MV.D_E_L_E_T_ = ''"
cQuery += " AND (MV.CT7_CONTA LIKE '1%' OR MV.CT7_CONTA LIKE '2%')"
cQuery += " GROUP BY MV.CT7_CONTA, ISNULL(CT.CT1_DESC01,'')"
cQuery += " UNION ALL"
cQuery += " SELECT LTRIM(Str(cEmpresa ,3,0)) AS EMPRESA, CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112)+'01' AS CSC_DTINI, MV.CT3_CONTA AS CSC_CONTA, ISNULL(CT.CT1_DESC01,'') AS CS3_NOMECT, MV.CT3_CUSTO AS CSC_CCUSTO,'' AS CS5_NOME,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT3_DATA,1,6) <CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT3_DEBITO - MV.CT3_CREDIT ELSE 0 END),18,2)) AS CSC_VALINI, 'D' AS CSC_INDINI,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT3_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT3_DEBITO ELSE 0 END),18,2)) AS CSC_VALDEB,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT3_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT3_CREDIT ELSE 0 END),18,2)) AS CSC_VALCRE,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT3_DATA,1,6) =CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT3_DEBITO- MV.CT3_CREDIT ELSE 0 END),18,2)) AS CSC_VALMOV,"
cQuery += " LTRIM(Str(SUM(CASE WHEN SUBSTRING(MV.CT3_DATA,1,6) <=CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) THEN MV.CT3_DEBITO - MV.CT3_CREDIT ELSE 0 END),18,2)) AS CSC_VALFIN, 'D' AS CSCINDFIM"
cQuery += " FROM CT3"+cEmpresa +" MV LEFT JOIN CT1"+cEmpresa +" CT"
cQuery += " ON CT.CT1_CONTA = MV.CT3_CONTA"
cQuery += " WHERE SUBSTRING(MV.CT3_DATA,1,6) <=CONVERT(nvarchar(6), DATEADD(month, -1, GETDATE()), 112) AND isnull(MV.D_E_L_E_T_,'') = ''"
cQuery += " AND (MV.CT3_CONTA NOT LIKE '1%' AND MV.CT3_CONTA NOT LIKE '2%')"
cQuery += " GROUP BY MV.CT3_CONTA, ISNULL(CT.CT1_DESC01,''), MV.CT3_CUSTO"

else
// break

Faz = 0
ENDIF

ENDDO

Por favor Acessar ou Registrar para participar da conversa.

Mais
7 anos 5 meses atrás #32220 por admin
Respondido por admin no tópico Ajuda no Select
Silvana boa tarde, vc precisa desse arquivo uma unica vez ?

Não consegui entender qual sua necessidade.

Por favor Acessar ou Registrar para participar da conversa.

Tempo para a criação da página:0.101 segundos
Joomla templates by a4joomla