视图中关键字“ DECLARE”附近的语法不正确

时间:2019-05-30 11:23:04

标签: sql-server tsql

有人可以确认如何消除以下错误。

DECLARE @StartDate DATETIME, @EndDate DATETIME
SET @StartDate = DATEADD(mm, DATEDIFF(mm, 0, getdate()) - 1, 0)
SET @EndDate = DATEADD(mm, 1, @StartDate)
SELECT        
dbo.General_Ledger_Detail.Accounting_ID,
dbo.General_Ledger_Detail.Cost_Centre,
dbo.General_Ledger_Detail.Product_ID, 

dbo.General_Ledger_Detail.Accounted_Amount AS Amount, 
dbo.General_Ledger_Detail.Account_Name, 
dbo.General_Ledger_Detail.Accounting_Date, 

dbo.Account_Codes_Sales_OPEX$.[Opex Type], 
dbo.LogSolOpexCC.Logistic_Solutions_Type

FROM            
dbo.General_Ledger_Detail 
INNER JOIN dbo.Account_Codes_Sales_OPEX$ 
ON dbo.General_Ledger_Detail.Accounting_ID = 
    dbo.Account_Codes_Sales_OPEX$.[Account Code] 
INNER JOIN dbo.LogSolOpexCC 
ON dbo.General_Ledger_Detail.Cost_Centre = dbo.LogSolOpexCC.Cost_Centre

GROUP BY dbo.General_Ledger_Detail.Accounting_ID, 
dbo.General_Ledger_Detail.Cost_Centre, 
dbo.General_Ledger_Detail.Product_ID, 

dbo.General_Ledger_Detail.Accounted_Amount, 
dbo.General_Ledger_Detail.Account_Name, 
dbo.General_Ledger_Detail.Accounting_Date, 

dbo.Account_Codes_Sales_OPEX$.[Opex Type],     
dbo.LogSolOpexCC.Logistic_Solutions_Type

HAVING (dbo.General_Ledger_Detail.Accounting_Date BETWEEN @startdate AND @enddate)

2 个答案:

答案 0 :(得分:0)

SELECT        dbo.General_Ledger_Detail.Accounting_ID, dbo.General_Ledger_Detail.Cost_Centre, dbo.General_Ledger_Detail.Product_ID, 
                         dbo.General_Ledger_Detail.Accounted_Amount AS Amount, dbo.General_Ledger_Detail.Account_Name, dbo.General_Ledger_Detail.Accounting_Date, 
                         dbo.Account_Codes_Sales_OPEX$.[Opex Type], dbo.LogSolOpexCC.Logistic_Solutions_Type
FROM            dbo.General_Ledger_Detail INNER JOIN
                         dbo.Account_Codes_Sales_OPEX$ ON dbo.General_Ledger_Detail.Accounting_ID = dbo.Account_Codes_Sales_OPEX$.[Account Code] INNER JOIN
                         dbo.LogSolOpexCC ON dbo.General_Ledger_Detail.Cost_Centre = dbo.LogSolOpexCC.Cost_Centre
GROUP BY dbo.General_Ledger_Detail.Accounting_ID, dbo.General_Ledger_Detail.Cost_Centre, dbo.General_Ledger_Detail.Product_ID, 
                         dbo.General_Ledger_Detail.Accounted_Amount, dbo.General_Ledger_Detail.Account_Name, dbo.General_Ledger_Detail.Accounting_Date, 
                         dbo.Account_Codes_Sales_OPEX$.[Opex Type], dbo.LogSolOpexCC.Logistic_Solutions_Type
HAVING        (dbo.General_Ledger_Detail.Accounting_Date BETWEEN DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0) AND DATEADD(mm, 1, GETDATE()))

答案 1 :(得分:0)