现有2张表CREATE TABLE [dbo].[tb1](
[id] [int] IDENTITY(1,1) NOT NULL,
[sourcecode] [varchar](50) NULL,
[psourcecode] [varchar](50) NULL,
[sourcename] [varchar](50) NULL
) ON [PRIMARY]CREATE TABLE [dbo].[tb2](
[id] [int] IDENTITY(1,1) NOT NULL,
[sourcecode] [varchar](50) NULL
) ON [PRIMARY]tb1中的sourcecode与psourcecode是父子关系,就像是treeview的菜单如何得到如下结果:谢谢
sourcecode   sourcename  psourcecode sourcename(psourcecode的名称)

解决方案 »

  1.   


    tb1里的数据
    1 01 0 test1
    2 0101 01 test2
    3 0102 01 test3
    4 02 0 test4
    tb2里的数据
    1 01
    2 0101
    3 0102
    4 02
    得到的结果
    sourcecode sourcename psourcecode sourcename(psourcecode的名称)
    01         test1       0          null
    0101       test2       01         test1
    0102       test3       01         test1
    02         test4       0          null
      

  2.   

    --实例2:--> 生成测试数据表:tbIF NOT OBJECT_ID('[tb]') IS NULL
        DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([id] INT,[parentid] INT,[categoryname] NVARCHAR(10))
    INSERT [tb]
    SELECT 1,0,'test1' UNION ALL
    SELECT 2,0,'test2' UNION ALL
    SELECT 3,1,'test1.1' UNION ALL
    SELECT 4,2,'test2.1' UNION ALL
    SELECT 5,3,'test1.1.1' UNION ALL
    SELECT 6,1,'test1.2'
    GO
    --SELECT * FROM [tb]-->SQL查询如下:
    ;WITH T AS
    (
        SELECT *,CAST(ID AS VARBINARY(MAX)) AS px 
        FROM tb AS A
        WHERE NOT EXISTS(SELECT * FROM tb WHERE id=A.[parentid])
        UNION ALL 
        SELECT A.*,CAST(B.px+CAST(A.ID AS VARBINARY) AS VARBINARY(MAX))  
        FROM tb AS A
            JOIN T AS B
               ON A.[parentid]=B.id
    )
    SELECT [id],[parentid],[categoryname] FROM T 
    ORDER BY px
    /*
    id          parentid    categoryname
    ----------- ----------- ------------
    1           0           test1
    3           1           test1.1
    5           3           test1.1.1
    6           1           test1.2
    2           0           test2
    4           2           test2.1(6 行受影响)
    */本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2010/04/23/5518166.aspx
      

  3.   

    在[dbo].[tb1]里面定义一个字段 指向[dbo].[tb2]的id 让他们相等 再查询
      

  4.   

    select m.sourcecode ,
           sourcename = (select sourcename from tb1 n where n.sourcecode = n.sourcecode) ,
           psourcecode = (select psourcecode from tb1 n where n.sourcecode = n.sourcecode) ,
           sourcename = (select sourcename from tb1 n where n.psourcecode = n.sourcecode) 
    from tb2 m 
      

  5.   

    忽略5楼.select m.sourcecode ,
           sourcename = (select sourcename from tb1 n where n.sourcecode = m.sourcecode) ,
           psourcecode = (select psourcecode from tb1 n where n.sourcecode = m.sourcecode) ,
           sourcename = (select sourcename from tb1 n where n.psourcecode = m.sourcecode) 
    from tb2 m select m.sourcecode , n.sourcename , n.psourcecode , 
           sourcename = (select sourcename from tb1 t where t.psourcecode = m.sourcecode)
    from tb2 m , tb1 n 
    where m.sourcecode = n.sourcecode
      

  6.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-09-29 15:36:40
    -- Verstion:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) 
    -- Apr 22 2011 11:57:00 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb1]
    if object_id('[tb1]') is not null drop table [tb1]
    go 
    create table [tb1]([id] int,[sourcecode] varchar(4),[psourcecode] varchar(2),[sourcename] varchar(5))
    insert [tb1]
    select 1,'01','0','test1' union all
    select 2,'0101','01','test2' union all
    select 3,'0102','01','test3' union all
    select 4,'02','0','test4'
    --> 测试数据:[tb2]
    if object_id('[tb2]') is not null drop table [tb2]
    go 
    create table [tb2]([id] int,[sourcecode] varchar(4))
    insert [tb2]
    select 1,'01' union all
    select 2,'0101' union all
    select 3,'0102' union all
    select 4,'02'
    --------------开始查询--------------------------
    ;with f1 as
    (
    select
       a.sourcecode,a.sourcename,a.psourcecode
    from
       tb1 a join tb2 b
    on
       a.sourcecode=b.sourcecode
     ),
     
     f2 as
     (
     select * from f1 as a where not exists(select 1 from f1 where sourcecode=a.psourcecode)
     union all
     select a.* from f1 as a join f2 as b on a.sourcecode=b.psourcecode
     )
    select
       b.sourcecode , b.sourcename,b.psourcecode,a.sourcename
     from
       tb1 b left join  f2 a
     on
       a.sourcecode=b.sourcecode
    ----------------结果----------------------------
    /* sourcecode sourcename psourcecode sourcename
    ---------- ---------- ----------- ----------
    01         test1      0           test1
    0101       test2      01          NULL
    0102       test3      01          NULL
    02         test4      0           test4(4 行受影响)*/
      

  7.   

    我发现我还是写错了  反正就是个 CTE递归 不写了 哎
      

  8.   

    同意,不如如下即可./*
    标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(表格形式显示)
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有子节点的函数
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @id , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.id , @level
        from tb a , @t_Level b
        where a.pid = b.id and b.level = @level - 1
      end
      return
    end
    go--调用函数查询001(广东省)及其所有子节点
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市
    003  001  深圳市
    004  002  天河区
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 10 行)
    */--调用函数查询002(广州市)及其所有子节点
    select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    002  001  广州市
    004  002  天河区(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有子节点
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    003  001  深圳市
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 7 行)
    */drop table tb
    drop function f_cid@@ROWCOUNT:返回受上一语句影响的行数。
    返回类型:integer。
    注释:任何不返回行的语句将这一变量设置为 0 ,如 IF 语句。
    示例:下面的示例执行 UPDATE 语句并用 @@ROWCOUNT 来检测是否有发生更改的行。UPDATE authors SET au_lname = 'Jones' WHERE au_id = '999-888-7777'
    IF @@ROWCOUNT = 0
       print 'Warning: No rows were updated'结果:(所影响的行数为 0 行)
    Warning: No rows were updated/*
    标题:SQL SERVER 2005中查询指定节点及其所有子节点的方法(表格形式显示)
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2010-02-02
    地点:新疆乌鲁木齐
    */create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))
    insert into tb values('001' , null  , N'广东省')
    insert into tb values('002' , '001' , N'广州市')
    insert into tb values('003' , '001' , N'深圳市')
    insert into tb values('004' , '002' , N'天河区')
    insert into tb values('005' , '003' , N'罗湖区')
    insert into tb values('006' , '003' , N'福田区')
    insert into tb values('007' , '003' , N'宝安区')
    insert into tb values('008' , '007' , N'西乡镇')
    insert into tb values('009' , '007' , N'龙华镇')
    insert into tb values('010' , '007' , N'松岗镇')
    goDECLARE @ID VARCHAR(3)--查询ID = '001'的所有子节点
    SET @ID = '001'
    ;WITH T AS
    (
      SELECT ID , PID , NAME 
      FROM TB
      WHERE ID = @ID
      UNION ALL
      SELECT A.ID , A.PID , A.NAME 
      FROM TB AS A JOIN T AS B ON A.PID = B.ID
    )
    SELECT * FROM T ORDER BY ID
    /*
    ID   PID  NAME
    ---- ---- ----------
    001  NULL 广东省
    002  001  广州市
    003  001  深圳市
    004  002  天河区
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(10 行受影响)
    */--查询ID = '002'的所有子节点
    SET @ID = '002'
    ;WITH T AS
    (
      SELECT ID , PID , NAME 
      FROM TB
      WHERE ID = @ID
      UNION ALL
      SELECT A.ID , A.PID , A.NAME 
      FROM TB AS A JOIN T AS B ON A.PID = B.ID
    )
    SELECT * FROM T ORDER BY ID
    /*
    ID   PID  NAME
    ---- ---- ----------
    002  001  广州市
    004  002  天河区(2 行受影响)
    */--查询ID = '003'的所有子节点
    SET @ID = '003'
    ;WITH T AS
    (
      SELECT ID , PID , NAME 
      FROM TB
      WHERE ID = @ID
      UNION ALL
      SELECT A.ID , A.PID , A.NAME 
      FROM TB AS A JOIN T AS B ON A.PID = B.ID
    )
    SELECT * FROM T ORDER BY ID
    /*
    ID   PID  NAME
    ---- ---- ----------
    003  001  深圳市
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(7 行受影响)
    */drop table tb--注:除ID值不一样外,三个SQL语句是一样的。
      

  9.   

    CREATE TABLE [dbo].[tb1](
        [id] [int] IDENTITY(1,1) NOT NULL,
        [sourcecode] [varchar](50) NULL,
        [psourcecode] [varchar](50) NULL,
        [sourcename] [varchar](50) NULL
    ) INSERT  [tb1] select  '01', '0' ,N'test1'
    INSERT  [tb1] SELECT  '0101', '01', N'test2'
    INSERT  [tb1] SELECT  '0102', '01', N'test3'
    INSERT  [tb1] SELECT  '02', '0', N'test4'GO
    SELECT a.sourcecode,a.sourcename,a.psourcecode ,b.sourcename 
    FROM tb1 AS a
    LEFT JOIN tb1 AS b ON a.psourcecode=b.sourcecode
    /*
    sourcecode sourcename psourcecode sourcename
    01 test1 0 NULL
    0101 test2 01 test1
    0102 test3 01 test1
    02 test4 0 NULL
    */
      

  10.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-09-29 15:36:40
    -- Verstion:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) 
    -- Apr 22 2011 11:57:00 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb1]
    if object_id('[tb1]') is not null drop table [tb1]
    go 
    create table [tb1]([id] int,[sourcecode] varchar(4),[psourcecode] varchar(2),[sourcename] varchar(5))
    insert [tb1]
    select 1,'01','0','test1' union all
    select 2,'0101','01','test2' union all
    select 3,'0102','01','test3' union all
    select 4,'02','0','test4'
    --> 测试数据:[tb2]
    if object_id('[tb2]') is not null drop table [tb2]
    go 
    create table [tb2]([id] int,[sourcecode] varchar(4))
    insert [tb2]
    select 1,'01' union all
    select 2,'0101' union all
    select 3,'0102' union all
    select 4,'02'
    --------------开始查询--------------------------
    ;with f1 as
    (
    select
       a.sourcecode,a.sourcename,a.psourcecode
    from
       tb1 a join tb2 b
    on
       a.sourcecode=b.sourcecode
     ),
     
     f2 as
     (
     select * from f1 as a where not exists(select 1 from f1 where sourcecode=a.psourcecode)
     union all
     select a.* from f1 as a join f2 as b on a.sourcecode=b.psourcecode
     )
    select
       a.sourcecode , a.sourcename,a.psourcecode,b.sourcename
     from
       tb1 a left join  f2 b
     on
       a.psourcecode=b.sourcecode----------------结果----------------------------
    /* sourcecode sourcename psourcecode sourcename
    ---------- ---------- ----------- ----------
    01         test1      0           NULL
    0101       test2      01          test1
    0102       test3      01          test1
    02         test4      0           NULL(4 行受影响)*/
      

  11.   

    with f1 as
    (
    select
       a.sourcecode,a.sourcename,a.psourcecode
    from
       tb1 a join tb2 b
    on
       a.sourcecode=b.sourcecode
     ),
    没有这样写过
      

  12.   

    就你的情况而已,TB2表可以不用.
    CREATE TABLE [dbo].[tb1](
        [id] [int] IDENTITY(1,1) NOT NULL,
        [sourcecode] [varchar](10) NULL,
        [psourcecode] [varchar](10) NULL,
        [sourcename] [varchar](10) NULL
    ) ON [PRIMARY]insert into tb1(sourcecode,psourcecode,sourcename) values(1,2,'第一')
    insert into tb1(sourcecode,psourcecode,sourcename) values(2,3,'第二')
    insert into tb1(sourcecode,psourcecode,sourcename) values(3,null,'第三')goselect m.sourcecode ,
           m.sourcename ,
           m.psourcecode ,
           sourcename = (select sourcename from tb1 n where n.sourcecode = m.psourcecode) 
    from tb1 m /*
    sourcecode sourcename psourcecode sourcename 
    ---------- ---------- ----------- ---------- 
    1          第一         2           第二
    2          第二         3           第三
    3          第三         NULL        NULL(所影响的行数为 3 行)
    */
    drop table tb1 , tb2