Скрипт для для каждого ТС в строке названия групп через ; Версия для: MS-SQL Standart
Code
declare @t table
(ObjectID int,obj_g varchar(max) )
declare cur cursor local for
select ObjectId from bn.dbo.FLEET_Object
where objectactive=1;
declare @ObjectId int
open cur
declare @obj_g varchar(max)
fetch next from cur into @ObjectId
while @@fetch_status=0 begin
set @obj_g =replace(replace(CAST ( (select G.ObjectGroupeName as 'th',';' from [BN].[dbo].[FLEET_ObjectRelation] as Ore1
inner join bn.dbo.FLEET_ObjectGroupe g on Ore1.ObjectGroupeID=G.ObjectGroupeID
where ORE1.ObjectID=@ObjectId FOR XML PATH(''), TYPE)
as nvarchar(max) ),'<th>',''),'</th>','')
insert into @t
values (@ObjectId,@obj_g)
fetch next from cur into @ObjectId
end
close cur
deallocate cur
select o.ObjectId,o.ObjectGarNum,o.ObjectGosNum, t. obj_g,d.code,org.shortname from bn.dbo.FLEET_Object as o
inner join @t as t on t.ObjectId=o.ObjectId
inner join bn.dbo.SYS_DEV_Device as d on d.id=o.device_id
left join bn.dbo.SYS_OrgRequisite as Org on ORG.id=o.OrganizationId
where o.objectactive=1;
Выводится таблица:
ID, Гаражный номер, Гос номер, Список групп через ;, АТ, Орагнизация