2009-07-01 551 views
3

我有一個存儲過程在我的本地SQL Server(2005或2008無法重新召回)上正常工作,但當我嘗試在生產服務器(SQL 2000)上創建過程時失敗。 任何幫助,將不勝感激。 TIA。MS SQL存儲過程問題

存儲過程的聲明是這樣的:

/****** Object: StoredProcedure [dbo].[AssignPCSCheckNumbers] Script Date: 06/29/2009 13:12:24 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE PROCEDURE [dbo].[AssignPCSCheckNumbers]   
(   
    @MonthEnd DATETIME,   
    @Seed  INT,   
    @ManifestKey UNIQUEIDENTIFIER,   
    @Threshold DECIMAL(9,2)   
)   

AS   

SET NOCOUNT ON   

BEGIN   


--Create a temporary table variable to store our data   
DECLARE @MyTemp TABLE 
( 
    ProducerNumber VARCHAR(20), 
    LastCheckDate DATETIME, 
    Due DECIMAL(9,2) DEFAULT 0, 
    Returned DECIMAL(9,2) DEFAULT 0   
) 

--Fill the table with a listing of producers from our PCSItems table and their ACH Status 
INSERT INTO @MyTemp (ProducerNumber) 
SELECT  PCSItems.ProducerNumber 
FROM  PCSItems 
LEFT JOIN Producer 
ON   PCSItems.ProducerNumber = Producer.prodNum 
WHERE  ISNULL(Producer.PayCommissionByACH,0) = 0 

--UPDATE the table with the last time a check was printed for each 
--of these producers 
UPDATE  @MyTemp 
SET   LastCheckDate = (
SELECT  ISNULL(MAX(EntryDate),'1/1/1901') 
FROM  CommissionLedger WITH (NOLOCK) 
WHERE  CommissionLedger.TransactionType = 1 
AND   CommissionLedger.ProducerNumber = [@MyTemp].ProducerNumber 
) 


--update the table with the amount of comission owed to each producer   
UPDATE  @MyTemp   
SET   Due = (
SELECT  IsNull(SUM(CommPaid),0)   
FROM  ProducerComm WITH (NOLOCK)   
WHERE  ProducerComm.CommApplies = [@MyTemp].ProducerNumber 
AND   ProducerComm.EntryDate >= LastCheckDate 
AND   ProducerComm.EntryDate <= @MonthEnd 
)   

--update the table with the amount of commission returned by each producer        
UPDATE  @MyTemp   
SET   Returned = (
SELECT  ISNULL(SUM(Amount), 0) 
FROM  CommissionLedger WITH (NOLOCK)   
WHERE  CommissionLedger.ProducerNumber = [@MyTemp].ProducerNumber   
AND   CommissionLedger.EntryDate >= [@MyTemp].LastCheckDate   
AND   CommissionLedger.EntryDate <= @MonthEnd 
) 

--create a table to assist with our operations   
DECLARE @MyFinal TABLE 
(
    ID INT IDENTITY(1,1),   
    ProducerNumber VARCHAR(10)   
) 

--just insert the producers that are owed an amount over a user specified   
--threshold   
INSERT INTO @MyFinal (ProducerNumber)   
SELECT  ProducerNumber   
FROM  @MyTemp   
WHERE  (Due + Returned) > @Threshold   

--update our items with the check numbers finally =)   
UPDATE  PCSItems   
SET   CheckNumber = (SELECT (([@MyFinal].ID - 1) + @Seed)  
          FROM @MyFinal   
          WHERE [@MyFinal].ProducerNumber = PCSItems.ProducerNumber)   

SET NOCOUNT OFF   

END 
GO 

而且服務器響應的錯誤是這樣的:

Msg 107, Level 16, State 2, Procedure AssignPCSCheckNumbers, Line 35 
The column prefix '@MyTemp' does not match with a table name or alias name used in the query. 
Msg 107, Level 16, State 2, Procedure AssignPCSCheckNumbers, Line 45 
The column prefix '@MyTemp' does not match with a table name or alias name used in the query. 
Msg 107, Level 16, State 2, Procedure AssignPCSCheckNumbers, Line 55 
The column prefix '@MyTemp' does not match with a table name or alias name used in the query. 
Msg 107, Level 16, State 2, Procedure AssignPCSCheckNumbers, Line 55 
The column prefix '@MyTemp' does not match with a table name or alias name used in the query. 
Msg 107, Level 16, State 2, Procedure AssignPCSCheckNumbers, Line 79 
The column prefix '@MyFinal' does not match with a table name or alias name used in the query. 
Msg 107, Level 16, State 2, Procedure AssignPCSCheckNumbers, Line 79 
The column prefix '@MyFinal' does not match with a table name or alias name used in the query. 
+1

不應該在SET BEGIN中設置「SET NOCOUNT ON」嗎? – GalacticCowboy 2009-07-01 19:09:43

回答

2

這應該在2000箱創建任何問題(我通過驗證在我的sql 2000框中創建它)。你確定你的數據庫不在7.0兼容模式嗎?

運行

sp_helpdb 'YourDatabaseName'

和看,如果兼容性是80

+0

是的,它是80兼容的。 – 2009-07-01 19:08:10

+0

也在我的SQL 2000框中工作過。 – MattH 2009-07-01 19:14:49

+0

我的存儲過程創建在你的SQL2000上工作?有趣。我不知道有什麼區別。 – 2009-07-01 19:38:18

0

這是鬆散的,從我提出的問題前一段時間(Link)報價,所以如果你的作品,給予好評邁克L的反應,而不是我的。

如果您使用Database Publishing Wizard爲SP創建腳本,則可以在2005年構建它們並使用它將其部署到2000,從而讓該向導處理您可能遇到的任何兼容性問題。

2

我不知道2000支持表變量,因爲我在我的第一個答案懷疑。

現在我試圖在查詢分析器,發現@table從這導致錯誤消息「無效的對象名@table」 [@table]不同的處理。

我建議從@表名刪除方括號。

更新:

This page表示以表的別名可能會解決問題。我剛剛嘗試過:

UPDATE @a SET a = a + b FROM @a INNER JOIN @b ON @a.a = @b.b 

失敗並顯示錯誤。重寫爲

UPDATE @a SET a = a + b FROM @a aa INNER JOIN @b bb ON aa.a = bb.b 

的作品。希望它爲你工作太;)

0

這不是導致該問題的@符號,它的支架@a不是[@a。

0

這已經有一段時間,但我似乎記得SQL 2000需要你的時候,你在這兩個更新語句,並在子查詢中引用該表使用的別名。希望這有助於您找到解決方案。where子句中

0

AFAIK表不能用名字引用的,你應該爲自己的搜索條件別名爲此,MSSQL2000書網上,我們看到:

<search_condition>:= 
    { constant 
    | scalar_function 
    | [ alias. ] column 
    | local_variable 
    | (expression) 
    | (scalar_subquery) 
    | { unary_operator } expression 
    | expression { binary_operator } expression 
    } 

,正如你看到的,有沒有table or alias並且它只是alias