根据其他2个表在表中插入数据

时间:2016-03-29 09:26:40

标签: sql sql-server

我在填写第3张表时遇到问题,具体取决于其他2张表的数据。我不知道如何解决我的问题,我希望有人能以正确的方式向我展示,或者也许是正确的方式。 我对SQL不是很有经验,所以我可能不知道哪些东西让我搜索错误的方向

CREATE TABLE UserProfiles
(
   UserProfileId bigint NOT NULL identity(1,1) constraint pk_UserProfileId primary key,
   DescriptionTranslationId bigint not null,
   [Description] varchar(max),
   AuditModifiedOn datetime2,
   AuditModifiedBy varchar(MAX),
   AuditDeleted bit not null default '0',
   AuditCreatedOn datetime2 NOT NULL default GETDATE(),
   AuditCreatedBy varchar(MAX) NOT NULL
)

CREATE TABLE ProjectTypes
(
   ProjectTypeId bigint NOT NULL identity(1,1) constraint pk_ProjectTypeId primary key,
   ProjectTypeName varchar(max),
   DescriptionTranslationId bigint NOT NULL,
   AuditModifiedOn datetime2,
   AuditModifiedBy varchar(MAX),
   AuditDeleted bit not null default '0',
   AuditCreatedOn datetime2 NOT NULL default GETDATE(),
   AuditCreatedBy varchar(MAX) NOT NULL,
)

CREATE TABLE UserProfileProjectType
(
   UserProfileProjectTypeId bigint NOT NULL identity(1,1) constraint pk_UserProfileProjectTypeId primary key,
   UserProfileId bigint not null,
   ProjectTypeId bigint not null,
   AuditModifiedOn datetime2,
   AuditModifiedBy varchar(MAX),
   AuditDeleted bit not null default '0',
   AuditCreatedOn datetime2 NOT NULL default GETDATE(),
   AuditCreatedBy varchar(MAX) NOT NULL,
   constraint fk_UserProfileProjectType_UserProfileId_UserProfiles foreign key (UserProfileId) references UserProfiles(UserProfileId),
   constraint fk_UserProfileProjectType_ProjectTypeId_ProjectTypes foreign key (ProjectTypeId) references ProjectTypes(ProjectTypeId)
)

前两个表填充了数据,第三个表需要填写userProfiles和projectTypes表中的ID。我将数据插入userProfiles和ProjectTypes表中,如下所示

insert into dbo.UserProfiles (DescriptionTranslationId, [Description], AuditCreatedBy)
values 
(9999, 'Super User', 'email'),
(9999,  'Support Admin', 'email'),
(9999, 'Support Manager','email'),
(9999, 'Dev Admin', 'email'),
(9999, 'Dev Manager', 'email'),
(9999, 'Dynapps Admin', 'email'),
(9999, 'Dynapps Manager', 'email');

insert into ProjectTypes (ProjectTypeName, DescriptionTranslationId, AuditCreatedBy)
values
('Managed services', 9999, 'email'),
('Support', 9999, 'email'),
('Dev', 9999, 'email'),
('Dyn', 9999, 'email');

现在我想将信息插入第3个表(userProfileProjectTypes),具体取决于其他2个表中的数据。

因此,当userProfile是超级用户时,它需要的projectType是托管服务,支持,开发和Dyn。因此userProfileProjectType表应该有4行,具有相同的userProfileId和4次不同的projectTypeId。最终我必须为每个userProfile执行此操作(对于userProfileId'dev manager',只有projectTypeId'dev'应位于userProfileProjectType表中)。我不知道如何实现这一目标。我确实试过这样的东西。但它没有用。

Select UserProfileId, [Description],
CASE 
   WHEN [Description] = 'Super User' THEN (insert into UserProfileProjectType (UserProfileId, ProjectTypeId, AuditCreatedBy) values (UserProfileId), (select ProjectTypeId from ProjectTypes where ProjectTypeName = "Managed services"), ('email'))
   WHEN [Description] = 'Super User' THEN (insert into UserProfileProjectType (UserProfileId, ProjectTypeId, AuditCreatedBy) values (UserProfileId), (select ProjectTypeId from ProjectTypes where ProjectTypeName = "Support"), ('email'))
   WHEN [Description] = 'Super User' THEN (insert into UserProfileProjectType (UserProfileId, ProjectTypeId, AuditCreatedBy) values (UserProfileId), (select ProjectTypeId from ProjectTypes where ProjectTypeName = "Dev"), ('email'))
   WHEN [Description] = 'Super User' THEN (insert into UserProfileProjectType (UserProfileId, ProjectTypeId, AuditCreatedBy) values (UserProfileId), (select ProjectTypeId from ProjectTypes where ProjectTypeName = "Dynapps"), ('email'))
   ELSE 
END
from UserProfiles

1 个答案:

答案 0 :(得分:2)

也许是这样的?

INSERT INTO UserProfileProjectType(UserProfileId,ProjectTypeId,AuditCreatedOn,AuditCreatedBy)
SELECT up.UserProfileId
      ,pt.ProjectTypeId 
      ,GETDATE()
      ,'TestCreator'
FROM UserProfiles AS up
CROSS JOIN ProjectTypes AS pt --cross join with all
WHERE [Description]='Super User'

UNION ALL

SELECT up.UserProfileId
      ,pt.ProjectTypeId 
      ,GETDATE()
      ,'TestCreator'
FROM UserProfiles AS up
CROSS JOIN (SELECT * FROM ProjectTypes WHERE ProjectTypeName='Dev') AS pt --cross join only Dev
WHERE [Description]='Dev Manager'
;

SELECT * FROM UserProfileProjectType;