Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Error: Incorrect syntax near the keyword 'End'

Need help with this SQL Server 2000 procedure. The problem is made difficult because I'm testing procedure via Oracle SQL Developer.

I'm running the procedure to iterate column with new sequence of numbers in Varchar format for those who have null values.

But I keep getting error, so a) I may have done a wrong approach b) syntax is incorrect due to version used. I'm primarily Oracle user.

Error I keep getting: SQL Error: Incorrect syntax near the keyword 'End'. which isn't helpful enough to fix it out. The End refers to the very last 'End' in the procedure.

Any help would be greatly appreciated.

Here's the Procedure.

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
like image 593
Joshua Avatar asked Sep 02 '10 14:09

Joshua


1 Answers

you're missing a begin right after the WHILE. You indented like you want a block (multiple statements) in the while loop, and even have a end for the while, but no begin.

make it:

...
  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
...
like image 177
KM. Avatar answered Nov 07 '22 05:11

KM.