此插入语句有效。但是,我希望有人可以教我一种更有效的方法来进行这种查询。这就是我所拥有的......
我有一个名为Source的表,其中DealerID是一个非唯一ID(无论如何都在此表中)。对于每个DealerID,有多个名称。例如:
15 BillBoard
15 Event
15 Newspaper
16 BillBoard
16 Event
16 Newspaper
我知道,我知道。这是存储此数据的非常低效的方法。我们正在处理遗留应用,而且我现在无法对此数据结构进行全面改革。所以,我需要做的是为每个DealerID添加两个新记录。一个用于电话'一个用于'互联网'。所以在插入之后,它看起来像这样:
15 BillBoard
15 Event
15 Newspaper
15 Phone
15 Internet
16 BillBoard
16 Event
16 Newspaper
16 Phone
16 Internet
下面的sql语句有效,但我想知道是否有更好的方法...
declare @SourceTemp table (
[Id] int identity (1, 1) not null,
[DealerId] int
)
insert into @SourceTemp select distinct DealerId from Source where DealerId is not null
declare @dealerid int
declare @rowcount int = 1
declare @idcount int
select @idcount = max(Id) from @SourceTemp
while @rowcount < (@idcount + 1)
begin
select @dealerid = DealerId from @SourceTemp where Id = @rowcount
---------------- Insert Phone
if not exists (select * from Source where DealerId = @dealerid and Name = 'Phone')
begin
insert into
Source
([DealerID],[Name],[Service],[CampaignCode],[Description],[Created],[UserCreated],[Active])
values
(@dealerid, 'Phone', 'Generic', 'CampaignCode', NULL, GETDATE(), 0, 1)
end
--------------- Insert Internet
if not exists (select * from Source where DealerId = @dealerid and Name = 'Internet')
begin
insert into
Source
([DealerID],[Name],[Service],[CampaignCode],[Description],[Created],[UserCreated],[Active])
values
(@dealerid, 'Internet', 'Generic', 'CampaignCode', NULL, GETDATE(), 0, 1)
end
set @rowcount = @rowcount + 1
end
编辑后:
我不得不为汤姆做出一个小模型,添加了DISTINCT。这有效......
INSERT INTO Source ([DealerID], [Name], [Service], [CampaignCode], [Description], [Created], [UserCreated], [Active])
SELECT DISTINCT
S.DealerId, SQ.Name, 'Generic', 'CampaignCode', NULL, GETDATE(), 0, 1
FROM
Source S
CROSS JOIN (SELECT 'Internet' AS Name UNION ALL SELECT 'Phone' AS Name) SQ
WHERE
NOT EXISTS (SELECT * FROM Source WHERE DealerID = S.DealerID AND Name = SQ.Name)
答案 0 :(得分:5)
INSERT INTO Source (DealerID, Name, Service, CampaignCode, Description, ...)
SELECT DISTINCT
S.DealerID, SQ.Name, ...
FROM
Source S
CROSS JOIN (SELECT 'Internet' AS Name UNION ALL SELECT 'Phone' AS Name) SQ
WHERE
NOT EXISTS (SELECT * FROM Source WHERE DealerID = S.DealerID AND Name = SQ.Name)
更正了它以包含DISTINCT,因为每个Name
会有多个其他DealerID
个。如果性能有问题,那么您可以将DISTINCT
向下移动到Source
表上的子查询,然后从那里移动CROSS JOIN
:
INSERT INTO Source (DealerID, Name, Service, CampaignCode, Description, ...)
SELECT DISTINCT
S.DealerID, SQ.Name, ...
FROM
(SELECT DISTINCT DealerID
FROM Source
) S
CROSS JOIN (SELECT 'Internet' AS Name UNION ALL SELECT 'Phone' AS Name) SQ
WHERE
NOT EXISTS (SELECT * FROM Source WHERE DealerID = S.DealerID AND Name = SQ.Name)