A表
ID       权限
1        1,3,5
2        3,12,15B表字段
ID       文字
1        查询员工信息
2        删除员工信息
3        添加员工信息
4        更改员工信息
5        查询工作记录
6        删除工作记录
7        更改工作记录
8        添加工作记录
现在我要在存储过程或者函数中返回 A表中的权限,但是不是返回数字,而是返回B表中的文字字段比如返回ID    文字
1     查询员工信息,添加员工信息,更改员工信息应该怎么写??? 我试着用IN语句,,,提示"在应用程序的上下我能使用了非布尔类型的表达式"

解决方案 »

  1.   


    --你的分太少啦,都有点不想搞
    if object_id('A') IS NOT NULL
    DROP TABLE A
    go
    create table A
    (
    id int,
    quanxi nvarchar(20)
    )
    go
    if object_id('B') IS NOT NULL
    DROP TABLE B
    GO
    create table B
    (
    id int,
    wenzi nvarchar(20)
    )
    go
    insert into A
    select 1,'1,3,5' union all
    select 2,'3,5,8'go
    insert into B
    SELECT 1,'查找员工信息' union all
    SELECT 2,'删除员工信息' union all
    SELECT 3,'添加员工信息' union all
    SELECT 4,'更新员工信息' union allSELECT 5,'查找工作记录' union all
    SELECT 6,'删除工作记录' union all
    SELECT 7,'添加工作记录' union all
    SELECT 8,'更新工作记录'
    go
    SELECT * FROM a
    go
    with t as
    (
    select A.id,B.VALUE from
    (
    SELECT ID,value=convert(xml,'<root><v>'+replace(quanxi,',','</v><v>')+'</v></root>') from A
    )A
    outer apply
    (
    select VALUE = N.V.value('.','VARCHAR(100)') FROM A.[value].nodes('/root/v') N(V)
    )b
    )
    --select t.id,b.id,b.wenzi from t INNER join B on t.value = b.id
    ,t2 as
    (
    select t.id,b.id as bid,b.wenzi from t INNER join B on t.value = b.id
    )
    --select * from t2
    select id,
    stuff((select ','+wenzi from t2 a where a.id = b.id for xml path('')),1,1,'') '文字' from t2 b
    group by id
      

  2.   

    难写的查询往往是因为劣质的设计所导致的。大神一般都下班后不上来的,等我这小菜来回答吧,2005以后的写法:----------------------------------------------------------------
    -- Author  :DBA_Huangzj(發糞塗牆)
    -- Date    :2013-05-27 20:36:10
    -- Version:
    --      Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86) 
    -- Jun 17 2011 00:57:23 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[A]
    if object_id('[A]') is not null drop table [A]
    go 
    create table [A]([ID] int,[权限] varchar(7))
    insert [A]
    select 1,'1,3,5' union all
    select 2,'3,12,15'--> 测试数据:[B]
    if object_id('[B]') is not null drop table [B]
    go 
    create table [B]([ID] int,[文字] varchar(12))
    insert [B]
    select 1,'查询员工信息' union all
    select 2,'删除员工信息' union all
    select 3,'添加员工信息' union all
    select 4,'更改员工信息' union all
    select 5,'查询工作记录' union all
    select 6,'删除工作记录' union all
    select 7,'更改工作记录' union all
    select 8,'添加工作记录'--------------开始查询--------------------------
    ;WITH ym AS
    (
    SELECT a.[ID],b.[文字]
    FROM 
    (select
        id, 
        SUBSTRING([权限],number,CHARINDEX(',',[权限]+',',number)-number) as [权限] 
    from
        A a,master..spt_values 
    where
        number >=1 and number<len([权限])  
        and type='p'
        and substring(','+[权限],number,1)=',') a INNER JOIN [b] ON a.[ID]=b.[ID]
    )
    select a.ID,
    stuff((select ','+[文字] from ym b 
           where b.ID=a.ID 
           for xml path('')),1,1,'') '权限'
    from ym a
    group by  a.ID
    ----------------结果----------------------------
    /* 
    ID          权限
    ----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    1           查询员工信息,查询员工信息
    2           删除员工信息,删除员工信息,删除员工信息
    */