Sql 根据其他两个表在表中插入数据

Sql 根据其他两个表在表中插入数据,sql,sql-server,Sql,Sql Server,根据其他两个表的数据填写第三个表有问题。我不知道如何解决我的问题,我希望有人能以正确的方式或正确的方式向我展示。 我对SQL不是很有经验,所以我可能不知道什么东西会让我朝着错误的方向搜索 CREATE TABLE UserProfiles ( UserProfileId bigint NOT NULL identity(1,1) constraint pk_UserProfileId primary key, DescriptionTranslationId bigint not n

根据其他两个表的数据填写第三个表有问题。我不知道如何解决我的问题,我希望有人能以正确的方式或正确的方式向我展示。 我对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');
现在,我想根据其他两个表中的数据将信息插入第三个表(userProfileProjectTypes)

因此,当userProfile是超级用户时,它需要的项目类型是托管服务、支持、开发和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

也许是这样的

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;

CASE
与过程语言中的
if
不同,它只返回一个原子值。感谢您提供了可复制粘贴的测试场景。值得投一票!我明白你说的原子值是什么意思了。我知道我的想法行不通,但我不知道如何做一些有用的东西。感谢upvote Shnugoi如果我是你,我会创建一个存储过程来插入行。因为您的需求指向存储过程需求。通过使用存储过程,您将在Sql Server上具有编程灵活性。要熟悉Sql过程,可以查看以下链接。这里有一个和你相似的例子;非常感谢你!这正是我所需要的。我会看看交叉连接是如何工作的,这似乎是我需要的。好的,我明白了。你帮了我很多忙!我需要做一些这样的表格,这做的很好!再次感谢你。