English 中文(简体)
如何在建立储存程序之前检查是否存在储存程序
原标题:How to check if a stored procedure exists before creating it

我有一张卡片,每当客户履行“数据库管理”功能时,必须操作。 该书包括建立客户数据库储存的程序。 其中一些客户可能已经拥有管理该书的存储程序,有些客户可能没有。 我需要把所储存的失踪程序添加到客户数据库中,但是,我为什么试图把T-SQL syntax带走,我却拿到了什么东西。

CREATE/ALTER PROCEDURE 必须是在询问中的第一个发言。

我在开始施工之前就读到这种退步,但我不喜欢这样做。

IF EXISTS (SELECT * FROM sys.objects WHERE type =  P  AND name =  MyProc )
DROP PROCEDURE MyProc
GO

CREATE PROCEDURE MyProc
...

如果存在某种储存程序,如果存在这种程序,但如果存在这种程序,我怎么能够补充检查程序的存在并制造这种程序?

最佳回答

在你能够提出质询的地方,你可以操作程序法。

仅复制AS后的所有材料:

BEGIN
    DECLARE @myvar INT
    SELECT  *
    FROM    mytable
    WHERE   @myvar ...
END

该法典与所储存的胎盘完全相同,但并不储存在数据库中。

这与<代码>PL/SQL中所谓的匿名程序非常相似。

<>Update:

你的问答题是一点点混淆。

如果你只需要制定程序,那么,你的法典只是罚款。

此处为:

IF EXISTS ( SELECT  *
            FROM    sys.objects
            WHERE   object_id = OBJECT_ID(N myproc )
                    AND type IN ( N P , N PC  ) ) 
DROP …
CREATE …

<>Update:

在列入计划时如何做到:

IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N [dbo].[MyProc] ) 
                   and OBJECTPROPERTY(id, N IsProcedure ) = 1 )
BEGIN
    DROP PROCEDURE [dbo].[MyProc]
END

如上所示,dbo是该表。

<>Update:

在2016年+服务器中,你只能做事。

CREATE OR ALTER PROCEDURE dbo. MyProc

问题回答

我已经认识到这一点,但我们这样做了:

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

ALTER PROCEDURE [dbo].[MyProc] 
AS
  ....

避免放弃程序。

如果你在删除数据库之前重新寻找最简单的方式来检查数据库物体的存在,那么,就采取某种方式(例如使用SPROC,如上例,但可以修改表格、索引等):

IF (OBJECT_ID( MyProcedure ) IS NOT NULL)
  DROP PROCEDURE MyProcedure
GO

这是迅速和合法的,但需要确保你拥有所有物体的独一无二的物体名称,因为它没有考虑到这一点。

我知道,你希望“如果存在危险物质,就建立这种程序,但我认为这样做更简单:

  1. Drop the procedure (if it already exists) and then
  2. Re-create it.

与此类似:

IF OBJECT_ID( MyProcedure ,  P ) IS NOT NULL
    DROP PROCEDURE MyProcedure
GO

CREATE PROCEDURE MyProcedure AS
BEGIN
    /* ..... */
END
GO

第二项参数计数如下:>OBJECT_ID。 储存程序:

AF = 聚合功能(CLR)

C = CHECK制约因素

D = 权力下放(制约或独立)

F = 签字的限制

FN = 食力功能

FS=大会(CLR)

FT = 大会(CLR)表格价值的职能

缩略语

IT = 内部表格

P = 储存程序

PC = 储存的保有权

PG = 计划指南

PK = 初步关键制约因素

R = 规则(旧式、独立)

RF = 重复-过滤-procedure

S = 系统基数表

SN = 地名

SO = 序号

TF = 表格估价功能

TR = Trigger

可以通过下列途径获得全部备选办法清单:

SELECT name 
FROM master..spt_values
WHERE type =  O9T 

As of SQL SERVER 2016 you can use the new DROP PROCEDURE IF EXISTS.
DROP { PROC | PROCEDURE } [ IF EXISTS ] { [ schema_name. ] procedure } [ ,...n ]

Reference : https://msdn.microsoft.com/en-us/library/ms174969.aspx

我知道这是一个非常老的员额,但由于这出现在最高级的搜索结果中,因此增加了使用<>SQL服务器2016 SP1的人的最新信息。 -

create or alter procedure procTest
as
begin
 print (1)
end;
go

如果不存在,则会产生一种储存程序,但如果存在,则会改变。

https://msdn.microsoft.com/library/ms187926.aspx”rel=“noreferer”>Reference

我有同样的错误。 我知道,这种read子已经死亡,但除了“匿名程序”之外,我还要提出另一种选择。

我解决了这一问题:

  1. 是否存在所储存的程序:

    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name= my_procedure ) BEGIN
        print  exists   -- or watever you want
    END ELSE BEGIN
        print  doesn  texists    -- or watever you want
    END
    
  2. However the "CREATE/ALTER PROCEDURE must be the first statement in a query batch" is still there. 我解决了这一问题:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE -- view procedure function or anything you want ...
    
  3. 最后,我要指出:

    IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID( my_procedure ))
    BEGIN
        DROP PROCEDURE my_procedure
    END
    
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE PROCEDURE [dbo].my_procedure ...
    

这里是使用这种方法的一种方法和一些理由。 这与储存的胎盘相对应,但存在利弊。

UPDATE:你也可以在一份译文中填满这一整个呼吁。 包括许多储存在单项交易中的程序,这些交易可以全部进行或全部进行。 交易中总结的另一个好处是,只要不使用READ UNCOMMITTED交易孤立程度,存储程序就一直存在其他链接。

(1) 避免作为进程决定而改变。 我们的工作永远都是通过国际自由论坛进行的。 如果你采取同样的模式,选择新的PROC,那么满足变化的要求就更困难了,因为你会有一个国际自由论坛。

2) 您必须把CREATE/ALTER作为首批呼吁,以便你能够在动态外的交易中总结一系列程序更新。 从根本上说,如果你想要掌握一整套程序,更新或将其全部撤回,而不恢复非行的备份,就是在单一批次中做一切事的一种方式。

IF NOT EXISTS (select ss.name as SchemaName, sp.name as StoredProc 
    from sys.procedures sp
    join sys.schemas ss on sp.schema_id = ss.schema_id
    where ss.name =  dbo  and sp.name =  MyStoredProc )
BEGIN
    DECLARE @sql NVARCHAR(MAX)

    -- Not so aesthetically pleasing part. The actual proc definition is stored
    -- in our variable and then executed.
    SELECT @sql =  CREATE PROCEDURE [dbo].[MyStoredProc]
(
@MyParam int
)
AS
SELECT @MyParam 
    EXEC sp_executesql @sql
END

在2008年之后的Sql服务器中,你可以使用“。 资料。 ROUTINES

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.ROUTINES 
  WHERE ROUTINE_NAME =  MySP 
        AND ROUTINE_TYPE =  PROCEDURE ) 

** 减少和重新计算T-Sql储存的胎盘的最简单途径是 **

Use DatabaseName
go
If Object_Id( schema.storedprocname ) is not null
begin
   drop procedure schema.storedprocname
end
go

create procedure schema.storedprocname
as

begin
end

这就是我使用的文字。 因此,我避免不必要地放弃和收回所储存的物料。

IF NOT EXISTS (
    SELECT *
    FROM sys.objects
    WHERE object_id = OBJECT_ID(N [dbo].[uspMyProcedure] )
    )
BEGIN
  EXEC sp_executesql N CREATE PROCEDURE [dbo].[uspMyProcedure] AS select 1 
END
GO

ALTER PROCEDURE [dbo].[uspMyProcedure] 
    @variable1 INTEGER  
AS
BEGIN
   -- Stored procedure logic
END

为什么不像你那样简单

    IF EXISTS(SELECT * FROM sys.procedures WHERE NAME LIKE  uspBlackListGetAll )
    BEGIN
         DROP PROCEDURE uspBlackListGetAll
    END
    GO

    CREATE Procedure uspBlackListGetAll

......

我 wonder! 为什么不写整个问题。

GO
create procedure [dbo].[spAddNewClass] @ClassName varchar(20),@ClassFee int
as
begin
insert into tblClass values (@ClassName,@ClassFee)
end

GO
create procedure [dbo].[spAddNewSection] @SectionName varchar(20),@ClassID       int
as
begin
insert into tblSection values(@SectionName,@ClassID)
end

Go
create procedure test
as
begin 
select * from tblstudent
end

i already know that first two procedures are already exist sql will run the query will give the error of first two procedures but still it will create the last procedure SQl is itself taking care of what is already exist this is what i always do to all my clients!

CREATE Procedure FI NOT EXISTS 页: 1 BEGIN ... END





相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...