我必须优化每天导出的查询,并且通过创建一些表变量来做到这一点。当我现在尝试使用Import / Export-Wizard导出查询时,我得到一个空的csv文件(它具有标题,但没有数据)。
该查询在Management Studio中执行时工作正常,并且在进行更改之前可以毫无问题地导出。我所做的主要更改是表变量,因此我认为这就是问题所在。我什至可以在预览中看到数据,但不会导出。 在Google上找不到关于该主题的太多信息。
/****** Skript für Erfassungsprotokoll Faktura daily Export (Version 2019-04-23 mit Temporären Tabellen) ******/
Declare @daily_date Date, @start_date Date, @first_date Date
set @daily_date = DATEADD(day,-1,getdate())
set @start_date = @daily_date
set @first_date = DATEADD(day,-90,getdate())
DECLARE @salestempline TABLE
(
ITEMID nvarchar(20) NOT NULL,
SALESID nvarchar(20) NOT NULL,
SALESPRICE numeric(28,12) NOT NULL,
NAME nvarchar(1000) NOT NULL,
DIMENSION2_ nvarchar(10),
DIMENSION3_ nvarchar(10)
);
INSERT INTO @salestempline (ITEMID, SALESID, SALESPRICE, NAME, DIMENSION2_, DIMENSION3_)
SELECT [SALESLINE].[ITEMID], [SALESID], [SALESPRICE], [NAME], DIT.[DIMENSION2_], DIT.[DIMENSION3_]
FROM [AX2009_PROD].[dbo].[SALESLINE]
left outer join (SELECT ITEMID, DIMENSION2_, DIMENSION3_ FROM [AX2009_PROD].[dbo].[INVENTTABLE]) DIT
ON SUBSTRING(SALESLINE.NAME, CHARINDEX('(', SALESLINE.NAME) + 1, 6) = DIT.[ITEMID]
WHERE [SALESLINE].[ITEMID] in ('nachtr.-RV','sonst.-RV')
GROUP BY [SALESLINE].[ITEMID], [SALESID], [SALESPRICE], [NAME], DIT.[DIMENSION2_], DIT.[DIMENSION3_];
DECLARE @ledgtrans TABLE
(
[ACCOUNTNUM] [nvarchar](20) NOT NULL,
[TRANSDATE] [datetime] NOT NULL,
[TRANSTYPE] [int] NOT NULL,
[VOUCHER] [nvarchar](20) NOT NULL,
[CREDITING] [int] NOT NULL,
[TXT] [nvarchar](60) NOT NULL,
[CREATEDDATETIME] [datetime] NOT NULL,
[DIMENSION2_] [nvarchar](10) NOT NULL,
[DIMENSION3_] [nvarchar](10) NOT NULL,
[AMOUNTCUR] [numeric](28, 12) NOT NULL,
[TAXREFID] [int] NOT NULL,
[DATAAREAID] [nvarchar](4) NOT NULL
);
INSERT INTO @ledgtrans ([ACCOUNTNUM], [TRANSDATE], [TRANSTYPE], [VOUCHER], [CREDITING], [TXT], [CREATEDDATETIME], [DIMENSION2_], [DIMENSION3_], [AMOUNTCUR], [TAXREFID], [DATAAREAID])
SELECT [ACCOUNTNUM], [TRANSDATE], [TRANSTYPE], [VOUCHER], [CREDITING], [TXT], [CREATEDDATETIME], [DIMENSION2_], [DIMENSION3_], [AMOUNTCUR], [TAXREFID], [DATAAREAID]
FROM [AX2009_PROD].[dbo].[LEDGERTRANS]
WHERE ([LEDGERTRANS].VOUCHER like 'RG%' or [LEDGERTRANS].VOUCHER like 'GS%' or [LEDGERTRANS].VOUCHER like 'MA%' or [LEDGERTRANS].VOUCHER like 'ST%') and [LEDGERTRANS].[CREATEDDATETIME] between @start_date and DATEADD(day,1,@daily_date) and [LEDGERTRANS].[TRANSDATE] >= @first_date;
DECLARE @taxtran TABLE
(
[VOUCHER] [nvarchar](20) NOT NULL,
[TRANSDATE] [datetime] NOT NULL,
[TAXBASEAMOUNT] [numeric](28, 12) NOT NULL,
[TAXAMOUNT] [numeric](28, 12) NOT NULL,
[TAXVALUE] [numeric](28, 12) NOT NULL,
[TAXREFID] [int] NOT NULL,
[OPERATIONACCOUNT] [nvarchar](20) NOT NULL,
[DIMENSION2_] [nvarchar](10) NOT NULL,
[DIMENSION3_] [nvarchar](10) NOT NULL,
[PRINTCODE] [nvarchar](10) NOT NULL,
[DATAAREAID] [nvarchar](4) NOT NULL
);
INSERT INTO @taxtran ([VOUCHER], [TRANSDATE], [TAXBASEAMOUNT], [TAXAMOUNT], [TAXVALUE], [TAXREFID], [OPERATIONACCOUNT], [DIMENSION2_], [DIMENSION3_], [PRINTCODE], [DATAAREAID])
SELECT [VOUCHER], [TRANSDATE], [TAXBASEAMOUNT], [TAXAMOUNT], [TAXVALUE], [TAXREFID], [OPERATIONACCOUNT], [DIMENSION2_], [DIMENSION3_], [PRINTCODE], [DATAAREAID]
FROM [AX2009_PROD].[dbo].[TAXTRANS]
WHERE ([TAXTRANS].VOUCHER like 'RG%' or [TAXTRANS].VOUCHER like 'GS%' or [TAXTRANS].VOUCHER like 'MA%' or [TAXTRANS].VOUCHER like 'ST%') and TRANSDATE >= @first_date;
DECLARE @taxtran2 TABLE
(
[VOUCHER] [nvarchar](20) NOT NULL,
[TRANSDATE] [datetime] NOT NULL,
[DIMENSION3_] [nvarchar](10) NOT NULL,
[PRINTCODE] [nvarchar](10) NOT NULL,
[DATAAREAID] [nvarchar](4) NOT NULL
);
INSERT INTO @taxtran2 ([VOUCHER], [TRANSDATE], [DIMENSION3_], [PRINTCODE], [DATAAREAID])
select VOUCHER, TRANSDATE, [DIMENSION3_], [PRINTCODE], DATAAREAID from @taxtran GROUP BY VOUCHER, TRANSDATE, [DIMENSION3_], [PRINTCODE], DATAAREAID;
DECLARE @taxtran3 TABLE
(
[VOUCHER] [nvarchar](20) NOT NULL,
[Basis] [numeric](28, 12) NOT NULL,
[Steuer] [numeric](28, 12) NOT NULL,
[TAXVALUE] [numeric](28, 12) NOT NULL,
[DIMENSION2_] [nvarchar](10) NOT NULL,
[DIMENSION3_] [nvarchar](10) NOT NULL,
[OPERATIONACCOUNT] [nvarchar](20) NOT NULL,
[TAXREFID] [int] NOT NULL
);
INSERT INTO @taxtran3 ([VOUCHER], [Basis], [Steuer], [TAXVALUE], [DIMENSION2_], [DIMENSION3_], [OPERATIONACCOUNT], [TAXREFID])
SELECT [VOUCHER]
,sum([TAXBASEAMOUNT]) 'Basis'
,sum([TAXAMOUNT]) 'Steuer'
,[TAXVALUE]
,[DIMENSION2_]
,[DIMENSION3_]
,[OPERATIONACCOUNT]
,[TAXREFID]
FROM @taxtran
group by [VOUCHER]
,[TAXVALUE]
,[DIMENSION2_]
,[DIMENSION3_]
,[OPERATIONACCOUNT]
,[TAXREFID];
Declare @tmptable TABLE
(
[Rechnungsdatum] [nvarchar](20) NOT NULL,
[Buchungsdatum] [nvarchar](20) NOT NULL,
[Fälligkeit] [nvarchar](20) NOT NULL,
[Rechnung] [nvarchar](20) NOT NULL,
[Auftrag] [nvarchar](20) NOT NULL,
[Debitorenkonto] [nvarchar](20) NOT NULL,
[Rechnungskonto] [nvarchar](20) NOT NULL,
[Debitor] [nvarchar](60) NOT NULL,
[Debitorenadresse] [nvarchar](250) NOT NULL,
[Konto] [nvarchar](20) NOT NULL,
[Kontoname] [nvarchar](60) NOT NULL,
[Kst] [nvarchar](10) NOT NULL,
[Kostenstelle] [nvarchar](60) NOT NULL,
[Kostenträger] [nvarchar](10) NOT NULL,
[Betrag] [numeric](15, 2) NOT NULL,
[Steuerbetrag] [numeric](15, 2) NOT NULL,
[Buchungstyp] [int] NOT NULL,
[Steuerreferenz] [int] NOT NULL,
[Steuersatz] [nvarchar](10) NOT NULL,
[OP] [nvarchar](10) NOT NULL,
[S/H] [nvarchar](10) NOT NULL
);
INSERT INTO @tmptable ([Rechnungsdatum], [Buchungsdatum], [Fälligkeit], [Rechnung], [Auftrag], [Debitorenkonto], [Rechnungskonto], [Debitor], [Debitorenadresse], [Konto], [Kontoname], [Kst], [Kostenstelle], [Kostenträger], [Betrag], [Steuerbetrag], [Buchungstyp], [Steuerreferenz], [Steuersatz], [OP], [S/H])
SELECT convert(nvarchar(10), LT.[TRANSDATE], 104) as 'Rechnungsdatum'
,convert(nvarchar(10), LT.[CREATEDDATETIME], 104) as 'Buchungsdatum'
,convert(nvarchar(10), [DUEDATE], 104) as 'Fälligkeit'
,LT.[VOUCHER] as 'Rechnung'
,'Auftrag' =
CASE
WHEN LEFT(LT.[VOUCHER], 2) = 'MA' THEN substring(LT.TXT, 9, 9)
WHEN LEFT(LT.[VOUCHER], 2) = 'ST' THEN substring(LT.TXT, 15, 9)
ELSE substring(LT.TXT, 13, 8)
END
,[CUSTINVOICEJOUR].[ORDERACCOUNT] as 'Debitorenkonto'
,[INVOICEACCOUNT] as 'Rechnungskonto'
,[CUSTINVOICEJOUR].[DELIVERYNAME] as 'Debitor'
,replace(CUSTINVOICEJOUR.[DELIVERYADDRESS], CHAR(10), ' - ') as 'Debitorenadresse'
,LT.[ACCOUNTNUM] as 'Konto'
,[ACCOUNTNAME] as 'Kontoname'
,'Kst' =
CASE
WHEN LT.[DIMENSION3_] in ('nachtr.-RV','sonst.-RV')
THEN SL.[DIMENSION2_]
ELSE LT.[DIMENSION2_]
END
,'Kostenstelle' =
CASE
WHEN LT.[DIMENSION3_] in ('nachtr.-RV','sonst.-RV')
THEN (SELECT [DIMENSIONS].[DESCRIPTION] FROM [AX2009_PROD].[dbo].DIMENSIONS WHERE [DIMENSIONS].[NUM] = SL.[DIMENSION2_])
ELSE coalesce([DIMENSIONS].[DESCRIPTION], '')
END
,'Kostenträger'=
CASE
WHEN LT.[DIMENSION3_] in ('nachtr.-RV','sonst.-RV')
THEN SL.[DIMENSION3_]
ELSE LT.[DIMENSION3_]
END
,'Betrag' =
CASE
WHEN LT.[DIMENSION3_] in ('nachtr.-RV','sonst.-RV')
THEN convert(numeric(15,2),SL.[SALESPRICE])
ELSE convert(numeric(15,2),LT.[AMOUNTCUR])
END
,convert(numeric(15,2),
CASE
WHEN LT.[DIMENSION3_] in ('nachtr.-RV','sonst.-RV') THEN SL.[SALESPRICE] * coalesce(LEFT(CIT.[PRINTCODE], LEN(CIT.[PRINTCODE])-1), 0) / 100
WHEN LT.[TAXREFID] = '0' OR LT.[VOUCHER] not in
(SELECT LT1.[VOUCHER]
FROM @ledgtrans AS LT1
WHERE LT1.[ACCOUNTNUM] in ('12110','12121','14110')
) THEN 0
ELSE TaxTr.Steuer
END) as 'Steuerbetrag'
,LT.[TRANSTYPE] as 'Buchungstyp'
,LT.[TAXREFID] as 'Steuerreferenz'
,'Steuersatz' =
CASE
WHEN LT.[TAXREFID] = '0' or LT.[VOUCHER] not in
(SELECT LT2.[VOUCHER]
FROM @ledgtrans AS LT2
WHERE LT2.[ACCOUNTNUM] in ('12110','12121','14110')
) THEN ''
ELSE coalesce(CIT.[PRINTCODE], '')
END
,'OP' =
CASE
WHEN LT.[VOUCHER] in
(SELECT LT3.[VOUCHER]
FROM @ledgtrans AS LT3
WHERE LT3.[ACCOUNTNUM] in ('12110','12121','14110')
) THEN '1'
ELSE '3'
END
,'SH' = CASE WHEN [CREDITING] = 0 THEN 's' ELSE 'h' END
FROM @ledgtrans AS LT
left outer join [AX2009_PROD].[dbo].LEDGERTABLE
ON LT.ACCOUNTNUM = LEDGERTABLE.ACCOUNTNUM and LT.DATAAREAID = LEDGERTABLE.DATAAREAID
left outer join [AX2009_PROD].[dbo].DIMENSIONS
ON LT.[DIMENSION2_] = DIMENSIONS.NUM and LT.DATAAREAID = DIMENSIONS.DATAAREAID
left outer join @taxtran2 CIT
ON LT.VOUCHER = CIT.VOUCHER and LT.TRANSDATE = CIT.TRANSDATE
and LT.DIMENSION3_ = CIT.[DIMENSION3_] and LT.DATAAREAID = CIT.DATAAREAID
inner join [AX2009_PROD].[dbo].CUSTINVOICEJOUR
ON LT.VOUCHER = CUSTINVOICEJOUR.LEDGERVOUCHER and LT.DATAAREAID = CUSTINVOICEJOUR.DATAAREAID
left outer join @salestempline AS SL
ON LT.DIMENSION3_ = 'nachtr.-RV' and CUSTINVOICEJOUR.SALESID = SL.SALESID
left outer join @taxtran3 TaxTr
ON LT.VOUCHER = TaxTr.VOUCHER and LT.[AMOUNTCUR] = TaxTr.Basis and LT.[DIMENSION3_] = TaxTr.[DIMENSION3_] and LT.[ACCOUNTNUM] = TaxTr.OPERATIONACCOUNT and LT.[TAXREFID] = TaxTr.[TAXREFID];
SELECT Rechnungsdatum
,Buchungsdatum
,Fälligkeit
,Rechnung
,Auftrag
,Debitorenkonto
,Rechnungskonto
,Debitor
,Debitorenadresse
,Konto
,Kontoname
,Kst
,Kostenstelle
,Kostenträger
,Betrag
,'Steuerbetrag' =
CASE
WHEN ROW_NUMBER() OVER(partition by Rechnung, Steuersatz ORDER BY Betrag DESC) = 1 and Abfrage.Steuersatz != '' THEN Steuerbetrag + (SELECT Betrag FROM @tmptable WHERE Rechnung = Abfrage.Rechnung and Konto = '34515') - (select sum(Steuerbetrag) 'Steuerbetrag' FROM @tmptable WHERE Rechnung = Abfrage.Rechnung)
ELSE Steuerbetrag
END
,Buchungstyp
,Steuerreferenz
,Steuersatz
,OP
,[S/H]
FROM @tmptable AS Abfrage
ORDER BY Auftrag, Rechnung, Konto
查询所做的是获取昨天的分类帐交易,将其与其他表格合并,计算税金并针对四舍五入误差进行调整。在特殊情况下,我不得不对其进行调整以获取一些其他数据,这使得税收计算和更正变得极其缓慢,因此我尝试使用表变量对其进行优化。 我希望获得与Management Studio中相同的数据,但是行为空。