Groups | Blog | Home
all groups > sql server programming > march 2007 >

sql server programming : Sql Server 2000 GOTO statement


Dave Sexton
3/17/2007 10:27:20 PM
Hi,

I'm hoping someone can verify whether the following T-SQL batch is valid in
Sql Server 2000:

IF 1 = 1
GOTO TEST

RETURN
TEST:
SELECT 'Tested!'

The expected result is:

Tested!

Thanks :)

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)


Adi
3/17/2007 10:30:50 PM
On Mar 18, 6:54 am, "Dave Sexton" <dave@jwa[remove.this]online.com>
[quoted text, click to view]

The T-SQL code that you wrote at the first message is valid code that
should run on the SQL Server with no problems. The error message that
you get was not originated from SQL Server. It was originated from
the editor that you are using.

Adi
Tony Rogerson
3/18/2007 12:00:00 AM
I'd lay money that they are looking for 'GO' in the line and treating GOTO
as a batch seperator.

Tony.

[quoted text, click to view]
Dan Guzman
3/18/2007 12:00:00 AM
I agree with Tony that this is a bug in their "Query Analyser". Error
message "Line 1: Incorrect syntax near '1'." results from executing only:

IF 1 = 1

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
Uri Dimant
3/18/2007 12:00:00 AM
Dave

As Sylvain said this is valid and it does work with BEGIN ....END
IF 1 = 1
BEGIN
GOTO TEST
END

RETURN
TEST:
SELECT 'Tested!'


You will have to provide more info about the script in order to get an
accurate suggestions.





[quoted text, click to view]

Sylvain Lafontaine
3/18/2007 12:11:04 AM
Yes, it's valid. For other control of flow statements with T-SQL, see:

http://www.databasejournal.com/features/mssql/article.php/3361651
http://www.databasejournal.com/features/mssql/article.php/10894_3100621_2

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)


[quoted text, click to view]

Dave Sexton
3/18/2007 12:54:09 AM
Thanks for the reply.

I'm using godaddy.com's Sql Server Web Admin interface and the T-SQL that I
posted doesn't work there. It's been a while, but IIRC I've used this type
of control-of-flow before in 2000, extensively, and I just wanted to be
absolutely sure that it's not my mistake :)

The problem seems to be that GOTO doesn't work within a BEGIN..END block or
even immediately following a WHILE or IF statement, as in my example. It
seems that the only way I can get this to work is using a single line:

IF 1 = 1 GOTO TEST

The original T-SQL that I posted produces the following error:

Error -2147217900
Line 1: Incorrect syntax near '1'.

The godaddy.com tech talked to a supervisor who said that everything in the
shared Sql Server 2000 installation is up-to-date and looks ok, and that
it's something with my code, and that the T-SQL is passed from the web
interface to Sql Server unmodified, but they won't run the code in my
example to see for themselves that it doesn't work.

I have a very large script that uses GOTO statements and I don't want to
rewrite it, especially since I'm not the original author. Actually, it's
not even possible to rewrite it since the script sometimes sets a local
variable and then uses GOTO all within the same BEGIN..END block (you know,
the canonical error-handling stuff).

Has anyone ever experienced an issue like this before? I'm prepared to call
godaddy.com back again and refer them to this thread if need be.

Microsoft SQL Server 2000 - 8.00.2039 (Intel X86)
May 3 2005 23:18:38
Copyright (c) 1988-2003 Microsoft Corporation
Standard Edition on Windows NT 5.2 (Build 3790: )

Thanks again!

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)

"Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
[quoted text, click to view]

Sylvain Lafontaine
3/18/2007 1:47:00 AM
Are you sure that this godaddy.com's Sql Server Web Admin interface is
sending the whole code as a single batch and not line by line?

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)


[quoted text, click to view]

Dave Sexton
3/18/2007 4:10:53 AM
Hi everyone,

Thanks for your replies. I'll address comments and questions here:

- The code I posted *is* the code that is failing, literally. The actual
script
that I need to run isn't going to work if the code I posted doesn't work
either.
- I thought it was the editor's fault as well but godaddy claims that they
don't
modify the T-SQL - it's passed to Sql Server 2000 unmodified, they said.
I
asked them to try the code but they said no, and that was that.
- I had already tried the BEGIN..END code that Uri Dimant posted but that
didn't work either. Here's the error with Uri's code:

Error -2147217900
Line 2: Incorrect syntax near 'BEGIN'.

- The editor is an extremely simplified version of Query Analyzer, called
"Query Analyser" ('s', not 'z'), and executes the entire multi-line text
as a single batch. It uses the GO keyword to separate batches.
For example, the following script:

SET NOCOUNT ON
DECLARE @data TABLE ( num int )
DECLARE @num int
SET @num = 1
WHILE @num < 5 BEGIN
INSERT @data SELECT @num
SET @num = @num + 1
END
SELECT * FROM @data

produces the following output:

num
1
2
3
4
4 record(s) affected.

I'll contact godaddy tomorrow and refer them to this thread; hopefully
they'll pay attention this time. I'll post the result of the call in case
anyone is interested. Again, I just wanted verification so that I wouldn't
waste their time if it was actually my mistake, but it seems to be valid
T-SQL for Sql Server 2000 - thanks.

If anyone has anything else to add please feel free. Also, if there is
anyone with a shared-Sql Server 2000 account on godaddy.com I'd like to know
whether the code in my original post works for you. In the Hosting Manager
for your website click SQL Server under the Databases section. Login and
expand the Tools node. Click "Query Analyser", copy & paste the entire IF 1
= 1... code into the textbox and click Submit. Error or result? If you get
an error try putting GOTO TEST on the same line as IF 1 = 1 and see if that
works (it works for me).

Thanks again :)

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)

[quoted text, click to view]

Dan Guzman
3/18/2007 8:54:31 AM
[quoted text, click to view]

The "Line 1: Incorrect syntax near '1'." error is a syntax error returned by
SQL Server. However, the actual SQL Server error is 170 rather than
the -2147217900 reported by the web tool.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
Dave Sexton
3/18/2007 12:41:27 PM
Hi everyone,

Good call about the GO in GOTO. I'm almost ashamed at myself for not seeing
that :p

Anyway, I tried Tony's suggestion about creating a proc named GOTEST just to
see if I could call it without using EXEC and it seems that you guys are
correct about your assumption:

CREATE PROC GOTEST AS
SELECT 'Proc Executed'
GO
GOTEST
DROP PROC GOTEST

Result:
Error -2147217908
Command text was not set for the command object.

Lol. Thanks again - I'll call godaddy and let them know, but I have a
feeling that this battle is going to be uphill...

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)

[quoted text, click to view]
Dave Sexton
3/18/2007 1:16:56 PM
Hi again,

This issue has been escalated to a godaddy db admin. To my surprise the
woman that I talked to from godaddy.com immediately accepted a link to this
thread, even though the first time I called I was told that the error was
certainly with my code, which they wouldn't even try to run. I guess it
really does matter who you get on the phone :)

Thanks again for all of your help - this thread may help them to resolve the
issue.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)

[quoted text, click to view]
Sylvain Lafontaine
3/18/2007 1:46:53 PM
Wild guess here: it's possible that they look for the word GO only when they
are the first two caracters of a line, so you should try *hiding* your GOTO
by adding the test IF 1=1 just before it each time.

If this work, peppering your code with this test won't change its current
logic; so you won't have to re-test it or worse, to rewrite it using other
control flow statements.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)


[quoted text, click to view]
Dave Sexton
3/18/2007 2:15:02 PM
Hi Sylvain,

Lol. That is a good idea, for a hack :)

I opened up the very large script in Management Studio and used the
following Find and Replace regular expression:

Replace "<GOTO " with "IF 1=1 GOTO "

95 occurrences.

That did the trick, so I don't have to wait for them to fix Query Analyser.
Thanks!

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)

"Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
[quoted text, click to view]
Tony Rogerson
3/18/2007 3:55:12 PM
To prove it, create a stored procedure called something like gototestproc
that writes the current_timestamp to a table and try running it I bet you
the stored procedure never runs.

Tony.

[quoted text, click to view]
Dave Sexton
3/20/2007 9:36:55 PM
Hi,

I just wanted to let the newsgroup know that godaddy.com emailed me to claim
responsibility for the *bug*, which is the term they used to describe the
issue, and they said that they "are currently working on a fix that will be
deployed to correct this issue". They also offered a workaround that I
don't think is as helpful as Sylvain's so it's probably not worth
mentioning.

Thanks again to everyone who posted :)

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)

[quoted text, click to view]
AddThis Social Bookmark Button