7

需要此SQL Server 2000過程的幫助。由於我通過Oracle SQL Developer測試過程,所以問題變得困難。SQL錯誤:關鍵字'End'附近的語法不正確

我正在運行該過程來迭代列以Varchar格式的數字的新序列爲那些誰有空值。

但是我不斷收到錯誤,所以a)我可能做了一個錯誤的方法b)由於使用的版本,語法不正確。我主要是Oracle用戶。

錯誤我一直在收到︰SQL Error: Incorrect syntax near the keyword 'End'.這是沒有幫助足以解決它。 End指的是程序中最後的'結束'。

任何幫助將不勝感激。

這裏是過程。

ALTER PROCEDURE [dbo].[OF_AUTOSEQUENCE] @JvarTable Varchar(250), @varColumn Varchar(250), @optIsString char(1), @optInterval int AS 
/* 
Procedure OF_AUTOSEQUENCE 
Created by Joshua [Surname omitted] 
When  20100902 

Purpose  To fill up column with new sequence numbers 
Arguments varTable - Table name 
      varColumn - Column name 
      optIsString - Option: is it string or numeric, either use T(rue) or F(alse) 
      optInterval - Steps in increment in building new sequence (Should be 1 (one)) 

Example script to begin procedure 

EXECUTE [dbo].[OF_AUTOSEQUENCE] 'dbo.EH_BrownBin', 'Match', 'T', 1 

Any questions about this, please send email to 
[business email omitted] 
*/ 

declare 
@topseed  int, 
@stg_topseed varchar(100), 
@Sql_string nvarchar(4000), 
@myERROR  int,  
@myRowCount int 

set @Sql_string = 'Declare MyCur CURSOR FOR select ' + @varColumn + ' from ' + @JvarTable + ' where ' + @varColumn + ' is null' 
Exec sp_executesql @Sql_string 

SET NOCOUNT ON 

Begin 

    if @optIsString = 'T' 
    Begin 
     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) 
    ENd 
    else 
    Begin 
     set @Sql_string = 'select top 1 ' + @varColumn + ' from ' + @JvarTable + ' order by ' + @varColumn + ' desc' 
     set @topseed = @Sql_string 
    ENd 
-- SELECT @myERROR = @@ERROR, @myRowCOUNT = @@ROWCOUNT 
-- IF @myERROR != 0 GOTO HANDLE_ERROR 


    open MyCur 
    fetch next from MyCur 
    WHILE @@FETCH_STATUS = 0 
    set @topseed = @topseed + @optInterval 
    if @optIsString = 'T' 
     begin 
     set @Sql_string = 'update ' + @JvarTable + ' set ' + @varColumn + ' = cast((' + @topseed + ') as char) where current of ' + MyCur 
     exec (@Sql_string) 
     ENd 
    else 
     begin 
     set @Sql_string = 'update ' + @JvarTable + ' set ' + @varColumn + ' = ' + @topseed + ' where current of ' + MyCur 
     exec (@Sql_string) 
     ENd 
    fetch next from MyCur 
    ENd 
-- SELECT @myERROR = @@ERROR, @myRowCOUNT = @@ROWCOUNT 
-- IF @myERROR != 0 GOTO HANDLE_ERROR 

--HANDLE_ERROR: 
--print @myERROR 

CLOSE MyCur 
DEALLOCATE MyCur 

End 

回答

8

你錯過了begin之後WHILE。你縮進就像你想要一個塊(多條語句)在while循環中,甚至有endwhile,但沒有begin

使其:

... 
    open MyCur 
    fetch next from MyCur 
    WHILE @@FETCH_STATUS = 0 
    begin --<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<add this 
    set @topseed = @topseed + @optInterval 
    if @optIsString = 'T' 
     begin 
     set @Sql_string = 'update ' + @JvarTable + ' set ' + @varColumn + ' = cast((' + @topseed + ') as char) where current of ' + MyCur 
     exec (@Sql_string) 
     ENd 
    else 
     begin 
     set @Sql_string = 'update ' + @JvarTable + ' set ' + @varColumn + ' = ' + @topseed + ' where current of ' + MyCur 
     exec (@Sql_string) 
     ENd 
    fetch next from MyCur 
    ENd 
... 
+0

好斑點。現在我有一個不同的錯誤。 SQL Error: Invalid column name 'MyCur'.。也許我的做法是錯誤的。 – Joshua 2010-09-02 15:12:21

+0

您需要'從MyCur INTO @ a'中取出下一個。所以,你必須先聲明一個局部變量@a,然後將'+ MyCur'的當前值改變爲...'+',其中'+ @ a'的當前值爲'+' – 2010-09-02 15:30:16

相關問題