Showing posts with label procedures. Show all posts
Showing posts with label procedures. Show all posts

Wednesday, March 28, 2012

Incorrect Syntax using IF statement

Hi,

I'm new to SQL Server Programming, I work with ASP a lot, but lately
I've been trying to create Stored Procedures, etc. I'm having a
problem writing a simple IF statement.. I don't seem to understand why
it's giving me this error. I've search around on Google Groups, but I
still don't get it.

=================
USE msdb

IF NOT EXISTS (SELECT * FROM sysjobs WHERE name = 'Scheduled Nightfax')

END
=================

My error is:
Server: Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'END'.

Thanks for any help.you need a BEGIN for every END
example

DECLARE @.v BIT
SELECT @.v = 1

IF @.v = 1
BEGIN
select 'yes'
END
ELSE
BEGIN
select 'No'
END

Or without begin...end
IF @.v = 1
select 'yes'
ELSE
select 'No'

Denis the SQL Menace
http://sqlservercode.blogspot.com/|||Thanks, I played with it a bit and I got the result I was looking for.

==============
USE msdb
DECLARE @.JobName varchar(255)
SELECT @.JobName = name FROM sysjobs WHERE name = 'Scheduled Nightfax'

IF @.JobName = 'Scheduled Nightfax'
PRINT 'YES'
ELSE
PRINT 'NO'
==============

Friday, March 23, 2012

Incorrect syntax near 'E' in SQL Server 2005

We have over 20 stored procedures and they all seem to work fine.
Every once in a while one of the stored procedures will glitch and
return the error, "Incorrect syntax near 'E'." One w it will be one
stored procedure. Another w it will be another. We try to 'solve'
the problem by dropping and recreating the procedure and it works fine
again. (Note that we did not change any syntax in the stored
procedure.) After we recreate the stored procedure it will work a few
times and then it will stop working. It's appears random and we cannot
isolate what is causing it. Simply running it a few times can cause
it.
One time we found out that playing around with the indexes of a column
caused the error (which is very strange.) We have no clue what is
going on.
So now I am attempting to debug it in Visual Studio by attaching a
process to the sqlservr.exe.
Does anyone out there have any idea what is going on? I truly believe
it is a SQL Server bug and not on our end because it is so random and
the syntax of the sp has nothing to do with the error.If this is reproducible, can you post one culprit stored procedure & the
structures of the tables involved?
Anith|||After ws of this issue we have found a solution. Here is the
problem and the solution:
Problem: I have two stored procedures. One (p1) does inserts, and
updates. The second one (p2) just selects. p1 is run before p2. If I
run p1 multiple times before p1 finishes, it will mess up p2 with the
error message (Incorrect syntax near 'E'). This is crazy because the
syntax for p2 never changes.
Solution: For p1 add "WITH RECOMPILE" to the stored procedure. It
forces p1 to compile everytime. Seems to work.