T-SQL stored procedure that accepts multiple Id values

后端 未结 6 956
既然无缘
既然无缘 2020-11-22 03:06

Is there a graceful way to handle passing a list of ids as a parameter to a stored procedure?

For instance, I want departments 1, 2, 5, 7, 20 returned by my stored

6条回答
  •  礼貌的吻别
    2020-11-22 03:45

    You could use XML.

    E.g.

    declare @xmlstring as  varchar(100) 
    set @xmlstring = '-1' 
    
    declare @docid int 
    
    exec sp_xml_preparedocument @docid output, @xmlstring
    
    select  [id],parentid,nodetype,localname,[text]
    from    openxml(@docid, '/args', 1) 
    

    The command sp_xml_preparedocument is built in.

    This would produce the output:

    id  parentid    nodetype    localname   text
    0   NULL        1           args        NULL
    2   0           1           arg         NULL
    3   2           2           value       NULL
    5   3           3           #text       42
    4   0           1           arg2        NULL
    6   4           3           #text       -1
    

    which has all (more?) of what you you need.

提交回复
热议问题