@BranchID附近的语法错误不正确

时间:2014-10-30 05:01:17

标签: sql sql-server sql-server-2008

我想用两个临时表创建SP。但是我收到错误' 语法错误错误'

我创建的商店程序:

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 (G.BranchID=@BranchID OR 0=@BranchID)                                        
AND                               
(CAST(G.Date AS DATE)>=@FromDate OR @FromDate IS NULL) AND                               
(CAST(G.Date AS DATE)<=@ToDate  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 (G.BranchID=@BranchID OR 0=@BranchID)                   
AND                               
(CAST(G.Date AS DATE)>=@FromDate1 OR @FromDate1 IS NULL) AND                               
(CAST(G.Date AS DATE)<=@ToDate1   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 个答案:

答案 0 :(得分: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做同样的事。