简体   繁体   English

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

[英]Invalid Object Name - Stored Procedure

I am creating a stored procedure in SQL Server via SSMS. 我正在通过SSMS在SQL Server中创建存储过程。

I have written the stored procedure below, however when I click execute it am given the error: 我已经编写了下面的存储过程,但是当我单击执行它时会出现错误:

Msg 208, Level 16, State 6, Procedure NewQuestion, Line 11 Invalid object name 'hgomez.NewQuestion'. Msg 208,Level 16,State 6,Procedure NewQuestion,Line 11无效的对象名称'hgomez.NewQuestion'。

the table is ownership is correct. 表是所有权是正确的。 (hgomez.Questions) (hgomez.Questions)

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

Thanks in advance 提前致谢

I was a fan of always prepending my CREATE statements with an explicit check for existence and dropping if it was found. 我总是在我的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

That can be a bit of hassle with regard to permissions so others use an approach wherein they create a stub method only to immediately ALTER it. 这可有点麻烦,对于权限,以便其他人使用,其中,他们创建一个存根方法的方法只有立即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

This script tries to modify a procedure that already exists; 此脚本尝试修改已存在的过程; it doesn't create the procedure. 它不会创建过程。

To create the procedure use CREATE PROCEDURE 要创建该过程,请使用CREATE PROCEDURE

CREATE PROCEDURE [hgomez].[NewQuestion]

Once the procedure exists, you can modify its definition by using ALTER PROCEDURE 该过程存在后,您可以使用ALTER PROCEDURE修改其定义

ALTER PROCEDURE [hgomez].[NewQuestion]

This solution https://stackoverflow.com/a/26775310/2211788 explained 该解决方案https://stackoverflow.com/a/26775310/2211788解释

If you drop and re-create a stored procedure it gets a new objectid - the list of stored procedures in SSMS is linked to the id it knows at the time the list was built. 如果你删除并重新创建一个存储过程,它会获得一个新的objectid - SSMS中的存储过程列表链接到它在构建列表时知道的id。 If you re-create it but don't refresh the stored procedures folder then any attempts to edit it will indicate the procedure is not found as the id has changed. 如果您重新创建它但不刷新存储过程文件夹,那么任何编辑它的尝试都将指示在ID已更改时找不到该过程。

This happened to me once when I had two instances of SSMS open and I was working on the one I opened first. 当我打开两个SSMS实例并且我正在处理我首先打开的实例时,这发生在我身上。 Closed them both down, reopened and it worked fine. 将它们关闭,重新打开并且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM