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 2000 Forums
 Transact-SQL (2000)
 If & Create Procedure statements?

Author  Topic 

Mike@Syntex
Starting Member

3 Posts

Posted - 2002-09-11 : 11:13:31
I'd like to use an If statement to create a stored procedure if the procedure does not exist. Does SQL Server support this or am I limited to dropping the procedure using the If statement and then creating the procedure, irregardless of the results of the If statement testing for the procedure's existance?

TIA!

JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2002-09-11 : 11:39:12
If you want to create the procedure without dropping it in the event that it already exists why not just trap the error and bypass the need for the if statement.

Justin

Have you hugged your SQL Server today?
Go to Top of Page

Mike@Syntex
Starting Member

3 Posts

Posted - 2002-09-11 : 11:59:21
It needs to be in a simple txt install script run via OSQL.


Mike

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2002-09-11 : 12:13:24
The create statement needs to be the first statement in a batch

i.e.

if...
create procedure
will not work

if...
go
create procedure
will though.

You could call an osql statement to add the single SP depending on the if statement

if object_id('spname') is not null
exec master..xp_cmdshell 'osql ...'

this is the way some release scripts work but usually you want to recrate the SP anyway just in case something has changed.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Mike@Syntex
Starting Member

3 Posts

Posted - 2002-09-11 : 13:32:13
Actually that didn't work either. But building the create as a string and executing the string did work.


Thanks anyway.

Mike

Go to Top of Page
   

- Advertisement -