我使用了一个查询,它在一个表的一列中拆分文本,并从另一个表中检索相应的id。
以下是查询:
DECLARE @X XML
DECLARE @STR VARCHAR(MAX) = ''
SELECT @STR = @STR + ';' + P_AUTHOR
FROM sub_aminer_paper
WHERE PID = 4
ORDER BY PID
SELECT @STR = substring(@STR,2,len(@STR))
SELECT @X = CONVERT(xml,' <root> <s>' + REPLACE(@STR,';','</s> <s>') + '</s> </root> ')
SELECT aid as [Author_id], name as [Author_Name]
FROM sub_aminer_author s
INNER JOIN
(SELECT
row_number() OVER(ORDER BY (SELECT null)) AS rn,
T.c.value('.','varchar(max)') AS value
FROM
@X.nodes('/root/s') T(c) ) t ON t.value = s.name
ORDER BY rn
如果列p_author
包含此值,即
Sushil Jajodia;Peter A. Ng;Frederick N. Springsteel
然后此查询提供以下输出,即
Author_id Author_Name
578328 Sushil Jajodia
865779 Peter A. Ng
669143 Frederick N. Springsteel
现在我希望Author_id
跳过第一个作者ID,例如我需要这样的输出:
Author_id Author_Name
865779 Peter A. Ng
669143 Frederick N. Springsteel
然后我还要在另一个表中插入这些Author_id
。请帮忙解决此问题。
答案 0 :(得分:0)
只需添加WHERE rn>1
DECLARE @X XML
DECLARE @STR VARCHAR(MAX)=''
SELECT @STR = @STR+';'+P_AUTHOR
FROM sub_aminer_paper
WHERE PID = 4
ORDER BY PID
SELECT @STR = substring(@STR,2,len(@STR))
SELECT @X = CONVERT(xml,' <root> <s>' + REPLACE(@STR,';','</s> <s>') + '</s> </root> ')
SELECT aid as [Author_id], name as [Author_Name]
FROM sub_aminer_author s
INNER JOIN (SELECT row_number()
OVER(ORDER BY (SELECT null)) AS rn, T.c.value('.','varchar(max)') AS value
FROM @X.nodes('/root/s') T(c)
) t ON t.value = s.name
WHERE rn>1
ORDER BY rn