0

为什么在创建或更改时以下SP不会生成错误? 我在数据库'AdventureWorks2014'中不存在的以下SP中使用了一个标量函数[dbo.fn_General_GetCurrentTime()]SQL server - 在创建之前检查SP中是否存在对象

我很困惑它是否应该给出错误。 有什么办法可以强制它检查对象的存在吗?

USE [AdventureWorks2014] 
GO 

SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

Create PROCEDURE [dbo].[SP_Zee_Test] 
    @ID int 
AS 
BEGIN 
    Declare @DateC DateTime 

    Set @DateC = dbo.fn_General_GetCurrentTime() --this function is not exists in database 
END 

GO 
+0

人都在问了几十年的一些方法来禁用延迟名称解析,没有任何东西被迹象尽快实施的任何时间。这是一个“功能”。 –

回答

0

您可以使用元数据表检查数据库中是否存在对象。对于存储过程和函数可以使用表INFORMATION_SCHEMA.ROUTINES

--check if a stored procedure exists 
if exists (SELECT 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE ='PROCEDURE' and ROUTINE_SCHEMA='yourSpSchema' and ROUTINE_NAME='yourSpName') 
    begin 
     --the stored procedure exists 
    end 
else 
    begin 
     --the stored procedure does not exist 
    end 

--check if a function exists 
if exists (SELECT 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE ='FUNCTION' and ROUTINE_SCHEMA='yourFxSchema' and ROUTINE_NAME='yourFxName') 
    begin 
     --the function exists 
    end 
else 
    begin 
     --the function does not exist 
    end 
相关问题