参考:http://community.csdn.net/Expert/topic/3173/3173909.xml?temp=.7441065

解决方案 »

  1.   

    用函数处理的话,能不能只用一个函数?因为不想给SaleList的每个字段都写函数。还有很多这样的表,每个字段都写的话会很麻烦。最好能够最调用一次函数,曾经想过用“内嵌表值函数”,不过一直没有能够实现。
      

  2.   

    而且,SaleList的每个字段调用一次函数的话,速度上是否会有所影响?
      

  3.   

    通常这种情况都是写函数来处理的虽然需要处理的字段很多,但是函数的功能都是一样,结构也一样,你可以用写一段SQL,来自动生成这样的函数调用函数比不 调用函数速度上要打个折扣,这也是避免不了的
      

  4.   

    可以只写一个函数,注意设计传入的参数,按照你的要求,初步看了以下,可能需要传入:
    选取信息表   (saleList)
    匹配字段        (saleList.id)
    匹配值          (SaleList.id)
    选择字段        (SaleList.Product)
      

  5.   

    --每个表写个函数可以(当然,如果你的其他表结构类似,也可以参考下面的书写方式,合并写一个函数)
    create function f_str(
    @Id int,
    @fdname sysname
    )returns varchar(8000)
    as
    begin
    declare @re varchar(8000)
    set @re=''
    if @fdname='ListId'
    select @re=@re+','+cast(ListId as varchar) from SaleList where id=@id
    else if @fdname='Product'
    select @re=@re+','+Product from SaleList where id=@id
    else if @fdname='Quantity'
    select @re=@re+','+cast(Quantity as varchar) from SaleList where id=@id
    else if @fdname='Price'
    select @re=@re+','+cast(Price as varchar) from SaleList where id=@id
    else if @fdname='Amount'
    select @re=@re+','+cast(Amount as varchar) from SaleList where id=@id
    return(stuff(@re,1,1,''))
    end
    go--调用函数实现你的要求
    select Id,AcctDate,[User]
    ,ListId=dbo.f_str(id,'ListId')
    ,Product=dbo.f_str(id,'Product')
    ,Quantity=dbo.f_str(id,'Quantity')
    ,Price=dbo.f_str(id,'Price')
    ,Amount=dbo.f_str(id,'Amount')
    from Sale
      

  6.   

    上面只是给了一个表的写法,如果其他表结构类似,你也可以加多一个表名的参数,用上面的if判断方式进行处理.因为自定义函数不支持动态SQL语句,所以得逐个写.(不过处理也不复杂)
      

  7.   

    如果SaleList表还要连接其他表才能构成,比如,通常,SaleList表只记录ProductId,另外有Product表记录商品的详细内容,这样要读ProductName还需要连表。这种情况下,是不是干脆用迪卡尔乘积读出来,让界面来解决会更好一些?
        期待大家的答复