我有一张桌子Companiesdata
CREATE TABLE [dbo].[Companiesdata]
(
[Company Name] nvarchar(255),
[Industry] varchar(40),
[ParentId] int NULL,
)
并且行是
CompanyName Industry Parent ID
--------------------------------------------------------
Xyz technologies Software 1
apple Technologies software 1
Sun network media 2
abc Technologies advertising 4
PQR Technnologies Marketing 5
abc Technologies Media 4
我还有一张桌子
create table dbo.companiesss
(
autoid int identity(1,1),
companyname varchar(max),
Industry varchar(max)
)
我写了一个这样的程序:
create proc pr_getlistofcompaniesss (@tparentid varchar(20))
as
begin
insert into dbo.companiesss(companyname, industry)
select
[CompanyName], [Industry]
from
[Companiesdata]
where
parentid in (select items from dbo.split(@tparentid,','))
except
select company name, industry
from dbo.companiesss
end
输出如下:
pr_getlistofcompaniesss 1,2,4
行显示为
AutoID Company name Industry
---------------------------------------------------
1 apple Technologies software
2 Sun network Media
3 xyz Technologies software
4 abc Technologies advertising
5 abc technologies media
相反,我的输出应如下所示:
pr_getlistofcompaniesss 1,2,4
AutoID Company name Industry
---------------------------------------------------
1 apple Technologies software
2 Sun network Media
3 xyz Technologies software
4 abc Technologies advertising,media
即如果我有同一个公司(这里abc technologies
)有不同的行业名称,那么行业字段应该用逗号分隔,在同一行显示记录,即(advertising, media
)