How to write stored procedure?

2020-07-13 08:48发布

I am learning how to write a stored procedure. I kinda get it but I don't. I know it goes something like this..

CREATE PROCEDURE|PROC <sproc name>
[<parameter name> [schema.]<data type> [VARYING] [=<default value>] [OUT[PUT]]
[READONLY]
[,<parameter name> [schema.]<data type> [VARYING] [=<deafult value>] [OUT[PUT]]
[READONLY]
[,...
  ...
   ]]
[WITH 
  RECOMPILE|ENCRYPTION|[EXECUTE AS{ CALLER|SELF|OWNER|<'user name'>}]
[FOR REPLICATION]
AS
 <code> | EXTERNAL NAME <assembly name>.<assembly class>.<method>

So what I am stuck on and I am trying to understand is this.. Write a stored procedure that accepts a Territory ID, Territory Description, and Region ID and inserts them as new row in the Territories table in Northwind.

Ok so I know I could do something like this I believe:

USE Northwind
GO
CREATE PRO spTerritory
AS
SELECT Territory ID,Territory Description,RegionID
From dbo.Territories

But then I can be wrong. But I dont know where to insert then as a new row in the table. I know I would use something like @newrow or @rows along that line. If someone can help me understand who to do a sproc I would be greatful.

3条回答
乱世女痞
2楼-- · 2020-07-13 09:03

USE [SchoolData] GO

For getting the all data

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[GetDepartment] as select Id,Name,Description,IsDeleted from Departments where IsDeleted=0

查看更多
乱世女痞
3楼-- · 2020-07-13 09:06

You are looking for the INSERT statement.

CREATE PROCEDURE InsertTerritory (
     @territoryId int
    ,@territoryDescription nvarchar(200)
    ,@regionId int)
AS
BEGIN

    INSERT INTO Territories (Id, [Description], RegionId)
    VALUES (@territoryId, @territoryDescription, @regionId)

END
GO
查看更多
登录 后发表回答