下面in (6131,6132,6778,7551,7712,7756,7797,7887,8008,8039,8101,8194......是函数取得的分类数组。
下面这条sql的做用是查询出所列分类的数据。现在的问题是函数返回的分类数组十在太长,使得用varchar(8000)不够用。如果返回像这种"6131,6132,6778,"最后是一个逗号时,执行下面sql时就会语法错误。解决的方法,我是想有没更优化的做法,达到取出这些分类下面的数据的
十在没有的话就看用什么代替varchar(8000)Select Top 6 * From [news] Where AuditingState=1 and c_ID in (6131,6132,6778,7551,7712,7756,7797,7887,8008,8039,8101,8194,8234,8427,8695,8881,8966,9065,9172,9260,9279,9299,9300,9316,9357,9386,6133,6176,6200,6201,6229,6233,6238,6259,6301,6310,6394,6417,6502,6577,6598,6649,6662,6672,6698,6717,6739,6771,6779,6808,6841,6852,6860,6875,6895,6919,6943,6990,7007,7032,7053,7213,7334,7345,7378,7388,7397,7403,7429,7434,7441,7446,7449,7485,7490,7501,7528,7534,7538,7547,7552,7563,7576,7599,7636,7658,7676,7713,7716,7727,7738,7746,7757,7766,7773,7780,7888,7935,7985,8000,8009,8024,8040,8057,8063,8070,8076,8085,8100,8102,8168,8195,8212,8228,8428,8439,8537,8548,8577,8578,8612,8621,8635,8644,8650,8672,8681,8694,8696,8724,8759,8771,8792,8812,8831,8870,8880,8882,8898,8920,8924,8927,8930,8936,8946,8962,8963,8964,8965,8967,8977,8987,9013,9043,9066,9101,9111,9140,9141,9152,9155,9159,9160,9173,9219,9238,9244,9255,9261,9267,9275,9280,9292,9301,9305,9311,9317,9326,9334,9340,9348,9358,9359,9378,9379,9387,9392,9402,9406,9410,6134,6137,6140,6141,6142,6143,6148,6149,6150,6155,6160,6164,6165,...................后面还长得很.) Order by n_ID desc[news]表结构
n_ID    c_ID         Title   content分类表结构
c_ID    c_ParentID   Name
(这个分类是树形,分类记录近3w条,参考贴:http://topic.csdn.net/u/20090828/14/81725919-3f03-4e5d-95b0-65a3884399aa.html)。

解决方案 »

  1.   

    淘宝那么复杂的分类,肯定不少到我的3w条吧,不知道他们是怎么写sql取数据的,呵呵
      

  2.   


    最后一个逗号是因为varchar(8000)装不下了,其实后面还有数据,是被截取了
      

  3.   


    那个数组是函数里面返回的,也是几位老大位搞定的,应该有印象吧。
    varchar(max)用在那函数里报语法错误。
      

  4.   

    将字符串放入一个表,然后JOIN
     if object_id('tb') is not null
      drop table tb
    go
    create table tb( AA varchar(10) )go
    declare @s varchar(8000)
    set @s='1,3,6,10,11,17'
    insert tb
    select id=substring(@s, number, charindex(',', @s + ',', number) - number)
    from master..spt_values
    where type='p' 
        and substring(',' + @s,number,1) = ',' select * from tb join [news] on c_I=aa and AuditingState=1   
      

  5.   


    create function test_funtion()
    returns table 
    as 
     return( select * from 
              (
                select '123' as id  
                union all 
                select '234'
               ) A
            )
    Select Top 6 * From [news] Where AuditingState=1 and c_ID in 
    (
          select id from dbo.test_funtion()
    )
     Order by n_ID desc---试试!
      

  6.   

     To:
     dawugui
    (爱新觉罗.毓华)  哈哈,乌龟怎么找呀!
      

  7.   

    假设字符为 12,13,14,15,16,17取出前三个字符。
    Select * From [news] Where AuditingState=1 and c_ID in(12,13,14)
    UNION ALL
    Select * From [news] Where AuditingState=1 and c_ID in(15,16,17)我想是这个意思吧!