SQL将具有相同节点的XML转换为数据集

时间:2019-06-06 20:19:36

标签: sql sql-server xml tsql

我有一个XML列,它们的节点名称相同。

这是XML列的外观。它具有三个Mapping节点。

<Mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Mapping>
    <ID>1</ID>
    <Name>John</Name>
  </Mapping>
  <Mapping>
    <ID>2</ID>
    <Name>Jill</Name>
  </Mapping>
  <Mapping>
    <ID>3</ID>
    <Name>Jason</Name>
  </Mapping>
</Mappings>

预期结果集

   ID          Name
------------------------
   1           John
   2           Jill
   3           Jason

这是我尝试做的

select
    convert(varchar(50), ID.query('./text()')) as ID,
    convert(varchar(50), fName.query('./text()')) as [Name]
from [MyTable]
cross apply XMLCol.nodes('/Mappings/Mapping/ID') as map1(ID)
cross apply XMLCol.nodes('/Mappings/Mapping/Name') as map2(fName)

这将用所有3个名字重复ID 3次。

我只想用相应的名称重复一次ID。

1 个答案:

答案 0 :(得分:3)

你很近。试试

示例

class TBRepoTableViewCell: UITableViewCell {
    @IBOutlet weak var repoLabel: UILabel!
    @IBOutlet weak var urlLabel: UILabel!
    @IBOutlet weak var repoIcon: UIImageView!

    override func layoutSubviews() {
        // Set the width of the cell
        self.bounds = CGRect(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width - 40, self.bounds.size.height)
        super.layoutSubviews()
    }
}

class SourcesViewController: UITableViewController {
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // note that indexPath.section is used rather than indexPath.row
        print("You tapped cell number \(indexPath.row).")
    }

    override func viewWillAppear(_ animated: Bool) {
        setEditing(false, animated: true)
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TBRepoTableViewCell
        let repoArray = array[indexPath.row]
        cell.repoLabel.text = repoArray.repoName
        cell.urlLabel.text = repoArray.repoURL
        cell.repoIcon.image = repoArray.icon

        self.tableView.separatorStyle = .none

        // add borders
        cell.layer.cornerRadius = 10
        cell.clipsToBounds = true
        cell.layer.masksToBounds = true

        return cell
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            array.remove(at: indexPath.row)
            tableView.reloadData()
        }
    }

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        tableView.reloadData()
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
}

返回

Select ID     = xAttr.value('ID[1]', 'int')
      ,[Name] = xAttr.value('Name[1]', 'varchar(100)')
 From  YourTable A
 Cross Apply XMLCol.nodes('/Mappings/Mapping') B(xAttr)