Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Error: Incorrect syntax near the keyword 'else'.

Author  Topic 

upani
Starting Member

12 Posts

Posted - 2010-04-23 : 06:00:14
Hi all,
I'm new in sql, i've written a code that check if a trigger named 'Hotel' exists or not. If it doesn't exist then creating it.
But the result returns error like this:
Incorrect syntax near 'Success'.

Here my code



if not exists (select * from sys.triggers where name = 'Hotel')
begin
EXEC ( 'create trigger Hotel on Registry for insert as
begin
print('Success')
end')
end
else
begin
print('Exists')
end

upani
Starting Member

12 Posts

Posted - 2010-04-23 : 06:02:50
So can you tell me how to solve this problem?
Thanks!

I'm sorry about the name of the Topic,
Incorrect syntax near 'Success' insteads of Incorrect syntax near 'else'

Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-23 : 06:03:48
no parenthesis round PRINT

try just PRINT 'Success' instead



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-23 : 06:04:57
But your trigger is completely pointless. Is this a stub?

Also -- just noticed it's inside a dynamic block so try

PRINT ''Success''


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-23 : 06:06:02
quote:
Originally posted by Transact Charlie

But your trigger is completely pointless. Is this a stub?

Also -- just noticed it's inside a dynamic block so try

PRINT ''Success''

-- NB: That is two single quotes in succession rather than double quote. this is how you escape the ' in strings.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION





Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-23 : 06:11:12
i cant understand why you need dynamic sql here

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-04-23 : 06:11:32
Inside your EXEC() you have to mask the single quotes by another quote.
print(''Success'')


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-04-23 : 06:12:26
The old rocker was too slow again...



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

upani
Starting Member

12 Posts

Posted - 2010-04-23 : 08:13:39

Thanks all but when I try all the ways you told me, there's still error:
The name "Success" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-23 : 08:16:38
show your query first

also did you see my question?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -