【问题标题】:Why does my stored procedure not show up in my SQL Server database?为什么我的存储过程没有出现在我的 SQL Server 数据库中?
【发布时间】:2014-12-28 17:10:41
【问题描述】:

我在 SQL Server 中创建了一个数据库。我遵循这些:

Programmability -> Stored Procedures -> right click -> new stored procedures.  

我在我的存储过程中写了这段代码:

create procedure Insert_Users_legal
(
    @name   nvarchar(50),
    @agentPosition  int,
    @email  varchar(50),
    @mobile char(11),
    @phone  char(11),
    @postalCode char(10),
    @address    nchar(10)
)
as
    set nocount on

    insert into users_legal (name, agentPosition, email, mobile, phone, postalCode, address)
    values (@name, @agentPosition, @email, @mobile, @phone, @postalCode, @address)

    select CAST(scope_identity() as int) as id_legalUser
    return  

但是当我保存我的存储过程时,它没有显示在对象资源管理器的存储过程文件夹中。

我能做什么?

【问题讨论】:

  • 在其中创建 DB 过程。尝试使用完全限定名称。您可能签入了错误的数据库
  • @Mihai:我经常刷新。
  • 数据库中没有“存储过程文件夹”;这是您用来访问数据库的客户端的功能。您还没有说您使用的是什么客户端...话虽如此,您确定您实际上已经创建了该程序吗?该过程是否显示在INFORMATION_SCHEMA.ROUTINES 中?
  • @NoDisplayName: 不,我签入了正确的数据库。 :(
  • 执行这个查询Select name FROM sys.procedures WHERE name LIKE '%Insert_Users_legal%'

标签: sql-server stored-procedures insert


【解决方案1】:

首先点击执行,然后点击刷新

【讨论】:

    【解决方案2】:

    执行以下语句,它必须在您正确的数据库中创建一个存储过程:

    /*****   use the correct database *****/
    USE municipalityDB;
    GO
    
    /*****   Drop Procedure if already exist *****/
    IF (OBJECT_ID('Insert_Users_legal') IS NOT NULL)
      DROP PROCEDURE Insert_Users_legal
    GO
    
    /*****   Now create the procedure *****/
    create procedure Insert_Users_legal
    (
    @name           nvarchar(50),
    @agentPosition  int,
    @email          varchar(50),
    @mobile         char(11),
    @phone          char(11),
    @postalCode     char(10),
    @address        nchar(10),
    @id_legalUser   INT OUTPUT   --<-- use output param to get new id 
    )
    as
    BEGIN
     set nocount on;
      insert into users_legal (name,agentPosition,email,mobile,phone,postalCode,[address])
      values (@name,@agentPosition,@email,@mobile,@phone,@postalCode,@address)
    
      SELECT  @id_legalUser = scope_identity();     --<-- No need to cast as INT 
    -- return    --<-- Not required   
    END
    GO
    
    
    /*****   Check if procedure exists *****/
    Select name FROM sys.procedures 
    WHERE name LIKE '%Insert_Users_legal%'
    GO
    

    【讨论】:

    • 您不需要编写所有这些代码,如果您从左上角的下拉列表中的 SSMS 中选择了正确的数据库,它就会在正确的数据库中创建过程。无论如何,我在此过程的定义中所做的重要更改是新生成的标识值的 OUTPUT 参数。使用输出参数将允许您在代码中的其他位置使用此值,否则我看不出选择它有任何意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2019-02-03
    • 1970-01-01
    • 2017-10-17
    • 2020-09-10
    • 2015-09-05
    相关资源
    最近更新 更多