例如新闻记录表和新闻类别表:
news(表名)
newsid
title
content
typeidnewstype(新闻类型表)
typeid
newsid
typename能不能建立一个视图,把查询新闻的类型以逗号的形式隔开,放在一个字段里面?
比如视图为"vw_news"
newsid "1"
title   "新闻标题"
content "新闻内容"
type "武侠,言情"我知道程序里面肯定能做到,我现在要把查询的结果放到DateTable,然后导出成Excel,所以没有办法,请大家帮忙参谋一下!

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/5006/5006846.xml?temp=.8458063
    看看,你写的要求有点乱
      

  2.   

    if object_id('tba') is not null
    drop table tba
    go
    create table TBA( col1 varchar(10), col2 varchar(5) )
    insert into tba 
    select 'A'  ,'1'  union all
    select 'A'  ,'2'  union all
    select 'A'  ,'3'  union all
    select 'B'  ,'1'  union all
    select 'B'  ,'6'  union all
    select 'C'  ,'2'  union all
    select 'C'  ,'3'GO
    if object_id('fntry') is not null
    drop function fntry
    GO
    ----创建字符串连接函数
    create function fntry(@id varchar(10))
    returns varchar(1000)
    as
    begin
    declare @str varchar(1000)
    set @str = ''
    select @str = @str + ',' + col2 from TBA where col1 = @id
    return stuff(@str,1,1,'')
    end
    GO
    ----调用函数
    select col1,dbo.fntry(col1) from TBA group by col1
      

  3.   

    请问下面这段放到哪儿?不会也放到自定义函数里面吧?实在不好意思阿,请明示,马上结帖,非常感谢!
    if object_id('tba') is not null
    drop table tba
    go
    create table TBA( col1 varchar(10), col2 varchar(5) )
    insert into tba 
    select 'A'  ,'1'  union all
    select 'A'  ,'2'  union all
    select 'A'  ,'3'  union all
    select 'B'  ,'1'  union all
    select 'B'  ,'6'  union all
    select 'C'  ,'2'  union all
    select 'C'  ,'3'GO
    if object_id('fntry') is not null
    drop function fntry
    GO