更新父表计数更新sql server中子表中的记录

时间:2017-02-21 20:17:54

标签: sql sql-server

我有两张桌子

  • 部门 (deptid(primarykey),deptname,empcount)
  • EmployeeInfo (empid(primarykey),name,deptid(foreign_key)引用deptid)

当我更新deptid中记录的EmployeeInfo时,可以立即修改计数以适应相应的更改。

子表中的更新将导致父表中的递增和递减。 我知道这可以通过触发器来完成,但有没有办法使用存储过程

2 个答案:

答案 0 :(得分:0)

恕我直言,聚合值 应该是需要更新的静态列。

视图会更好。这可以按需计算。

但是如果你坚持......这里有一个选项....它不需要nsync-ness和存储过程。

-- START TSQL

SET NOCOUNT ON


IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = N'dbo' and TABLE_NAME = N'Employee' and TABLE_TYPE = N'BASE TABLE' ) 
BEGIN 
DROP TABLE [dbo].[Employee] 
END 
GO


IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = N'dbo' and TABLE_NAME = N'Department' and TABLE_TYPE = N'BASE TABLE' ) 
BEGIN 
DROP TABLE [dbo].[Department] 
END 
GO


DROP FUNCTION dbo.udfEmployeeCountByDepartment
GO



CREATE TABLE [dbo].[Department](
    [DepartmentUUID] [uniqueidentifier] NOT NULL,
    [TheVersionProperty] [timestamp] NOT NULL,
    [DepartmentName] [nvarchar](80) NULL,
    [CreateDate] [datetime] NOT NULL,
    [MyTinyInt] tinyint not null
    )


ALTER TABLE dbo.[Department] ADD CONSTRAINT PK_Department PRIMARY KEY NONCLUSTERED ([DepartmentUUID]) 
GO

ALTER TABLE [dbo].[Department] ADD CONSTRAINT CK_DepartmentName_Unique UNIQUE ([DepartmentName]) 
GO






CREATE TABLE [dbo].[Employee] ( 

    [EmployeeUUID] [uniqueidentifier] NOT NULL,
    [ParentDepartmentUUID] [uniqueidentifier] NOT NULL,
    [TheVersionProperty] [timestamp] NOT NULL,
    [SSN] [nvarchar](11) NOT NULL,
    [LastName] [varchar](64) NOT NULL,
    [FirstName] [varchar](64) NOT NULL,
    [CreateDate] [datetime] NOT NULL,
    [HireDate] [datetime] NOT NULL
    )

GO

ALTER TABLE dbo.Employee ADD CONSTRAINT PK_Employee PRIMARY KEY NONCLUSTERED (EmployeeUUID) 
GO


ALTER TABLE [dbo].[Employee] ADD CONSTRAINT CK_SSN_Unique UNIQUE (SSN) 
GO

ALTER TABLE [dbo].[Employee] ADD CONSTRAINT FK_EmployeeToDepartment FOREIGN KEY (ParentDepartmentUUID) REFERENCES dbo.Department (DepartmentUUID) 
GO

CREATE FUNCTION dbo.udfEmployeeCountByDepartment(@DepartmentUUID uniqueidentifier)
RETURNS int
AS
BEGIN
    DECLARE @returnCount int
    select @returnCount = count(*) from dbo.Employee e where e.ParentDepartmentUUID = @DepartmentUUID
    RETURN @returnCount
END

GO



ALTER TABLE [dbo].[Department] ADD [ComputedEmployeeCount] as (dbo.udfEmployeeCountByDepartment(DepartmentUUID))

GO



Select * from dbo.Department

INSERT [dbo].[Department] ( DepartmentUUID , DepartmentName , CreateDate )
Select NEWID() , 'DepartmentOne', CURRENT_TIMESTAMP
UNION ALL Select NEWID() , 'DepartmentTwo', CURRENT_TIMESTAMP
UNION ALL Select NEWID() , 'DepartmentThree', CURRENT_TIMESTAMP
UNION ALL Select NEWID() , 'DepartmentFour', CURRENT_TIMESTAMP



INSERT INTO [dbo].[Employee] ( 
    [EmployeeUUID] ,
    [ParentDepartmentUUID] ,
    [SSN] ,
    [LastName] ,
    [FirstName] ,
    [CreateDate],
    [HireDate] 
    )
SELECT NEWID() , (select top 1 DepartmentUUID from dbo.Department order by newid()), '111-11-1111', 'OneLN', 'OneFN',  CURRENT_TIMESTAMP,  CURRENT_TIMESTAMP
UNION ALL SELECT NEWID() , (select top 1 DepartmentUUID from dbo.Department order by newid()), '222-22-2222', 'TwoLN', 'TwoFN',  CURRENT_TIMESTAMP,  CURRENT_TIMESTAMP
UNION ALL SELECT NEWID() , (select top 1 DepartmentUUID from dbo.Department order by newid()), '333-33-3333', 'ThreeLN', 'ThreeFN',  CURRENT_TIMESTAMP,  CURRENT_TIMESTAMP
UNION ALL SELECT NEWID() , (select top 1 DepartmentUUID from dbo.Department order by newid()), '444-44-4444', 'FourLN', 'FourFN',  CURRENT_TIMESTAMP,  CURRENT_TIMESTAMP
UNION ALL SELECT NEWID() , (select top 1 DepartmentUUID from dbo.Department order by newid()), '555-55-5555', 'FiveLN', 'FiveFN',  CURRENT_TIMESTAMP,  CURRENT_TIMESTAMP
UNION ALL SELECT NEWID() , (select top 1 DepartmentUUID from dbo.Department order by newid()), '666-66-6666', 'SixLN', 'SixFN',  CURRENT_TIMESTAMP,  CURRENT_TIMESTAMP
UNION ALL SELECT NEWID() , (select top 1 DepartmentUUID from dbo.Department order by newid()), '777-77-7777', 'SevenLN', 'SevenFN',  CURRENT_TIMESTAMP,  CURRENT_TIMESTAMP
UNION ALL SELECT NEWID() , (select top 1 DepartmentUUID from dbo.Department order by newid()), '888-88-8888', 'EightLN', 'EightFN',  CURRENT_TIMESTAMP,  CURRENT_TIMESTAMP
UNION ALL SELECT NEWID() , (select top 1 DepartmentUUID from dbo.Department order by newid()), '999-99-9999', 'NineLN', 'NineFN',  CURRENT_TIMESTAMP,  CURRENT_TIMESTAMP


Select * from dbo.Department

答案 1 :(得分:0)

我认为这就是你要找的东西:

CREATE PROCEDURE dbo.UpdateDeptId(
@p_Employee_id int,
@p_NewDept_id int 
)
AS
BEGIN

UPDATE EmployeeInfo SET deptid = @p_NewDept_id
WHERE empid = @p_Employee_id

UPDATE T1  SET T1.EmpCount = T2.CountEmp
FROM Departments AS T1 INNER JOIN (SELECT deptid , Count(*) as CountEmp FROM EmployeeInfo GROUP BY deptid) AS T2
ON T1.deptid = T2.deptid

END

备注:最佳做法是在列中存储聚合值,尝试使用视图或仅运行查询以在需要时检索