Stored procedures SQL Server:无游标存储过程中出错

Stored procedures SQL Server:无游标存储过程中出错,stored-procedures,sql-server-2000,Stored Procedures,Sql Server 2000,我在这个存储过程中遇到了问题,你能帮我一下吗 这是我得到的一个错误——通过其他地方托管的SQLServer2000上的Oracle Sql developer运行所有这些 错误 从命令中的第1行开始出错: 执行dbo.OF_ASEQ_EH_BROWNBIN'dbo.EH_BROWNBIN_Josh','Match',1 错误报告: 关键字“BEGIN”附近的语法不正确。 程序 而在Oracle中,您需要DECLARE-BEGIN-END,而在MSSQL中,您不必在过程主体上使用BEGIN/EN

我在这个存储过程中遇到了问题,你能帮我一下吗

这是我得到的一个错误——通过其他地方托管的SQLServer2000上的Oracle Sql developer运行所有这些

错误 从命令中的第1行开始出错: 执行dbo.OF_ASEQ_EH_BROWNBIN'dbo.EH_BROWNBIN_Josh','Match',1 错误报告: 关键字“BEGIN”附近的语法不正确。

程序


而在Oracle中,您需要DECLARE-BEGIN-END,而在MSSQL中,您不必在过程主体上使用BEGIN/END关键字

请注意,标签句柄\u错误:位于开始/结束之外

我的猜测是,删除DECLARE块之后的BEGIN和SELECT@myERROR=…之前的END,错误就会消失

编辑:

我想我没有得到以下陈述:

set @Sql_string = 'select top 1 ' + @varColumn + ' from ' + @JvarTable + ' order by convert(int, ' + @varColumn + ') desc'   
set @stg_topseed =  @Sql_string  
set @topseed = convert(int, @stg_topseed) 

分配一个字符串变量,将该值复制到另一个字符串变量,然后将该字符串变量转换为int,而不是执行SQL语句。

该代码中有几个错误,可能有更好的、基于集合的方法来解决此存储过程的问题。但这是另一个问题,需要了解未提供的目标表

下面的代码应该更接近您要查找的内容。 变化:

编辑可疑代码,将“++@topseed+”转换为char该列真的是char吗?或者是什么? 修正了@topseed fetch的逻辑。 更正了有关导入表使用的逻辑。 已更正更新SQL生成。 删除无关的开始/结束。 删除无关的HTML。 包含开始/结束的已包装过程。 最后加了围棋。 代码:


我一直在StackOverflow上做家务,我意识到我没有或没有接受我很久以前提出的问题的答案。我记不起我决定采用哪种方法/工作方法,但我觉得上传我已接受的工作脚本很重要,尽管我不再需要它

然而,我真诚地希望这段代码能帮助下一个和我有类似问题的人

CREATE PROCEDURE [dbo].[OF_ASEQ_EH_BROWNBIN] @JvarTable nvarchar(250), @varColumn nvarchar(250), @optInterval int AS
/*
Procedure   OF_ASEQ_EH_BROWNBIN
Created by  Joshua White
When        20100902

Purpose     To fill up column with new sequence numbers
Arguments   JvarTable   - Table name
            varColumn   - Column name
            optInterval - Steps in increment in building new sequence (Should be 1 (one))

Example script to begin procedure

EXECUTE [dbo].[OF_ASEQ_EH_BROWNBIN] 'EH_Brownbin_Josh', 'Match', 1

PLEASE NOTE - Typically Stored Procedures are supposed to be flexible to work on
any tables, but due to complications with SQL Server 2000 and problems with
Cursors, we have to use manual scripts and this Stored Procedure will only work
on EH_BrownBin table ONLY.

Any questions about this, please send email to
<email deleted>
*/

declare   @Sql_string   nvarchar(4000)
declare   @myERROR      int    
declare   @myRowCount   int

/* Fetching the last number in rows of table in question */
declare   @topseed      int
declare   @stg_topseed  varchar(100)

-- Temp table for rows with nulls in specific column
declare   @RowCnt       int
declare   @MaxRows      int
declare   @rc           int 
declare   @colu_Name     nvarchar(250)
declare   @colu_UPRN     nvarchar(250)
declare   @colu_JoinedOn smalldatetime

Begin
  set @stg_topseed = 'select top 1 ' + @varColumn + ' from ' + @JvarTable + ' order by convert(int, ' + @varColumn + ') desc'
  exec (@stg_topseed)

/* Begin collecting all rows with nulls in specified column */
select @RowCnt = 1

declare @Import table
(
  rownum int IDENTITY (1, 1) Primary key NOT NULL ,
  colu_Name nvarchar(250),
  colu_UPRN nvarchar(250),
  colu_JoinedOn smalldatetime
) 
set nocount on
select @MaxRows = count(*) from @Import

-- Next new seed
select @stg_topseed = @stg_topseed + @optInterval
select @rc=0 

while @RowCnt <= @MaxRows
  begin 
    select @colu_Name = colu_Name from @Import where rownum = @RowCnt
    select @colu_UPRN = colu_UPRN from @Import where rownum = @RowCnt
    select @colu_JoinedOn = colu_JoinedOn from @Import where rownum = @RowCnt

    set @Sql_string = 'update ' + @JvarTable + ' set ' + @varColumn + ' = ' + @stg_topseed + ' where Name = ''' + @colu_Name + ''' and UPRN = ''' + @colu_UPRN + ''' '
    exec (@Sql_string)
    select @stg_topseed = @stg_topseed + @optInterval
    Select @RowCnt = @RowCnt + 1 
  end

print 'END'
end

可以稍微清理一下代码;很难说格式是什么样子的。这可能是因为你在同一行上有一个BEGIN和SET,也可能是打字错误。它也可能是第一个ORDERBY子句,但正如您所显示的,它相当混乱。代码现在更整洁了-非常感谢@Martin Smith。感谢@LittleBobbyTables指出这一点。有人能找出我为什么仍然犯BEGIN错误吗?谢谢。问题在于表的原始设置-有一列具有序列号,但为字符串格式,因此需要找到最顶端的数字,然后将其转换为整数,以便在同一表中更新列中具有空值的行时,可以在每个循环中添加1。删除BEGIN and END everywhere=相同错误,因此,我强烈猜测Oracle SQL Developer工具不是在SQL Server 2000上调试新存储过程的最合适工具。可能需要访问该站点并在盒子上使用查询分析器。谢谢@Devio,我已经将程序的结尾处更正了。最后,我认为我使用了错误的工具来分析查询。这看起来不错,我将尝试一下。虽然昨天我访问了该站点并对其进行了操作(错误与我通过Oracle SQL Developer获得的错误不同),但得出结论认为该工具显然不是在非现场操作SQL Server 2000的正确工具。我将给出新提供的代码,以便下次在那里试用。非常感谢您向@Brock Adams提出的快速问题-如何在找到工作时解决我提出的问题。尽管如此,我还是要试试你的密码,给你一个信任。@Joshua:你是在问如何将这个问题标记为已回答吗?如果是这样,只需单击空心复选标记即可。在答案的左上角,投票箭头的下方。如果你问别的问题,我不清楚是什么。
ALTER PROCEDURE [dbo].[OF_ASEQ_EH_BROWNBIN]
    @JvarTable Varchar(250),
    @varColumn Varchar(250),
    @optInterval int
AS
BEGIN
    DECLARE   @Sql_string   nvarchar(4000)
    DECLARE   @myERROR      int
    DECLARE   @myRowCount   int
    DECLARE   @topseed      int
    DECLARE   @stg_topseed  varchar(100)

    -- Temp table for rows with nulls in specific column
    DECLARE   @RowCnt       int
    DECLARE   @MaxRows      int
    DECLARE   @col_Name     nvarchar(250)
    DECLARE   @col_UPRN     nvarchar(250)
    DECLARE   @col_JoinedOn smalldatetime

    SET @Sql_string     = 'select top 1 @stg_topseed = ' + @varColumn + ' from ' + @JvarTable + ' order by convert(int, ' + @varColumn + ') desc'
    EXEC SP_EXECUTESQL @Sql_string, N'@stg_topseed varchar(100) OUTPUT', @stg_topseed OUTPUT

    SELECT @myERROR = @@ERROR, @myRowCOUNT = @@ROWCOUNT
    IF @myERROR != 0 GOTO HANDLE_ERROR

    SET @topseed        = CONVERT(int, @stg_topseed)

    /* Can't use a table variable with EXEC and/or SP_EXECUTESQL.
        Therefore, forced to use a temporary table.
    */
    IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id('#Import') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
        DROP TABLE #Import

    CREATE table #Import
    (
        rownum          int IDENTITY (1, 1) Primary key NOT NULL,
        col_Name        nvarchar(250),
        col_UPRN        nvarchar(250),
        col_JoinedOn    smalldatetime
    )

    SET  @sql_string = 'insert into #Import (col_Name, col_UPRN, col_JoinedOn) select Name, UPRN, JoinedOn from ' + @JvarTable + ' where ' + @varColumn +' is null'
    EXEC (@Sql_string)

    SELECT @myERROR = @@ERROR, @myRowCOUNT = @@ROWCOUNT
    IF @myERROR != 0 GOTO HANDLE_ERROR

    SELECT @MaxRows=count(*) from #Import

    SELECT @myERROR = @@ERROR, @myRowCOUNT = @@ROWCOUNT
    IF @myERROR != 0 GOTO HANDLE_ERROR

    -- Next new seed
    SELECT @topseed = @topseed + @optInterval
    SELECT @RowCnt = 1

    WHILE @RowCnt <= @MaxRows
      BEGIN
        SELECT @col_Name = col_Name from #Import where rownum = @RowCnt
        SELECT @col_UPRN = col_UPRN from #Import where rownum = @RowCnt
        SELECT @col_JoinedOn = col_JoinedOn from #Import where rownum = @RowCnt

        SET    @Sql_string = 'update ' + @JvarTable + ' set ' + @varColumn + ' = ''' + CAST (@topseed AS varchar(250)) + '''  where Name = ''' + @col_Name + ''' and UPRN = ''' + @col_UPRN + ''' and JoinedOn = ''' + CAST(@col_JoinedOn AS varchar(250)) + ''' '
        EXEC  (@Sql_string)

        SELECT @topseed = @topseed + @optInterval
        SELECT @RowCnt = @RowCnt + 1
      END

    SELECT  @myERROR = @@ERROR, @myRowCOUNT = @@ROWCOUNT
    IF      @myERROR != 0 GOTO HANDLE_ERROR

    HANDLE_ERROR:
    RETURN @myERROR
END
GO
CREATE PROCEDURE [dbo].[OF_ASEQ_EH_BROWNBIN] @JvarTable nvarchar(250), @varColumn nvarchar(250), @optInterval int AS
/*
Procedure   OF_ASEQ_EH_BROWNBIN
Created by  Joshua White
When        20100902

Purpose     To fill up column with new sequence numbers
Arguments   JvarTable   - Table name
            varColumn   - Column name
            optInterval - Steps in increment in building new sequence (Should be 1 (one))

Example script to begin procedure

EXECUTE [dbo].[OF_ASEQ_EH_BROWNBIN] 'EH_Brownbin_Josh', 'Match', 1

PLEASE NOTE - Typically Stored Procedures are supposed to be flexible to work on
any tables, but due to complications with SQL Server 2000 and problems with
Cursors, we have to use manual scripts and this Stored Procedure will only work
on EH_BrownBin table ONLY.

Any questions about this, please send email to
<email deleted>
*/

declare   @Sql_string   nvarchar(4000)
declare   @myERROR      int    
declare   @myRowCount   int

/* Fetching the last number in rows of table in question */
declare   @topseed      int
declare   @stg_topseed  varchar(100)

-- Temp table for rows with nulls in specific column
declare   @RowCnt       int
declare   @MaxRows      int
declare   @rc           int 
declare   @colu_Name     nvarchar(250)
declare   @colu_UPRN     nvarchar(250)
declare   @colu_JoinedOn smalldatetime

Begin
  set @stg_topseed = 'select top 1 ' + @varColumn + ' from ' + @JvarTable + ' order by convert(int, ' + @varColumn + ') desc'
  exec (@stg_topseed)

/* Begin collecting all rows with nulls in specified column */
select @RowCnt = 1

declare @Import table
(
  rownum int IDENTITY (1, 1) Primary key NOT NULL ,
  colu_Name nvarchar(250),
  colu_UPRN nvarchar(250),
  colu_JoinedOn smalldatetime
) 
set nocount on
select @MaxRows = count(*) from @Import

-- Next new seed
select @stg_topseed = @stg_topseed + @optInterval
select @rc=0 

while @RowCnt <= @MaxRows
  begin 
    select @colu_Name = colu_Name from @Import where rownum = @RowCnt
    select @colu_UPRN = colu_UPRN from @Import where rownum = @RowCnt
    select @colu_JoinedOn = colu_JoinedOn from @Import where rownum = @RowCnt

    set @Sql_string = 'update ' + @JvarTable + ' set ' + @varColumn + ' = ' + @stg_topseed + ' where Name = ''' + @colu_Name + ''' and UPRN = ''' + @colu_UPRN + ''' '
    exec (@Sql_string)
    select @stg_topseed = @stg_topseed + @optInterval
    Select @RowCnt = @RowCnt + 1 
  end

print 'END'
end