更改多个搜索结果的SQL存储过程

时间:2015-11-17 20:08:12

标签: sql sql-server

我有一些我需要修改的继承代码,以便容纳多个@ParentFolderID参数。目前,传入一个ID。但是我现在需要考虑传入的几个ID并返回每个ID。以下是当前代码。我不太清楚我究竟会从哪里开始。

declare @Values             xml
declare @ValueAttributeID   int
declare @YearAttributeID    int
declare @CategoryID         int
declare @year               int
declare @ParentFolderID     int
declare @DealerAttributeID  int

set @ParentFolderID = 10646615
set @CategoryID = 10646175
set @YearAttributeID = 3
set @ValueAttributeID = 2
set @year = 2014
set @Values = '<values><value id=''1000104'' /></values>'

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

with Parents 
(
    dataid
)
as
(
    select            @ParentFolderID

    Union

    select            child.dataid
    from        DTree parent (NOLOCK)
    inner join  DTree child (NOLOCK) 
            on                parent.dataid           = child.parentid
    where       parent.dataid           = @ParentFolderID
    and child.subtype = 0

)
select      doc.name as '@name',
            doc.dataId as '@id',
            (
                select      allAtts.AttrID as '@id',
                            case when ((allAtts.ValInt is null) and (allAtts.ValStr is null))
                                then cast(allAtts.ValDate as nvarchar(255))
                                 when (allAtts.ValInt is null and allAtts.ValDate is null)
                                then cast(allAtts.ValStr as nvarchar(255))
                                 when (allAtts.ValDate is null and allAtts.ValStr is null)
                                then cast(allAtts.ValInt as nvarchar(255))
                            end as '@val'
                from        LLAttrData allAtts (NOLOCK) 
                where       allAtts.id = doc.dataid
                for xml path('attribute'), TYPE         
            )

from        DTree category (NOLOCK)


inner join  LLAttrData value (NOLOCK) 
on          category.dataid = value.defid

--Changes per environment (value attribute)
and         value.AttrID            = @ValueAttributeID

--Check for values
inner join @Values.nodes('//value') as A(att)
on          A.att.value('@id', 'nvarchar(255)') = value.ValStr

--Changes per environment (year attribute)
inner join  LLAttrData y (NOLOCK) 
on          category.dataid = y.defid

--Changes per environment (year attribute)
and         y.AttrID                = @YearAttributeID

--Check for year
and         year(y.valDate)         = @year

inner join  DTree doc (NOLOCK)
on          value.id    = doc.dataid
and         y.id        = doc.dataid


inner join  Parents parent
on          parent.dataid   = doc.parentid

--Must be associated to the  category
where       category.dataid = @CategoryID -- This is the hard coded category ID

order by doc.dataid --, allAtts.AttrID 

FOR XML PATH('document'), Root('documents')`

0 个答案:

没有答案