Sam*_*man 1 sql-server stored-procedures insert
我在SQL Server中创建了一个数据库。我遵循这些:
Programmability -> Stored Procedures -> right click -> new stored procedures.
Run Code Online (Sandbox Code Playgroud)
我在存储过程中编写以下代码:
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
Run Code Online (Sandbox Code Playgroud)
但是,当我保存存储过程时,它不会显示在对象资源管理器的存储过程文件夹中。
我能做什么?

执行以下语句,它必须在正确的数据库中创建一个存储过程:
/***** 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
Run Code Online (Sandbox Code Playgroud)