存储过程无法在sybase

时间:2015-05-18 09:13:40

标签: sql-server stored-procedures sybase sybase-ase

我使用交互式sql在Sybase中创建了一个存储过程,它比较了两个具有相同结构的表中的两行,如果表中任何列的数据有任何差异,那么它将返回该特定行。

Stored procedure is written as below:

CREATE or replace PROCEDURE Validation
 @databaseTo SYSNAME,
@tableTo    SYSNAME,
@tableFrom   SYSNAME
 AS
BEGIN

DECLARE @stmt NVARCHAR(10000)
DECLARE @columnName VARCHAR(100)
DECLARE @nameList VARCHAR(8000)

select @nameList=""
select @stmt=""
select @columnName=""

SET NOCOUNT ON

DECLARE c1 cursor
 for select name 
 from syscolumns 
 where id=(select id 
            from sysobjects 
            where name=@tableFrom)


OPEN c1
FETCH c1
INTO @columnName

while( @@sqlstatus!=2)
BEGIN

    if(@columnName!= "rqst_id")
        BEGIN
            select @nameList=@nameList+"a."+@columnName+"= b."+@columnName+" AND "

        END
 FETCH c1
 INTO @columnName

END
close c1

select @nameList=@nameList+"a.rqst_id=b.rqst_id"

PRINT @nameList
PRINT @tableFrom

select @stmt='select a.* from '+ @tableFrom+' a LEFT JOIN '+ @databaseTo+'.dbo.'+@tableTo+' b on '+ @nameList+' where b.rqst_id IS NULL'
PRINT @stmt
EXEC @stmt

END

此存储过程正确编译但在使用以下语句执行时

EXEC dbo.Validation ('fr2015_dev','fr300output_cost','fr300output_cost')

我收到此错误

  

“无法执行语句   存储过程'从fr300output_cost中选择a。* a a jover JOIN fr2015_dev.dbo.fr300output_cost b on a.carrier_id = b.carrier_id AND   a.rec_num = b.rec_num AND a.amenity_cost = b.amenity_cost AND   a.circuity_cost = b.circuity_cost AND a.lost_utilization_cost =   b.lost_utilization_cost AND a.dest_fuel_cost = b.dest_fuel_cost AND   a.future_stop_cost = b.future_stop_cost AND a.s'未找到。       ...       sp_help可能会产生大量输出。       sybase错误代码2812“

我在运行'sp_help'时看到这个过程。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

这样

EXEC @stmt

你运行一个程序,这就是你错误的原因。

对于run dynamic sql,你必须使用下面的括号

EXEC (@stmt)

您的完整代码应为

CREATE or replace PROCEDURE Validation
 @databaseTo SYSNAME,
@tableTo    SYSNAME,
@tableFrom   SYSNAME
 AS
BEGIN

DECLARE @stmt NVARCHAR(10000)
DECLARE @columnName VARCHAR(100)
DECLARE @nameList VARCHAR(8000)

select @nameList=""
select @stmt=""
select @columnName=""

SET NOCOUNT ON

DECLARE c1 cursor
 for select name 
 from syscolumns 
 where id=(select id 
            from sysobjects 
            where name=@tableFrom)


OPEN c1
FETCH c1
INTO @columnName

while( @@sqlstatus!=2)
BEGIN

    if(@columnName!= "rqst_id")
        BEGIN
            select @nameList=@nameList+"a."+@columnName+"= b."+@columnName+" AND "

        END
 FETCH c1
 INTO @columnName

END
close c1

select @nameList=@nameList+"a.rqst_id=b.rqst_id"

PRINT @nameList
PRINT @tableFrom

select @stmt='select a.* from '+ @tableFrom+' a LEFT JOIN '+ @databaseTo+'.dbo.'+@tableTo+' b on '+ @nameList+' where b.rqst_id IS NULL'
PRINT @stmt
EXEC (@stmt)

END