gpt4 book ai didi

sql - 无效的对象名称 - 存储过程

转载 作者:行者123 更新时间:2023-12-02 09:17:00 25 4
gpt4 key购买 nike

我正在通过 SSMS 在 SQL Server 中创建一个存储过程。

我已经编写了下面的存储过程,但是当我单击执行时,出现错误:

消息 208,级别 16,状态 6,程序 NewQuestion,第 11 行对象名称“hgomez.NewQuestion”无效。

该表的所有权是正确的。 (hgomez.问题)

USE [devworks_oscar]
GO
/****** Object: StoredProcedure [hgomez].[NewQuestion] Script Date: 10/23/2011 23:55:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [hgomez].[NewQuestion]
(
@QUESTIONNAME nvarchar(50),
@QUESTION_ID int OUTPUT
)

AS
/* SET NOCOUNT ON */
INSERT INTO [Questions] (QuestionText) VALUES (@QUESTIONNAME)
SET @QUESTION_ID = SCOPE_IDENTITY();
RETURN

提前致谢

最佳答案

我喜欢总是在 CREATE 语句前明确检查是否存在,如果找到则删除。

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'NewQuestion' AND ROUTINE_SCHEMA = 'hgomez')
BEGIN
DROP PROCEDURE hgomez.NewQuestion
END
GO

-- this is always a CREATE
CREATE PROCEDURE [hgomez].[NewQuestion]
(
@QUESTIONNAME nvarchar(50),
@QUESTION_ID int OUTPUT
)

AS
/* SET NOCOUNT ON */
INSERT INTO [Questions] (QuestionText) VALUES (@QUESTIONNAME)
SET @QUESTION_ID = SCOPE_IDENTITY();
RETURN

这在权限方面可能会有点麻烦,因此其他人使用一种方法,他们创建一个 stub 方法只是为了立即ALTER它。

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'NewQuestion' AND ROUTINE_SCHEMA = 'hgomez')
BEGIN
EXEC ('CREATE PROCEDURE hgomez.NewQuestion AS SELECT ''stub version, to be replaced''')
END
GO

-- This is always ALTER
ALTER PROCEDURE [hgomez].[NewQuestion]
(
@QUESTIONNAME nvarchar(50),
@QUESTION_ID int OUTPUT
)

AS
/* SET NOCOUNT ON */
INSERT INTO [Questions] (QuestionText) VALUES (@QUESTIONNAME)
SET @QUESTION_ID = SCOPE_IDENTITY();
RETURN

关于sql - 无效的对象名称 - 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7870501/

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