2016-03-29 8 views
1

他の2つのテーブルのデータに応じて3番目のテーブルを作成する際に問題があります。私は自分の問題をどのように解決するか分かりません。誰かが正しい方法で、あるいは正しい方法で私を見せてくれることを願っています。 私はおそらく私が間違った方向他の2つのテーブルに応じてテーブルにデータを挿入してください

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) 
) 

最初の2つのテーブルは、データと第三のニーズを満たしているに検索可能IDで満たされるために何かを知らないので、私は非常にSQLを経験していませんよuserProfilesおよびprojectTypesテーブルから取得します。私は今、私は2つの他のテーブルからのデータに応じて、第三表(userProfileProjectTypes)に情報を挿入するこの

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'); 

ようUSERPROFILES内のデータとProjectTypesテーブルを挿入します。

したがって、userProfileがSuper Userの場合、必要なprojectTypeはManaged services、support、devおよびDynです。したがって、userProfileProjectTypeテーブルには、同じuserProfileIdを持つ4行と、別のprojectTypeIdを4回使用する必要があります。最終的に私はすべての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 
+0

'CASE'は手続き型言語の' if'のようには機能しません。単に原子値を返します。 – HoneyBadger

+0

コピーアンドペースト可能なテストシナリオをありがとう。 upvoteに値する! – Shnugo

+0

さて、あなたは原子価の意味を理解しています。私のアイデアは機能していないことは分かっていましたが、何かを作る方法がわかりません。 ありがとうthe upvote Shnugo – ArneS

答えて

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; 
+0

ありがとうございました!それは確かに私が必要なものです。私はCROSS JOINがどのように動作するかを見ていきます。それは必要なことをするようです。 – ArneS

+0

@ArneS、クロスジョインはちょうど 'すべてのものすべてです' ... – Shnugo

+0

わかりました。あなたは私をたくさん助けました!私はテーブルのこれらのタイプのカップルを作る必要があり、これは非常に良い仕事をする!再度大変ありがとうございます。 – ArneS

関連する問題