【问题标题】:Stored procedure couldn't execute the entire statement in sybase存储过程无法在 sybase 中执行整个语句
【发布时间】:2015-07-29 17:19:08
【问题描述】:

我使用交互式 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')

我收到此错误

"无法执行语句 存储过程'select a.* from fr300output_cost a LEFT 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”时看到了这个过程。请帮我解决这个问题。

【问题讨论】:

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


    【解决方案1】:

    这边

    EXEC @stmt
    

    您运行了一个程序,因此这就是您出错的原因。

    要运行动态 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
    

    【讨论】:

      猜你喜欢
      • 2017-04-08
      • 2014-12-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多