简体   繁体   English

在SQL Server 2008中更改存储过程的名称

[英]Change name of stored procedure in SQL Server 2008

I have a stored procedure which I edit through Visual Studio 2008. Is there a simple way to change the name of the stored procedure? 我有一个存储过程,我通过Visual Studio 2008编辑。是否有一种简单的方法来更改存储过程的名称? Right now if I look at the sproc's properties, the name of the sproc is grayed out. 现在,如果我查看sproc的属性,sproc的名称将变灰。

EXEC sp_rename OLDNAME, NEWNAME

这假设您可以通过VS2008执行SQL语句。

If you have SSMS, you can right-click on the procedure name and choose rename. 如果您有SSMS,则可以右键单击过程名称并选择重命名。

The other option is to drop the procedure and recreate it with the new name. 另一种选择是删除过程并使用新名称重新创建它。

This question is very old and already seen as answered but just to know that Renaming SP is not a good idea because it does not update sys.procedures. 这个问题很老,已经看到了回答,但只是知道重命名SP不是一个好主意,因为它不会更新sys.procedures。

For example, create a SP "sp_abc", 例如,创建一个SP“sp_abc”,

CREATE PROCEDURE [dbo].[sp_abc]
AS
BEGIN
    SET NOCOUNT ON;
  SELECT ID,
         Name
  FROM tbl_Student WHERE IsDeleted = 0
END

Now, Rename it 现在,重命名它

sp_rename 'sp_abc', 'sp_Newabc'

it shows following warning. 它显示以下警告。

Caution: Changing any part of an object name could break scripts and stored procedures.

Now see sp_Newabc 现在看sp_Newabc

sp_helptext sp_Newabc

you can see this result. 你可以看到这个结果。

CREATE PROCEDURE [dbo].[sp_abc]
AS    
BEGIN    
    SET NOCOUNT ON;
  SELECT ID,    
         Name    
  FROM tbl_Student WHERE IsDeleted = 0    
END

It still contains old Procedure name sp_abc. 它仍包含旧的过程名称sp_abc。 Because when you rename SP it does not update sys.procedure. 因为重命名SP时它不会更新sys.procedure。

Better solution is to drop the stored procedure and re-create it with the new name.
sp_rename <oldname> <newname>

In SQL Server 2008 R2, sys.procedures seems to be updated. 在SQL Server 2008 R2中,sys.procedures似乎已更新。 (in test environment) (在测试环境中)

Rename stored procedure sql server: 重命名存储过程sql server:

For the correct answer: View this article. 正确答案: 查看此文章。

Usage: sp_rename'[old_object_name]','[new_object_name]','[object_type]' 用法: sp_rename'[old_object_name]','[new_object_name]','[object_type]'

在SQL Server中重命名存储过程

EXEC sp_rename 'Proc_OldSpName', 'Proc_NewSpName';

as kkk mentioned in his answer 正如kkk他的回答中提到的那样

Better solution is to drop the stored procedure and re-create it with the new name. Better solutiondrop存储过程并使用新名称re-create它。

to do this 去做这个

DROP PROCEDURE [dbo].[Procedure_Name]

then 然后

Create Procedure [dbo].[GetEmployees]  
as 
....

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

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