select top 30 pr=1,id from R_info_cashewhere contains(cashe_title,@kname)order by 
(case when pr=1 then 0 else 2 end)存储过程中运行提示pr列名无效。请高手指教,怎样才能使自定义列在by 后进行条件排序时有效。

解决方案 »

  1.   

    select top 30 pr=1,id from R_info_cashewhere contains(cashe_title,@kname)order by pr,
    (case when pr=1 then 0 else 2 end)
      

  2.   

    也提示错误小弟用的数据库是sql 2008
      

  3.   

    不是。是提示自定义的列pr无效。问题在于(case when pr=1 then 0 else 2 end)
    这句话中的pr无效。
      

  4.   

    另外你已经设置pr=1,则不存在其他值,所以你最后的case when没有意义.
      

  5.   

    不用什么数据。目前就是如何使自定义列PR,能在order by 后面例用case ,使PR有效。现在就是自定议列PR,在order by 后面使用case时无效。
      

  6.   

    ;with t
    as(
    select top 30 pr=1,id from R_info_cashe
    where contains(cashe_title,@kname)
    )select * from t
    order by pr,(case when pr=1 then 0 else 2 end)
    这样试试
      

  7.   


    --使用CASE WHEN排序实例:
    -->生成测试数据:
    GO
    IF OBJECT_ID('TBL')IS NOT NULL
    DROP TABLE TBL
    GO
    CREATE TABLE TBL(
    日期 DATE,
    备注 VARCHAR(100)
    )
    GO
    INSERT TBL
    SELECT '2012-03-02','B' UNION ALL
    SELECT '2012-03-05','C' UNION ALL
    SELECT '2012-03-06','D' UNION ALL
    SELECT '2012-03-07','E' UNION ALL
    SELECT '2012-03-09','F' UNION ALL
    SELECT '2012-03-11','G' UNION ALL
    SELECT '2012-03-12','H' UNION ALL
    SELECT '2012-03-13','I' UNION ALL
    SELECT '2012-03-15','J' UNION ALL
    SELECT '2012-03-19','K' UNION ALL
    SELECT '2012-03-20','L'
    --不使用union all
    ;with t
    as(
    select *,case when 日期>=getdate() then 1 else 0 end as A
    from tbl
    )select 日期,备注 from t
    order by a,(case when a=1 then 日期 end),
    (case when a=0 then 日期 end) desc/*
    日期 备注
    2012-03-09 F
    2012-03-07 E
    2012-03-06 D
    2012-03-05 C
    2012-03-02 B
    2012-03-11 G
    2012-03-12 H
    2012-03-13 I
    2012-03-15 J
    2012-03-19 K
    2012-03-20 L
    */
      

  8.   

    select * from(
    select top 30 pr=1,id from R_info_cashe
    where contains(cashe_title,@kname))a
    order by  (case when pr=1 then 0 else 2 end)
    这样应该也可以