if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Territories_Insert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[Territories_Insert]
GO
------------------------------------
-- 功能描述：增加一条记录 
-- 所属表格：Territories[ ]
-- 作者名称：
-- 创建日期：
-- 修改日期：
 /*
 -- 测试代码 --
 -- 参数设置 
DECLARE @TerritoryID nvarchar(20) -- 领域ID
    SET @TerritoryID =  ' '
DECLARE @TerritoryDescription nchar -- 领域描述
    SET @TerritoryDescription = 
DECLARE @RegionID int -- 所属地区ID
    SET @RegionID = 
 -- 调用存储过程 
 exec [Territories_Insert] @TerritoryID,@TerritoryDescription,@RegionID
 */
------------------------------------
CREATE PROCEDURE [Territories_Insert] (
@TerritoryID           nvarchar(20) = NULL,   -- 领域ID
@TerritoryDescription  nchar = NULL,          -- 领域描述
@RegionID              int = NULL             -- 所属地区ID

)
AS
INSERT INTO Territories(
	[TerritoryDescription],[RegionID]	 
)VALUES( 
	@TerritoryDescription,@RegionID	
)
SELECT @ID = SCOPE_IDENTITY() -- 返回新加记录的唯一标识
GO


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Territories_Edit]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[Territories_Edit]
GO
------------------------------------
-- 功能描述：修改一条记录 
-- 所属表格：Territories[]
-- 作者名称：
-- 创建日期：
-- 修改日期：
 /* -- 测试代码 --
 -- 参数设置 
DECLARE @TerritoryID nvarchar(20) -- 领域ID
    SET @TerritoryID =  ' '
DECLARE @TerritoryDescription nchar -- 领域描述
    SET @TerritoryDescription = 
DECLARE @RegionID int -- 所属地区ID
    SET @RegionID = 
 -- 调用存储过程 
 exec [Territories_Edit] @TerritoryID,@TerritoryDescription,@RegionID 
 */
------------------------------------
CREATE PROCEDURE [Territories_Edit]
@TerritoryID           nvarchar(20) = NULL,   -- 领域ID
@TerritoryDescription  nchar = NULL,          -- 领域描述
@RegionID              int = NULL             -- 所属地区ID

AS 
UPDATE Territories SET 
	 [TerritoryDescription] = @TerritoryDescription, [RegionID] = @RegionID
WHERE 
	 [TerritoryID] = @TerritoryID
GO


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Territories_Delete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[Territories_Delete]
GO
------------------------------------
-- 功能描述：删除一条记录 
-- 作者名称：
-- 创建日期：
-- 修改日期：
 /*
 -- 测试代码 --
 -- 参数设置 
 -- 调用存储过程 
DECLARE @ID int
    set @ID=0
 exec [Territories_Delete] @ID
 */
------------------------------------
CREATE PROCEDURE [Territories_Delete]
	@TerritoryID nvarchar --待删除的主键
AS 
   
	    DELETE FROM [Territories]	    WHERE TerritoryID = @TerritoryID 
     
     
GO   
     


