表 infoid  type   
0    1     
1    1    
2    2    
.    .    
我想写一个查询,找到一种类型的所有信息的详细内容detail,为此我已经写了一个函数f(@id),即输入id就可以通过其他表联接找到detail
现在我写的查询想法是
select 
      id artical_id,
      f(id)   artical_detail  -- 这一条我不知道怎么表达,就是想逐条对对应的id查询到对应的detail. 可是这个参数怎么传递?? 
from  info
where type=**高手指教! 十分感谢!

解决方案 »

  1.   

    可是要如下的内容?--带符号合并行列转换--有表t,其数据如下:
      a b
      1 1
      1 2
      1 3
      2 1
      2 2
      3 1
    --如何转换成如下结果:
      a b
      1 1,2,3
      2 1,2
      3 1 create table tb
    (
       a int,
       b int
    )
    insert into tb(a,b) values(1,1)
    insert into tb(a,b) values(1,2)
    insert into tb(a,b) values(1,3)
    insert into tb(a,b) values(2,1)
    insert into tb(a,b) values(2,2)
    insert into tb(a,b) values(3,1)
    goif object_id('pubs..f_hb') is not null
       drop function f_hb
    go--创建一个合并的函数
    create function f_hb(@a int)
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + ',' + cast(b as varchar) from tb where a = @a 
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select distinct a ,dbo.f_hb(a) as b from tbdrop table tb--结果
    a           b     
    ----------- ------
    1           1,2,3
    2           1,2
    3           1(所影响的行数为 3 行)
    多个前列的合并
    数据的原始状态如下:
    ID  PR   CON  OP    SC 
    001 p    c    差    6
    001 p    c    好    2
    001 p    c    一般  4
    002 w    e    差    8
    002 w    e    好    7
    002 w    e    一般  1
    ===========================
    用SQL语句实现,变成如下的数据
    ID  PR   CON  OPS
    001 p    c    差(6),好(2),一般(4)
    002 w    e    差(8),好(7),一般(1)if object_id('pubs..tb') is not null
       drop table tb
    gocreate table tb
    (
    id varchar(10),
    pr varchar(10),
    con varchar(10),
    op varchar(10),
    sc int
    )
     
    insert into tb(ID,PR,CON,OP,SC) values('001', 'p',    'c',    '差',    6)
    insert into tb(ID,PR,CON,OP,SC) values('001', 'p',    'c',    '好',    2)
    insert into tb(ID,PR,CON,OP,SC) values('001', 'p',    'c',    '一般',  4)
    insert into tb(ID,PR,CON,OP,SC) values('002', 'w',    'e',    '差',    8)
    insert into tb(ID,PR,CON,OP,SC) values('002', 'w',    'e',    '好',    7)
    insert into tb(ID,PR,CON,OP,SC) values('002', 'w',    'e',    '一般',  1)
    goif object_id('pubs..test') is not null
       drop table test
    go
    select ID,PR,CON , OPS = op + '(' + cast(sc as varchar(10)) + ')' into test from tb--创建一个合并的函数
    if object_id('pubs..f_hb') is not null
       drop function f_hb
    go
    create function f_hb(@id varchar(10),@pr varchar(10),@con varchar(10))
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + ',' + cast(OPS as varchar) from test where id = @id and @pr = pr and @con = con
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select distinct id ,pr , con , dbo.f_hb(id,pr,con) as OPS from testdrop table tb
    drop table test--结果
    id         pr         con        OPS                
    ---------- ---------- ---------- -------------------
    001        p          c          差(6),好(2),一般(4)
    002        w          e          差(8),好(7),一般(1)(所影响的行数为 2 行)create table b
    (col varchar(20))insert b values ('a')
    insert b values ('b')
    insert b values ('c')
    insert b values ('d')
    insert b values ('e')
    declare @sql varchar(1024)
    set @sql=''
    select @sql=@sql+b.col+',' from (select col from b) as b
    set @sql='select '''+@sql+''''
    exec(@sql)
      

  2.   

    select ID,artical_detail = dbo.f(ID)
    from info
    where type = **
      

  3.   

    不是前列合并的问题啊. 
    怎么说,就是说通过条件a查询所有符合的id.然后根据查询出来的id,逐条运用函数f获取其他信息. 函数我已经写好了,但是不知道这个函数的参数怎么传递.因为是动态的.
      

  4.   

    f(@id)是标量函数还是表值函数?表值函数不能以查询列作为参数,标量函数才可以。
      

  5.   

    simonhehe(收购猩猩) 这个方法我试过,不行啊.提示我ID是无效的
      

  6.   

    Limpire(昨夜小楼) 标量函数的.
      

  7.   

    select
    id artical_id,
    f(id) artical_detail -- 这一条我不知道怎么表达,就是想逐条对对应的id查询到对应的detail. 可是这个参数怎么传递??
    from info
    where type=**-------------------------是这个意思吗?select id, artical_id = dbo.f(id) from info wher type = **
      

  8.   

    写完整是
    select id  as artical_id,
           f(id)  as artical_detail
    from   info
    where  type=**也就是说查询出来的表是两列, artical_id 和artical_detail
    但是现在提示我函数的参数id列名无效
    这个id就是前面查询出来的id,是动态的.
    想问下参数该怎么传递?
      

  9.   

    select id as artical_id,
    --f(id) as artical_detail
    dbo.f(id) as artical_detail
    from info
    where type=**-----------------自定义函数要加DBOwner(dbo)引用。这就没错了,就这么传递参数的,把列名id作为参数。
      

  10.   

    ...其实这里的只是一个简写,我的原文是这样的  select
           illegal_id id,
           illegal_content_id content_id,
           dbo.f_q_illegalAuthorCityTopic(illegal_id) city  --这里参数用id我也试过,一样的
      from      illegal_info
      where     illegal_editor_id = 0调试的时候是加了dbo.的,但是这样一直没办法运行成功啊.
      

  11.   

    出错了
    OR
    没返回结果集
    OR
    返回结果集不对
      

  12.   

    出错. 消息 208,级别 16,状态 1,第 2 行
    对象名  'illegal_id' 无效。换成id做参数,错误会变成消息 207,级别 16,状态 1,第 5 行
    列名 'id' 无效。
      

  13.   

    请把illegal_info表的结构和函数的定义贴出来.
      

  14.   

    查查你的函数吧:select .dbo.f_q_illegalAuthorCityTopic(100)看看函数能不能执行。
      

  15.   

    表 illegal_info
    ---------------
    illegal_id
    illegal_content_id
    illegal_type
    illegal_editor_id函数
    ALTER FUNCTION [dbo].[f_q_illegalAuthorCityTopic]
    (
    @illegal_id int
    )
    RETURNS varchar(5000)函数具体实现我就不贴了,比较长,也跟这个没太大关系谢谢!
      

  16.   

    去掉函数试试,如果不出错则说明是函数定义出错了:
    select
           illegal_id id,
           illegal_content_id content_id  
    from   illegal_info
    where     illegal_editor_id = 0
      

  17.   

    Limpire(昨夜小楼) 函数没问题. 或者是我函数本身参数设置的不对?
      

  18.   

    :hellowork(一两清风) 我试过.确实是函数有问题
    我现在不知道是函数调用的问题还是定义的问题??
      

  19.   

    各位很抱歉,我重新调试函数的时候,执行语句
    select dbo.f_q_illegalAuthorCityTopic(7)
    报错消息 4121,级别 16,状态 1,第 3 行
    找不到列 "dbo" 或用户定义的函数或聚合 "dbo.f_q_illegalAuthorCityTopic",或者名称不明确。试了其他几个以前写过的正确函数也是这样,这是什么原因哪...昏了我
      

  20.   

    -- check函数的DBOwner是不是dbo你是不是dbo
      

  21.   

    是不是数据库选错了.在查询分析器的工具栏上选择正确的数据库,或者在dbo前加上函数所属数据库名称试试.例如:
    select 数据库名.dbo.f_q_illegalAuthorCityTopic(7)
      

  22.   

    LZ直接點吧,弄半天,也許就是個小錯誤.
    把表結構貼出來,-->希望得到什麼結果.
    或者把function貼出,大家可以幫你找錯誤