gpt4 book ai didi

sql-server - 如何检查 sql server 对象是否存在并删除它?

转载 作者:行者123 更新时间:2023-12-03 09:48:06 25 4
gpt4 key购买 nike

编辑 : 函数创建丢失,抱歉

我有一个 T-SQL 请求:

DECLARE @IsSomething bit
SET @IsSomething = 0
IF /some tests/ SET @IsSomething = 1
EXEC('
CREATE FUNCTION IsSomething ()
RETURNS bit
AS
BEGIN
RETURN ' + @IsSomething + '
END')

当然,如果我运行它两次,我会得到
There is already an object named 'IsSomething ' in the database. 

我将如何做这样的事情:
IF EXIST @IsSomething DESTROY @IsSomething // (Pseudo bad code)

最佳答案

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[IsSomething]') AND OBJECTPROPERTY(id, N'IsFunction') = 1) 

DROP function IsSomething
GO

关于sql-server - 如何检查 sql server 对象是否存在并删除它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1140203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com