7

Potrzebujesz pomocy w procedurze SQL Server 2000. Problem jest trudny, ponieważ testuję procedurę za pośrednictwem Oracle SQL Developer.Błąd SQL: Niepoprawna składnia w pobliżu słowa kluczowego "Koniec"

Używam procedury do iterowania kolumny z nową sekwencją liczb w formacie Varchar dla tych, którzy mają wartości null.

Ale wciąż dostaję błąd, więc a) Być może zrobiłem niewłaściwe podejście b) składnia jest niepoprawna z powodu używanej wersji. Jestem przede wszystkim użytkownikiem Oracle.

Błąd Ciągle otrzymuję: SQL Error: Incorrect syntax near the keyword 'End'., który nie jest wystarczająco pomocny, aby to naprawić. End odnosi się do ostatniego "Zakończenia" w procedurze.

Każda pomoc zostanie bardzo doceniona.

Oto procedura.

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 

Odpowiedz

8

jesteś brakuje begin tuż po WHILE. Wciskasz tak, jakbyś chciał bloku (wiele instrukcji) w pętli while, a nawet end dla while, ale nie while, ale nie .

uczynić go:

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

Dobrze plamistego. Teraz mam inny błąd. SQL Error: Invalid column name 'MyCur'.. Może moje podejście jest złe. – Joshua

+0

musisz "FETCH NEXT z MyCur INTO @ a". więc musisz wcześniej zadeklarować lokalną zmienną @a, a następnie zmienić ... '+ ', gdzie bieżący z' + MyCur' na ...' + ', gdzie bieżący z' + @ a' –

Powiązane problemy