遍历bulkcolumn SQL Server获取静态字符串之间的值

时间:2018-07-17 16:28:59

标签: sql sql-server stored-procedures sql-server-2014

我在SQL Server中有一个currQuestion列,其中包含我们网站页面之一的HTML转储。我对列中两个静态字符串/标签之间的值特别感兴趣。我需要这些静态字符串之间的所有数据:class App extends Component { constructor() { super(); this.state = { currQuest: null } } handleRandomTruth = () => { this.setState({ currQuest: truth[Math.floor(Math.random() * truth.length)] }) } handleRandomDare = () => { this.setState({ currQuest: dare[Math.floor(Math.random() * dare.length)] }) } render() { return ( <div className="App"> <div className="current-player"> <h3>current player</h3> </div> <div className="next-player"> <h3>next player</h3> </div> <div className="questions"> {this.state.currQuest ? <div>{this.state.currQuest.question}</div> : ''} </div> <button className="btn-truth" onClick= {this.handleRandomTruth}>Truth</button> <button className="btn-dare" onClick= {this.handleRandomDare}>Dare</button> <button className="btn-home" >Home</button> </div> )}; } NVARCHAR(MAX)

该列包含这两个字符串大约50次,并且我需要在字符串之间使用所有50个值,因此我一直在考虑使用游标在该列中循环,但是我的SQL知识是这里的瓶颈。

有没有人可以帮助我建立此查询?

先谢谢大家。

1 个答案:

答案 0 :(得分:0)

declare @htmlString  nvarchar(Max)
declare @Id  int
declare @strtIndex  int
declare @endIndex  int
declare @href  nvarchar(Max)
declare @result table (Id int, href nvarchar(Max))

--replace this line with your query
set @htmlString = 
'<!DOCTYPE html>
   <html>
   <head>
        <title></title>
        <meta charset="utf-8" />
   </head>
  <body>
    <h1>Hello world</h1>
    <a href="/a/jiu/sddddddddd/"></a>
    <a href="/b/jiu/sddddddddd/"></a>
    <a href="/c/jiu/sddddddddd/"></a>
    <a href="/d/jiu/sddddddddd/"></a>
</body>
</html>'

set @Id = 0
WHILE LEN(@htmlString) > 0
BEGIN
    set @Id = @Id+1
    set @strtIndex = CHARINDEX('href="', @htmlString) + LEN('href="')
    set @endIndex = CHARINDEX('/"', @htmlString, @strtIndex)

    IF @endIndex > 0
        BEGIN
            set @href =SUBSTRING(@htmlString, @strtIndex, @endIndex-@strtIndex)
            insert into @result (Id , href)
            values(@Id , @href)
            SET @htmlString = SUBSTRING(@htmlString, @endIndex +1,LEN(@htmlString))
        END
    ELSE
        BEGIN
            SET @htmlString = NULL
        END
END

select * from @result