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
您无法使用“@prefixed”创建临时表列名称。那是你得到那个错误。它应该是CREATE TABLE#Temp1( BranchID INT,FromDate DATE,...)仅用于声明变量时需要带变量名称的“@prefixed”。 – 2014-10-30 05:16:50
感谢您的建议。我希望这会执行 – user3797553 2014-10-31 05:54:51