Search code examples
sql-serverstored-proceduresinsert

Why does my stored procedure not show up in my SQL Server database?


I created a database in SQL Server. I follow these:

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

I write this code in my stored 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)
)
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  

But when I save my stored procedure, it doesn't show up in stored procedure folder in the Object Explorer.

What can I do?

enter image description here


Solution

  • execute the following statements and it must create a stored procedure in your correct database:

    /*****   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