2014-10-30 32 views
0

我想用兩個臨時表創建SP。但我得到的錯誤「不正確的語法錯誤@BranchID附近的語法錯誤不正確

,我創建的存儲過程:

CREATE PROCEDURE [dbo].[usp_Compare] 
(     
@BranchID INT,     
@FromDate DATE,      
@ToDate DATE,     
@FromDate1 DATE,     
@ToDate1 DATE     
) 
AS 
BEGIN 

If OBJECT_ID('tempdb..#Temp') is not null 
BEGIN 
    DROP TABLE #Temp 
END 

CREATE TABLE #Temp 
(     
@BranchID INT,     
@FromDate DATE,      
@ToDate DATE,     
@FromDate1 DATE,     
@ToDate1 DATE     
) 

Select         
C.CustomerID, C.Name +' - '+c.ShortCode AS CustomerName, ShortCode, SUM(CAST(G.Total AS DECIMAL(18,2))) AS FirstQtrBillingAmount 
INTO #Temp 

FROM GCNNC G with (NOLOCK)        
JOIN Customer C ON C.CUSTOMERID=G.CUSTOMERID 

where G.IsActive=1 AND C.CompanyID=1 AND ([email protected] OR [email protected])           
AND        
(CAST(G.Date AS DATE)>[email protected] OR @FromDate IS NULL) AND        
(CAST(G.Date AS DATE)<[email protected] OR @ToDate IS NULL)        

group by C.CustomerID,C.Name +' - '+c.ShortCode, ShortCode 
ORDER BY C.CustomerID 

If OBJECT_ID('tempdb..#Temp1') is not null 
BEGIN 
    DROP TABLE #Temp1 
END 

CREATE TABLE #Temp1 
(     
@BranchID INT,     
@FromDate DATE,      
@ToDate DATE,     
@FromDate1 DATE,     
@ToDate1 DATE     
) 

Select         
C.CustomerID, C.Name +' - '+c.ShortCode AS CustomerName, ShortCode, SUM(CAST(G.Total AS DECIMAL(18,2))) AS SecondQtrBillingAmount     
INTO #Temp1 

From GCNNC G with (NOLOCK)        
JOIN Customer C ON C.CUSTOMERID=G.CUSTOMERID     

where G.IsActive=1 AND C.CompanyID=1 AND ([email protected] OR [email protected])     
AND        
(CAST(G.Date AS DATE)>[email protected] OR @FromDate1 IS NULL) AND        
(CAST(G.Date AS DATE)<[email protected] OR @ToDate1 IS NULL)        

group by C.CustomerID,C.Name +' - '+c.ShortCode, ShortCode     
ORDER BY C.CustomerID 

SELECT     
CASE WHEN t1.CustomerID IS NULL THEN t2.CustomerID ELSE t1.CustomerID END AS CustomerID,     
CASE WHEN t1.CustomerName IS NULL THEN t2.CustomerName ELSE t1.CustomerName END AS CustomerName,     
CASE WHEN t1.ShortCode IS NULL THEN t2.ShortCode ELSE t1.ShortCode END AS ShortCode,     
Coalesce(FirstQtrBillingAmount,0) AS FirstQtrBillingAmount,Coalesce(SecondQtrBillingAmount,0) AS SecondQtrBillingAmount,      
Coalesce(SecondQtrBillingAmount,0)-Coalesce(FirstQtrBillingAmount,0) AS IncDecAmount,     
CASE     
    WHEN Coalesce(SecondQtrBillingAmount,0) - Coalesce(FirstQtrBillingAmount,0)=0 THEN CAST('0' AS DECIMAL(18,2))     
    WHEN Coalesce(SecondQtrBillingAmount,0) <=0 THEN CAST('-100' AS DECIMAL(18,2))     
    WHEN Coalesce(FirstQtrBillingAmount,0) <=0 THEN CAST('+100' AS DECIMAL(18,2))     
    WHEN Coalesce(SecondQtrBillingAmount,0) >0 THEN CAST(((Coalesce(SecondQtrBillingAmount,0)-Coalesce(FirstQtrBillingAmount,0))/Coalesce(SecondQtrBillingAmount,0)) * 100 AS DECIMAL(18,2))     
END AS IncDecPerc 

FROM #Temp t1     
LEFT JOIN #Temp1 t2     
ON t1.CustomerID = t2.CustomerID 

UNION 

SELECT     
CASE WHEN t1.CustomerID IS NULL THEN t2.CustomerID ELSE t1.CustomerID END AS CustomerID,     
CASE WHEN t1.CustomerName IS NULL THEN t2.CustomerName ELSE t1.CustomerName END AS CustomerName,    
CASE WHEN t1.ShortCode IS NULL THEN t2.ShortCode ELSE t1.ShortCode END AS ShortCode,     
Coalesce(FirstQtrBillingAmount,0) AS FirstQtrBillingAmount,Coalesce(SecondQtrBillingAmount,0) AS SecondQtrBillingAmount,      
Coalesce(SecondQtrBillingAmount,0)-Coalesce(FirstQtrBillingAmount,0) AS IncDecAmount,     
CASE     
    WHEN Coalesce(SecondQtrBillingAmount,0) - Coalesce(FirstQtrBillingAmount,0)=0 THEN CAST('0' AS DECIMAL(18,2))     
    WHEN Coalesce(SecondQtrBillingAmount,0) <=0 THEN CAST('-100' AS DECIMAL(18,2))     
    WHEN Coalesce(FirstQtrBillingAmount,0) <=0 THEN CAST('+100' AS DECIMAL(18,2))     
    WHEN Coalesce(SecondQtrBillingAmount,0) >0 THEN CAST(((Coalesce(SecondQtrBillingAmount,0)-Coalesce(FirstQtrBillingAmount,0))/Coalesce(SecondQtrBillingAmount,0)) * 100 AS DECIMAL(18,2))     
END AS IncDecPerc      

FROM #Temp t1     
RIGHT JOIN #Temp1 t2     
ON t1.CustomerID = t2.CustomerID     

END 
+1

您無法使用「@prefixed」創建臨時表列名稱。那是你得到那個錯誤。它應該是CREATE TABLE#Temp1( BranchID INT,FromDate DATE,...)僅用於聲明變量時需要帶變量名稱的「@prefixed」。 – 2014-10-30 05:16:50

+0

感謝您的建議。我希望這會執行 – user3797553 2014-10-31 05:54:51

回答

3

我還沒有運行存儲過程尚未調試,但我認爲你需要刪除@在臨時表內的列名前面。

取而代之的是:

CREATE TABLE #Temp (
@BranchID INT, 
@FromDate DATE, 
@ToDate DATE, 
@FromDate1 DATE, 
@ToDate1 DATE 
) 

試試這個:

CREATE TABLE #Temp (
BranchID INT, 
FromDate DATE, 
ToDate DATE, 
FromDate1 DATE, 
ToDate1 DATE 
) 

執行相同的#TEMP1。

+0

感謝您的支持。這是行得通的。 – user3797553 2014-10-31 05:55:18

相關問題