MS SQL存储过程问题

时间:2009-07-01 18:53:08

标签: sql-server tsql

我有一个存储过程可以在我的本地SQL Server(2005或2008无法回想起)上正常工作但在我尝试在生产服务器(SQL 2000)上创建过程时失败。 任何帮助,将不胜感激。 TIA。

存储过程声明是这样的:

/****** 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.

6 个答案:

答案 0 :(得分:2)

这应该在2000盒子上创建没问题(我通过在我的sql 2000盒子上创建验证)。您确定您的数据库不是7.0兼容模式吗?

运行

sp_helpdb 'YourDatabaseName'

并查看兼容性是否为80

答案 1 :(得分: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

的工作原理。希望它也适合你;)

答案 2 :(得分:0)

这是我前一段时间提出的一个问题(Link)中引用的问题,所以如果它对你有用,那么就应该支持Mike L'而不是我的回应。

如果使用Database Publishing Wizard为SP创建脚本,则可以在2005年构建它们并使用它来部署到2000,让向导处理您可能遇到的任何兼容性问题。

答案 3 :(得分:0)

导致问题的不是@符号,它是括号@a而不是[@a]。

答案 4 :(得分:0)

已经有一段时间但我似乎记得SQL 2000要求您在update语句和子查询中引用表时使用别名。希望这有助于您找到解决方案。

答案 5 :(得分:0)

where子句中的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