How can I check if a stored procedure exists in PERVASIVE database before creating it?

戏子无情 提交于 2020-01-17 06:01:08

问题


I want to create a stored procedure in a Pervasive database, but only if it does not yet exist.

I found something for SQL Server:

IF NOT EXISTS (SELECT * 
               FROM sys.objects 
               WHERE type = 'P' AND OBJECT_ID = OBJECT_ID('dbo.MyProc'))
   exec('CREATE PROCEDURE [dbo].[MyProc] AS BEGIN SET NOCOUNT ON; END')
GO

I found that code on this link How to check if a stored procedure exists before creating it.

So I want something really similar for Pervasive.


回答1:


There's no way to execute the IF NOT EXISTS syntax outside of a Stored Procedure in Pervasive. You could create a procedure taking a procedure name and drop it if it exist. Something like:

CREATE PROCEDURE DropProcIfExists(in :procname char(20))
AS
BEGIN
 IF( EXISTS (SELECT xp$Name FROM X$proc WHERE Xp$Name = :procname) ) THEN
  Exec('DROP Procedure "' + :procname + '"') ;
 END IF;
End#

call DropProcIfExists ('myProc')#

So your SQL would be something like:

call DropProcIfExists('MyNewProc')#
Create Procedure MyNewProc()
AS
BEGIN...


来源:https://stackoverflow.com/questions/37122700/how-can-i-check-if-a-stored-procedure-exists-in-pervasive-database-before-creati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!