| 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' |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-04-23 : 06:03:48
|
| no parenthesis round PRINTtry just PRINT 'Success' insteadCharlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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 tryPRINT ''Success''Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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 tryPRINT ''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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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. |
 |
|
|
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. |
 |
|
|
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. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-23 : 08:16:38
|
| show your query firstalso did you see my question?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|