zoukankan      html  css  js  c++  java
  • sqlserver FOR xml

    --sql FOR xml

    create table ClassInfo
    (
        CID  int identity(1,1), --本表ID
        Cnumber int,            --班级人数
        CName varchar(10)       --班级名称
    )

    go
    insert ClassInfo
           select 20,'一年级1班'
    union  select 20,'一年级2班'
    union  select 20,'一年级3班'
    union  select 20,'一年级4班'
    go
    select * from ClassInfo
    --Students (CID 班级表外键,SID学生信息表ID学生的学号就是,SName)
    create table Students
    (
        [SID] int identity(1,1), --学号
        CID int,                 --班级ID 
        SName varchar(10)         --姓名       
    )
    go
    insert into Students
          select 1,'张1'
    union select 1,'张2'
    union select 1,'张3'
    union select 1,'张4'
    union select 2,'张5'
    union select 2,'张6'

    --CID 本表ID,Cnumber 班级人数,CName
    select * from ClassInfo
    select * from Students

    select * from ClassInfo WHERE CName='一年级1班'
    SELECT CID,Cnumber,CName FROM ClassInfo WHERE CName='一年级1班'   FOR xml raw

        SELECT ClassInfo.CID,ClassInfo.Cnumber,ClassInfo.CName,Students.SID,Students.SName
        FROM ClassInfo  JOIN dbo.Students ON Students.CID=dbo.ClassInfo.CID 
        ORDER BY CID FOR xml raw 

    --raw to auto 把行数据变成节点数据
        SELECT ClassInfo.CID,ClassInfo.Cnumber,ClassInfo.CName,Students.SID,Students.SName
        FROM ClassInfo  JOIN dbo.Students ON Students.CID=dbo.ClassInfo.CID 
        ORDER BY CID FOR xml auto 
    --root属性 添加根结点
         SELECT ClassInfo.CID,ClassInfo.Cnumber,ClassInfo.CName,Students.SID,Students.SName
         FROM ClassInfo  JOIN dbo.Students ON Students.CID=dbo.ClassInfo.CID 
         ORDER BY CID FOR xml auto ,root('studentInfo')
    --Elements 属性 把属性变成了节点的方式
           SELECT ClassInfo.CID,ClassInfo.Cnumber,ClassInfo.CName,Students.SID,Students.SName
           FROM ClassInfo  JOIN dbo.Students ON Students.CID=dbo.ClassInfo.CID 
           ORDER BY CID FOR xml auto ,root('studentInfo'),Elements
    --path属性
            SELECT ClassInfo.CID,ClassInfo.Cnumber,ClassInfo.CName,Students.SID,Students.SName
            FROM ClassInfo  JOIN dbo.Students ON Students.CID=dbo.ClassInfo.CID 
            ORDER BY CID FOR xml path ,root('studentInfo')
    /*
    Path 是以数据基本结构也就是以字段为结点的形式返回Xml文档(这样说不专业可能容易理解)
    Raw 是把数据里所有的字段以属性的方式显示出Xml文档
    Auto 自动关连到相应的项目,和Raw一样在不加Elements属性时以这种方式返回数据
    */

  • 相关阅读:
    [Leetcode] Combination Sum II
    [Leetcode] Search in Rotated Sorted Array
    [LeetCode] Number of 1 Bits
    [Jobdu] 题目1373:整数中1出现的次数(从1到n整数中1出现的次数)
    [Jobdu] 题目1377:缓变序列
    [LeetCode] Add Two Numbers
    [LeetCode] Repeated DNA Sequences
    [LeetCode] Reverse Bits
    [Jobdu] 题目1139:最大子矩阵
    [LeetCode] Clone Graph
  • 原文地址:https://www.cnblogs.com/dingdingmao/p/3146535.html
Copyright © 2011-2022 走看看