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
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 TRY/CATCH and CREATE PROC

Author  Topic 

ravilobo
Master Smack Fu Yak Hacker

1184 Posts

Posted - 2009-06-01 : 12:46:43
Why the following code generates error?

BEGIN TRY
create procedure foo as select getdate()
END TRY


BEGIN CATCH
select 'err'
END CATCH


------------------------
I think, therefore I am - Rene Descartes

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-01 : 13:40:58
create procedure cannot be combined with any other commands


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

ravilobo
Master Smack Fu Yak Hacker

1184 Posts

Posted - 2009-06-01 : 13:47:40
Okay, thank you - webfred.

------------------------
I think, therefore I am - Rene Descartes
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-01 : 13:54:42
can i ask what you're trying to implement by above code?
Go to Top of Page

ravilobo
Master Smack Fu Yak Hacker

1184 Posts

Posted - 2009-06-01 : 14:03:21
quote:
Originally posted by visakh16

can i ask what you're trying to implement by above code?



I am just trying to create a proc.

1. I know I can use IF EXISTS ….DROP AND THEN CREATE. But for some reason if this code fails I need to know.
2. I can not wrap the SP definition in a string (that could solve my problem) because the proc has lost of double and single quotes.

I am thinking of using @@EROR


------------------------
I think, therefore I am - Rene Descartes
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-06-01 : 15:03:12
maybe check for existence after the CREATE statement batch. If it fails for whatever reason the @@error code may not even execute.

Be One with the Optimizer
TG
Go to Top of Page

ravilobo
Master Smack Fu Yak Hacker

1184 Posts

Posted - 2009-06-01 : 15:06:51
quote:
Originally posted by TG

maybe check for existence after the CREATE statement batch. If it fails for whatever reason the @@error code may not even execute.



That's a good suggestion. Thanks.

------------------------
I think, therefore I am - Rene Descartes
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-06-02 : 02:10:20
quote:
Originally posted by ravilobo

quote:
Originally posted by visakh16

can i ask what you're trying to implement by above code?



I am just trying to create a proc.

1. I know I can use IF EXISTS ….DROP AND THEN CREATE. But for some reason if this code fails I need to know.
2. I can not wrap the SP definition in a string (that could solve my problem) because the proc has lost of double and single quotes.

I am thinking of using @@EROR


------------------------
I think, therefore I am - Rene Descartes



Refer this to know how single quotes work in sql
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -