以下是我的代码:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [sheryl_insert_AntDB]
ON [dbo].[users]
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @id varchar(10), ----- 员工账号
@name varchar(40), ----- 员工姓名
@dep varchar(100), ----- 所在部门名称
@dep_id varchar(500), ----- 所在部门编号
@psd varchar(32), ----- 员工登录密码
@phone varchar(15), ----- 员工工作电话
@unit varchar(100), ----- 所在公司名称
@unit_id varchar(20), ----- 所在公司编号
@zone varchar(100), ----- 公司地址
@view_id int,
@group_id int,
@user_id int
SELECT @id = user_id,
@name = user_name,
@dep_id = user_dep,
@psd = user_psd,
@phone = user_phone,
@unit_id = user_unit FROM INSERTED
---------------------------------------- 获得用户的公司信息 ----------------------------------------
SELECT @unit_id = v.dic_id,
@unit = v.dic_name,
@unit_id = u.unit_id,
@zone = u.zone_name
FROM dbo.dict AS v,dbo.unit_zone AS u
WHERE v.dic_id = @unit_id AND u.unit_id = @unit_id
---- 通过查找 AntDB.dbo.hs_View 中 Col_Name 是否存在,如果存在就取出 AntDB.dbo.hs_view 的 Col_ID,
---- 若不存在就进行插入操作,然后提取插入之后 AntDB.dbo.hs_view 表中的Col_ID
IF EXISTS(SELECT*FROM AntDB.dbo.hs_View WHERE Col_Name = @unit)
SELECT @view_id = Col_ID
FROM AntDB.dbo.hs_View WHERE Col_Name = @unit
ELSE
BEGIN
INSERT INTO AntDB.dbo.hs_View(Col_Name,Col_Type,Col_ItemIndex)
VALUES(@unit,1,@unit_id)
SET @view_id = @@identity
END
PRINT @view_id
--------------------------------------- 获得用户的部门信息 --------------------------------------------
SELECT @dep = d.dep_name,
@dep_id = d.dep_id,
@id = b.user_id,
@dep_id = b.user_dep
FROM dbo.users AS b,dbo.department AS d
WHERE b.user_dep = @dep_id AND d.dep_id = @dep_id
---- 通过查找 AntDB.dbo.hs_Group 中 Col_Name 是否存在,如果存在就取出 AntDB.dbo.hs_Group 的 Col_ID,
---- 若不存在就进行插入操作,然后提取插入之后 AntDB.dbo.hs_Group 表中的 Col_ID
IF EXISTS(SELECT*FROM AntDB.dbo.hs_Group WHERE Col_Name = @dep)
SELECT @group_id = Col_ID
FROM AntDB.dbo.hs_Group WHERE Col_Name = @dep
ELSE
BEGIN
INSERT INTO AntDB.dbo.hs_Group(Col_Name,Col_ItemIndex)
SELECT @dep,@dep_id FROM INSERTED
SET @group_id = @@identity
END
PRINT @group_id
----------------------------------------- 获得用户的信息 -------------------------------------------------
---- 通过查找 AntDB.dbo.hs_User 中 Col_LoginName 是否存在,如果存在就取出 AntDB.dbo.hs_User 的 Col_ID,
---- 若不存在就进行插入操作,然后提取插入之后 AntDB.dbo.hs_User 表中的 Col_ID
IF EXISTS(SELECT*FROM AntDB.dbo.hs_User WHERE Col_LoginName = @id)
SELECT @user_id = Col_ID
FROM AntDB.dbo.hs_User WHERE Col_LoginName = @id
ELSE
BEGIN
INSERT INTO AntDB.dbo.hs_User(Col_LoginName,Col_Name,Col_PWord,Col_o_Address,Col_o_Phone)
SELECT @id,@name,@psd,@zone,@phone FROM INSERTED
SET @user_id = @@identity
END
PRINT @user_id
------------------------------ 获得用户的公司,组织,用户之间的关系信息 -------------------------------------
---- 通过 AntDB.dbo.hs_View 表中的 Col_ID 以及 AntDB.hs_Group 中的 Col_ID 来确定公司与部门的关系
INSERT INTO AntDB.dbo.hs_Relation(Col_G_ClassId,Col_G_ObjId,Col_HsItemID,Col_HsItemType,
Col_DHsItemID,Col_DHsItemType,Col_RelType,Col_Ref,Col_RelIndex,Col_ViewID)
VALUES(0,0,@view_id,4,@group_id,2,1,0,0,0)
---- 通过 AntDB.dbo.hs_View 表中的 Col_ID , AntDB.hs_Group 中的 Col_ID 以及 AntDB.hs_User 表中的 Col_ID
---- 来确定该用户所在的公司及部门
INSERT INTO AntDB.dbo.hs_Relation(Col_G_ClassId,Col_G_ObjId,Col_HsItemID,Col_HsItemType,
Col_DHsItemID,Col_DHsItemType,Col_RelType,Col_Ref,Col_RelIndex,Col_ViewID)
VALUES(0,0,@group_id,2,@user_id,1,1,0,0,@view_id)
END
程序在运行插入时没有报错,但运行后在数据库,和前台的显示为:
在数据库中:
问题:为什么会在同一个单位下出现相同的部门
如果是表里面的数据出现重复,菜单那样,那就是SQL的问题
如果是表里面的数据没有出现重复,菜单那样,那就是生成菜单逻辑问题
好长啊,录入部门前做个判断,判断部门是否存在,如果存在则不在录入。
我做了判断啊,就是在确定用的部门和组织关系时,出现重复了,我不知道该怎么解决
我做了判断啊,就是在确定用的部门和组织关系时,出现重复了,我不知道该怎么解决
SELECT @dep = d.dep_name,
@dep_id = d.dep_id,
@id = b.user_id,
@dep_id = b.user_dep
FROM dbo.users AS b,dbo.department AS d
WHERE b.user_dep = @dep_id AND d.dep_id = @dep_id
---- 通过查找 AntDB.dbo.hs_Group 中 Col_Name 是否存在,如果存在就取出 AntDB.dbo.hs_Group 的 Col_ID,
---- 若不存在就进行插入操作,然后提取插入之后 AntDB.dbo.hs_Group 表中的 Col_ID
IF EXISTS(SELECT*FROM AntDB.dbo.hs_Group WHERE Col_Name = @dep)
SELECT @group_id = Col_ID
FROM AntDB.dbo.hs_Group WHERE Col_Name = @dep
ELSE
BEGIN
INSERT INTO AntDB.dbo.hs_Group(Col_Name,Col_ItemIndex)
SELECT @dep,@dep_id FROM INSERTED
SET @group_id = @@identity
END
PRINT @group_id
你看着一段,我就是最后一步难住我了
------------------------------ 获得用户的公司,组织,用户之间的关系信息 -------------------------------------
---- 通过 AntDB.dbo.hs_View 表中的 Col_ID 以及 AntDB.hs_Group 中的 Col_ID 来确定公司与部门的关系
INSERT INTO AntDB.dbo.hs_Relation(Col_G_ClassId,Col_G_ObjId,Col_HsItemID,Col_HsItemType,
Col_DHsItemID,Col_DHsItemType,Col_RelType,Col_Ref,Col_RelIndex,Col_ViewID)
VALUES(0,0,@view_id,4,@group_id,2,1,0,0,0)
---- 通过 AntDB.dbo.hs_View 表中的 Col_ID , AntDB.hs_Group 中的 Col_ID 以及 AntDB.hs_User 表中的 Col_ID
---- 来确定该用户所在的公司及部门
INSERT INTO AntDB.dbo.hs_Relation(Col_G_ClassId,Col_G_ObjId,Col_HsItemID,Col_HsItemType,
Col_DHsItemID,Col_DHsItemType,Col_RelType,Col_Ref,Col_RelIndex,Col_ViewID)
VALUES(0,0,@group_id,2,@user_id,1,1,0,0,@view_id)
END
就是这段我不知道怎么处理,在AntDB数据库中,表dbo.hs_User,dbo.hs_View,dbo.hs_Group,dbo.hs_Realation中的Col_ID都是主键,而且是自增长的,而在dbo.hs_Realation表中体现了用户与公司部门之间的关系
1 set ANSI_NULLS ON 2 set QUOTED_IDENTIFIER ON 3 go 4 5 use sheryl 6 go 7 8 ALTER TRIGGER [sheryl_insert_AntDB] 9 ON [dbo].[users] 10 AFTER INSERT 11 AS 12 BEGIN 13 -- SET NOCOUNT ON added to prevent extra result sets from 14 -- interfering with SELECT statements. 15 SET NOCOUNT ON; 16 17 DECLARE @id varchar(10), ----- 员工账号 18 @name varchar(40), ----- 员工姓名 19 @dep varchar(100), ----- 所在部门名称 20 @dep_id varchar(500), ----- 所在部门编号 21 @psd varchar(32), ----- 员工登录密码 22 @phone varchar(15), ----- 员工工作电话 23 @unit varchar(100), ----- 所在公司名称 24 @unit_id varchar(20), ----- 所在公司编号 25 @zone varchar(100), ----- 公司地址 26 @view_id int, 27 @group_id int, 28 @user_id int, 29 @aa int, 30 @bb int 31 32 SELECT @id = user_id, 33 @name = user_name, 34 @dep_id = user_dep, 35 @psd = user_psd, 36 @phone = user_phone, 37 @unit_id = user_unit FROM INSERTED 38 39 40 41 ------------------------- 获得用户的公司信息 ------------------------------- 42 43 44 45 SELECT @unit_id = v.dic_id, 46 @unit = v.dic_name, 47 @unit_id = u.unit_id, 48 @zone = u.zone_name 49 FROM dbo.dict AS v,dbo.unit_zone AS u 50 WHERE v.dic_id = @unit_id AND u.unit_id = @unit_id 51 52 ---- 通过查找 AntDB.dbo.hs_View 中 Col_Name 是否存在,如果存在就取出 AntDB.dbo.hs_view 的 Col_ID, 53 54 ---- 若不存在就进行插入操作,然后提取插入之后 AntDB.dbo.hs_view 表中的Col_ID 55 56 57 IF EXISTS(SELECT*FROM AntDB.dbo.hs_View WHERE Col_Name = @unit) 58 SELECT @view_id = Col_ID 59 FROM AntDB.dbo.hs_View WHERE Col_Name = @unit 60 ELSE 61 BEGIN 62 INSERT INTO AntDB.dbo.hs_View(Col_Name,Col_Type,Col_ItemIndex,Col_IsPublic) 63 VALUES(@unit,1,@unit_id,0) 64 SET @view_id = @@identity 65 END 66 PRINT @view_id 67 68 69 70 ------------------------ 获得用户的部门信息 --------------------------------- 71 72 73 74 SELECT @dep = d.dep_name, 75 @dep_id = d.dep_id, 76 @id = b.user_id, 77 @dep_id = b.user_dep 78 FROM dbo.users AS b,dbo.department AS d 79 WHERE b.user_dep = @dep_id AND d.dep_id = @dep_id 80 81 ---- 通过查找 AntDB.dbo.hs_Group 中 Col_Name 是否存在,如果存在就取出 AntDB.dbo.hs_Group 的 Col_ID, 82 83 ---- 若不存在就进行插入操作,然后提取插入之后 AntDB.dbo.hs_Group 表中的 Col_ID 84 85 86 IF EXISTS(SELECT*FROM AntDB.dbo.hs_Group WHERE Col_Name = @dep) 87 SELECT @group_id = Col_ID 88 FROM AntDB.dbo.hs_Group WHERE Col_Name = @dep 89 ELSE 90 BEGIN 91 INSERT INTO AntDB.dbo.hs_Group(Col_Name,Col_ItemIndex) 92 SELECT @dep,@dep_id FROM INSERTED 93 SET @group_id = @@identity 94 END 95 PRINT @group_id 96 97 98 99 ------------------------- 获得用户的信息 ----------------------------------- 100 101 102 103 ---- 通过查找 AntDB.dbo.hs_User 中 Col_LoginName 是否存在,如果存在就取出 AntDB.dbo.hs_User 的 Col_ID, 104 105 ---- 若不存在就进行插入操作,然后提取插入之后 AntDB.dbo.hs_User 表中的 Col_ID 106 107 108 IF EXISTS(SELECT*FROM AntDB.dbo.hs_User WHERE Col_LoginName = @id) 109 SELECT @user_id = Col_ID 110 FROM AntDB.dbo.hs_User WHERE Col_LoginName = @id 111 ELSE 112 BEGIN 113 INSERT INTO AntDB.dbo.hs_User(Col_LoginName,Col_Name,Col_PWord,Col_o_Address,Col_o_Phone) 114 SELECT @id,@name,@psd,@zone,@phone FROM INSERTED 115 SET @user_id = @@identity 116 END 117 PRINT @user_id 118 119 120 121 ----------------- 获得用户的公司,组织,用户之间的关系信息 ----------------------- 122 123 124 125 ---- 通过 AntDB.dbo.hs_View 表中的 Col_ID 以及 AntDB.hs_Group 中的 Col_ID 来确定公司与部门的关系 126 127 128 129 SET @aa = (SELECT COUNT(*) FROM AntDB.dbo.hs_Relation 130 WHERE Col_HsItemID = @view_id and Col_DHsItemID = @group_id 131 and Col_HsItemType = 4 and Col_DHsItemType = 2 and Col_ViewID = 0) 132 133 IF(@aa >= 1) 134 PRINT '错误,不能插入重复数据' 135 ELSE 136 BEGIN 137 INSERT INTO AntDB.dbo.hs_Relation(Col_G_ClassId,Col_G_ObjId,Col_HsItemID,Col_HsItemType, 138 Col_DHsItemID,Col_DHsItemType,Col_RelType,Col_Ref,Col_RelIndex,Col_ViewID) 139 VALUES(0,0,@view_id,4,@group_id,2,1,0,0,0) 140 SET 141 END 142 143 144 145 ---- 通过 AntDB.dbo.hs_View 表中的 Col_ID , AntDB.hs_Group 中的 Col_ID 以及 AntDB.hs_User 表中的 Col_ID 146 147 ---- 来确定该用户所在的公司及部门 148 149 150 151 SET @bb = (SELECT COUNT(*) FROM AntDB.dbo.hs_Relation 152 WHERE Col_HsItemID = @group_id and Col_DHsItemID = @user_id 153 and Col_HsItemType = 2 and Col_DHsItemType = 1 and Col_ViewID = @view_id) 154 155 IF(@bb >= 1) 156 PRINT '错误,不能插入重复的数据' 157 ELSE 158 BEGIN 159 INSERT INTO AntDB.dbo.hs_Relation(Col_G_ClassId,Col_G_ObjId,Col_HsItemID,Col_HsItemType, 160 Col_DHsItemID,Col_DHsItemType,Col_RelType,Col_Ref,Col_RelIndex,Col_ViewID) 161 VALUES(0,0,@group_id,2,@user_id,1,1,0,0,@view_id) 162 END 163 164 END