由于变量表名称,我创建了一个变量SQL查询,在其中声明了要使用的SQL查询。我们根据系统中的帐户名设置了SQL。这意味着它看起来像这样:[LSRetail].[dbo].[AccountName$Item]
。
此处LSRetail
部分是动态的,因为如果它是丹麦,则将其称为LSRetailDK
;如果是瑞典,则将其称为LSRetailSE
。 AccountName
也是动态的,因为商店名称和邮政编码不同...
所有这些实际上都有效,并被声明并设置为@Sql
,但是由于空间原因,我想创建另一个DECLARE @Sql2 VARCHAR(8000)
,在其中我想输入SELECT * FROM @Sql
。由于@Sql
以with cte1 AS (
语句开头,所以不允许我这样做。
可以在这里做点什么吗?我想在@Sql2
或@Sql3
中将两者结合起来。现在,唯一有效的方法是SET @Sql2 = @Sql
并执行该操作。
下面的代码是我想在@Sql
中使用的@Sql2
的代码。
DECLARE @OrderNo VARCHAR(50);
DECLARE @StoreName VARCHAR(100);
DECLARE @Table1 VARCHAR(1000);
DECLARE @Table2 VARCHAR(1000);
DECLARE @Table3 VARCHAR(1000);
DECLARE @Table4 VARCHAR(1000);
DECLARE @Table5 VARCHAR(1000);
DECLARE @Table6 VARCHAR(1000);
DECLARE @Table7 VARCHAR(1000);
DECLARE @Table8 VARCHAR(1000);
DECLARE @Table9 VARCHAR(1000);
DECLARE @Database VARCHAR(1000);
DECLARE @BulkDiscount VARCHAR(1000);
DECLARE @DisclaimerDiscount VARCHAR(1000);
DECLARE @CashDiscount VARCHAR(1000);
DECLARE @Loyalty VARCHAR(1000);
DECLARE @Orders VARCHAR(8000);
SET @Database = '[LSRetail]'
SET @StoreName = 'AccountName';
SET @Orders = '''TI0573057'', ''TI0572704'', ''TI0572541'', ''TI0573058'', ''TI0572983'', ''TI0572595'', ''TI0573516'', ''TI0572841'', ''TI0573175'', ''TI0572759'', ''TI0572861'', ''TI0572917'', ''TI0568408'', ''TI0573077'', ''TI0572641'', ''TI0570757'', ''TI0573419'', ''TI0573394'', ''TI0572893'', ''TI0573029'', ''TI0573317'', ''TI0572581'', ''TI0570652'', ''TI0573061'', ''TI0573340'', ''TI0572417'', ''TI0572769'', ''TI0573315'', ''TI0572975'', ''TI0573304'', ''TI0573381'', ''TI0572976'', ''TI0572862'', ''TI0572886'', ''TI0573615'', ''TI0573049'', ''TI0572811'', ''TI0573156'', ''TI0573222'', ''TI0573095'', ''TI0573023''';
SET @BulkDiscount = 2.5/100
SET @DisclaimerDiscount = 1.5/100
SET @CashDiscount = 0.0/100
SET @Loyalty = 5.3/100
SET @Table1 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Sales Invoice Line]');
SET @Table2 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Sales Invoice Header]');
SET @Table3 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Item]');
SET @Table4 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Sales Cr_Memo Line]');
SET @Table5 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Sales Cr_Memo Header]');
SET @Table6 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Purchase Inv_ Header]');
SET @Table7 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Purchase Inv_ Line]');
SET @Table8 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Purchase Cr_ Memo Hdr_]');
SET @Table9 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Purchase Cr_ Memo Line]');
DECLARE @Sql VARCHAR(8000);
SET @Sql =
'
WITH cte1 AS(
SELECT a.[Document No_]
,''Invoice'' AS [Document Type]
,b.[Unique Document No_]
,a.[Type]
,a.[No_]
,c.[Vendor Item No_]
,a.[Gen_ Prod_ Posting Group]
,a.[VAT Bus_ Posting Group]
,a.[VAT Prod_ Posting Group]
,a.[Description]
,a.[Item Order Group Code]
,a.[Quantity]
,a.[Unit of Measure]
,a.[Unit Cost (LCY)]
,a.[Unit Price]
,a.[Line Amount]
,a.[Line Discount %]
,a.[Line Amount]/(a.[VAT %]/100+1)*0.96 AS [Total Line Sales]
,CASE WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] = ''OVT00100'' AND a.[Item Order Group Code] <> ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity] * (1-' + @DisclaimerDiscount + ') * (1-' + @BulkDiscount + ') * (1-' + @CashDiscount + ') * (1-' + @Loyalty + ')
WHEN a.[No_] LIKE ''SM%'' OR a.[No_] LIKE ''LE%'' AND a.[No_] LIKE ''%99999''
THEN a.[Unit Cost (LCY)]*a.[Quantity]
WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] <> ''OVT00100'' AND a.[Item Order Group Code] <> ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity] * (1-' + @CashDiscount + ') * (1-' + @Loyalty + ')
WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] = ''OVT00100'' AND a.[Item Order Group Code] = ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity]
END AS [Total Line Cost]
FROM ' + @Table1 + ' a
LEFT JOIN ' + @Table2 + ' b ON a.[Document No_] = b.[No_]
LEFT JOIN ' + @Table3 + ' c ON a.[No_] = c.[No_]
WHERE b.[Prepayment Invoice] = 0
),
cte2 AS(
SELECT a.[Document No_]
,''Credit Memo'' AS [Document Type]
,b.[Unique Document No_]
,a.[Type]
,a.[No_]
,c.[Vendor Item No_]
,a.[Gen_ Prod_ Posting Group]
,a.[VAT Bus_ Posting Group]
,a.[VAT Prod_ Posting Group]
,a.[Description]
,a.[Item Order Group Code]
,a.[Quantity] * (-1) AS [Quantity]
,a.[Unit of Measure]
,a.[Unit Cost (LCY)]
,a.[Unit Price]
,a.[Line Amount]
,a.[Line Discount %]
,a.[Line Amount]/(a.[VAT %]/100+1)*0.96 AS [Total Line Sales]
,CASE WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] = ''OVT00100'' AND a.[Item Order Group Code] <> ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity] * (1-' + @DisclaimerDiscount + ') * (1-' + @BulkDiscount + ') * (1-' + @CashDiscount + ') * (1-' + @Loyalty + ')
WHEN a.[No_] LIKE ''SM%'' OR a.[No_] LIKE ''LE%'' AND a.[No_] LIKE ''%99999''
THEN a.[Unit Cost (LCY)]*a.[Quantity]
WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] <> ''OVT00100'' AND a.[Item Order Group Code] <> ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity] * (1-' + @CashDiscount + ') * (1-' + @Loyalty + ')
WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] = ''OVT00100'' AND a.[Item Order Group Code] = ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity]
END AS [Total Line Cost]
FROM ' + @Table4 + ' a
LEFT JOIN ' + @Table5 + ' b ON a.[Document No_] = b.[No_]
LEFT JOIN ' + @Table3 + ' c ON a.[No_] = c.[No_]
WHERE b.[Prepayment Credit Memo] = 0
),
cte3 AS(
SELECT *
FROM cte1
UNION ALL
SELECT *
FROM cte2
)
SELECT *
,CASE WHEN [Total Line Sales] <> 0
THEN ([Total Line Sales] - [Total Line Cost])
ELSE 0
END AS [Margin]
,CASE WHEN [Total Line Sales] <> 0
THEN ([Total Line Sales] - [Total Line Cost])/[Total Line Sales]
ELSE 0
END AS [Margin %]
FROM cte3
WHERE [No_] <> ''''
AND [Unique Document No_] IN (' + @Orders + ')
ORDER BY [Unique Document No_], [Item Order Group Code]
'
谢谢您的帮助。
注意:我使用SQL Server。
我找到了解决问题的方法...
通过删除最后一个SELECT
语句,我可以在第二个变量中使用它。
DECLARE @OrderNo VARCHAR(50);
DECLARE @StoreName VARCHAR(100);
DECLARE @Table1 VARCHAR(1000);
DECLARE @Table2 VARCHAR(1000);
DECLARE @Table3 VARCHAR(1000);
DECLARE @Table4 VARCHAR(1000);
DECLARE @Table5 VARCHAR(1000);
DECLARE @Table6 VARCHAR(1000);
DECLARE @Table7 VARCHAR(1000);
DECLARE @Table8 VARCHAR(1000);
DECLARE @Table9 VARCHAR(1000);
DECLARE @Database VARCHAR(1000);
DECLARE @BulkDiscount VARCHAR(1000);
DECLARE @DisclaimerDiscount VARCHAR(1000);
DECLARE @CashDiscount VARCHAR(1000);
DECLARE @Loyalty VARCHAR(1000);
DECLARE @Orders VARCHAR(8000);
SET @Database = '[LSRetail]'
SET @StoreName = 'AccountName';
SET @Orders = '''TI0573057'', ''TI0572704'', ''TI0572541'', ''TI0573058'', ''TI0572983'', ''TI0572595'', ''TI0573516'', ''TI0572841'', ''TI0573175'', ''TI0572759'', ''TI0572861'', ''TI0572917'', ''TI0568408'', ''TI0573077'', ''TI0572641'', ''TI0570757'', ''TI0573419'', ''TI0573394'', ''TI0572893'', ''TI0573029'', ''TI0573317'', ''TI0572581'', ''TI0570652'', ''TI0573061'', ''TI0573340'', ''TI0572417'', ''TI0572769'', ''TI0573315'', ''TI0572975'', ''TI0573304'', ''TI0573381'', ''TI0572976'', ''TI0572862'', ''TI0572886'', ''TI0573615'', ''TI0573049'', ''TI0572811'', ''TI0573156'', ''TI0573222'', ''TI0573095'', ''TI0573023''';
SET @BulkDiscount = 2.5/100
SET @DisclaimerDiscount = 1.5/100
SET @CashDiscount = 0.0/100
SET @Loyalty = 5.3/100
SET @Table1 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Sales Invoice Line]');
SET @Table2 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Sales Invoice Header]');
SET @Table3 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Item]');
SET @Table4 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Sales Cr_Memo Line]');
SET @Table5 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Sales Cr_Memo Header]');
SET @Table6 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Purchase Inv_ Header]');
SET @Table7 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Purchase Inv_ Line]');
SET @Table8 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Purchase Cr_ Memo Hdr_]');
SET @Table9 = CONCAT(@Database,'.[dbo].[','' + @StoreName + '','$Purchase Cr_ Memo Line]');
DECLARE @Sql VARCHAR(8000);
SET @Sql =
'
WITH cte1 AS(
SELECT a.[Document No_]
,''Invoice'' AS [Document Type]
,b.[Unique Document No_]
,a.[Type]
,a.[No_]
,c.[Vendor Item No_]
,a.[Gen_ Prod_ Posting Group]
,a.[VAT Bus_ Posting Group]
,a.[VAT Prod_ Posting Group]
,a.[Description]
,a.[Item Order Group Code]
,a.[Quantity]
,a.[Unit of Measure]
,a.[Unit Cost (LCY)]
,a.[Unit Price]
,a.[Line Amount]
,a.[Line Discount %]
,a.[Line Amount]/(a.[VAT %]/100+1)*0.96 AS [Total Line Sales]
,CASE WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] = ''OVT00100'' AND a.[Item Order Group Code] <> ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity] * (1-' + @DisclaimerDiscount + ') * (1-' + @BulkDiscount + ') * (1-' + @CashDiscount + ') * (1-' + @Loyalty + ')
WHEN a.[No_] LIKE ''SM%'' OR a.[No_] LIKE ''LE%'' AND a.[No_] LIKE ''%99999''
THEN a.[Unit Cost (LCY)]*a.[Quantity]
WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] <> ''OVT00100'' AND a.[Item Order Group Code] <> ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity] * (1-' + @CashDiscount + ') * (1-' + @Loyalty + ')
WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] = ''OVT00100'' AND a.[Item Order Group Code] = ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity]
END AS [Total Line Cost]
FROM ' + @Table1 + ' a
LEFT JOIN ' + @Table2 + ' b ON a.[Document No_] = b.[No_]
LEFT JOIN ' + @Table3 + ' c ON a.[No_] = c.[No_]
WHERE b.[Prepayment Invoice] = 0
),
cte2 AS(
SELECT a.[Document No_]
,''Credit Memo'' AS [Document Type]
,b.[Unique Document No_]
,a.[Type]
,a.[No_]
,c.[Vendor Item No_]
,a.[Gen_ Prod_ Posting Group]
,a.[VAT Bus_ Posting Group]
,a.[VAT Prod_ Posting Group]
,a.[Description]
,a.[Item Order Group Code]
,a.[Quantity] * (-1) AS [Quantity]
,a.[Unit of Measure]
,a.[Unit Cost (LCY)]
,a.[Unit Price]
,a.[Line Amount]
,a.[Line Discount %]
,a.[Line Amount]/(a.[VAT %]/100+1)*0.96 AS [Total Line Sales]
,CASE WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] = ''OVT00100'' AND a.[Item Order Group Code] <> ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity] * (1-' + @DisclaimerDiscount + ') * (1-' + @BulkDiscount + ') * (1-' + @CashDiscount + ') * (1-' + @Loyalty + ')
WHEN a.[No_] LIKE ''SM%'' OR a.[No_] LIKE ''LE%'' AND a.[No_] LIKE ''%99999''
THEN a.[Unit Cost (LCY)]*a.[Quantity]
WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] <> ''OVT00100'' AND a.[Item Order Group Code] <> ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity] * (1-' + @CashDiscount + ') * (1-' + @Loyalty + ')
WHEN a.[No_] NOT LIKE ''SM%'' OR a.[No_] NOT LIKE ''LE%'' AND a.[No_] NOT LIKE ''%99999'' AND a.[Item Order Group Code] = ''OVT00100'' AND a.[Item Order Group Code] = ''OVT00500''
THEN a.[Unit Cost (LCY)]*a.[Quantity]
END AS [Total Line Cost]
FROM ' + @Table4 + ' a
LEFT JOIN ' + @Table5 + ' b ON a.[Document No_] = b.[No_]
LEFT JOIN ' + @Table3 + ' c ON a.[No_] = c.[No_]
WHERE b.[Prepayment Credit Memo] = 0
),
cte3 AS(
SELECT *
FROM cte1
UNION ALL
SELECT *
FROM cte2
)
'
@DECLARE @Sql2 VARCHAR(8000);
SET @Sql2 =
@Sql + '
SELECT *
,CASE WHEN [Total Line Sales] <> 0
THEN ([Total Line Sales] - [Total Line Cost])
ELSE 0
END AS [Margin]
,CASE WHEN [Total Line Sales] <> 0
THEN ([Total Line Sales] - [Total Line Cost])/[Total Line Sales]
ELSE 0
END AS [Margin %]
FROM cte3
WHERE [No_] <> ''''
AND [Unique Document No_] IN (' + @Orders + ')
ORDER BY [Unique Document No_], [Item Order Group Code]
'