日期:2014-05-17  浏览次数:20531 次

sqlserver with cte语句问题
 
  语句是这样的
with cte as(select distinct top 10 QStandType.QStandTypeID,QStandType.QStandTypeName,ROW_NUMBER() over(order by QStandID asc) as num from QStand
inner join containstable(QStand,(QStandFRContent,QStandGRContent),'混凝土 and 强度',50)as k
on QStandID=k.[key]
inner join QStandType on
QStand.QStandTypeID=QStandType.QStandTypeID
order by QStandTypeID asc
)
select * from cte where num>5

  查询结果如下:
  9 建筑地基处理技术规范 JGJ79—2002 20
  11 锚杆喷射混凝土支护技术规范 GB50086-2001 23
  11 锚杆喷射混凝土支护技术规范 GB50086-2001 24
  11 锚杆喷射混凝土支护技术规范 GB50086-2001 25
  11 锚杆喷射混凝土支护技术规范 GB50086-2001 26
  15 大体积混凝土施工规范 GB50496-2009 40
  15 大体积混凝土施工规范 GB50496-2009 41
  16 装配式大板居住建筑设计与施工规程 JGJ1-91 43
  17 高层建筑混凝土结构技术规程 JGJ3-2002 44
  19 冷拔钢丝预应力混凝土构件设计施工规程 JGJ19-92 45

  这里有两个问题
 1.查询出来的结果,不是5条,应该是后5条的,个人感觉containstable前面的语句没用上
 2.查询出来的结果有重复,但是我在第一句就写了 select distinct

  这里主要是涉及到了分页存储过程
  请各位大侠指点。。 实验成功的语句

  declare  @ContentOne nvarchar(50)
  declare  @ContentTwo nvarchar(50)
  set @ContentOne='混凝土'
  set @ContentTwo='强度'
  declare @sql nvarchar(500)
  set @sql='select distinct top 5 QStandType.QStandTypeID,QStandType.QStandTypeName from QStand
  inner join
  containstable(Qstand,([QStandFRContent],[QStandGRContent]),''("'+@ContentOne+'")and  
  ("'+@ContentTwo+'")'',5000) as k
  on QStandID=k.[key]
  inner join QStandType on
  QStand.QStandTypeID=QStandType.QStandTypeID
  order by QStandTypeID'
  exec sp_executesql @sql

  结果如下:
  3 建筑基坑支护技术规范 JGJ120-99
  4 湿陷性黄土地区建筑基坑工程安全技术规程 JGJ167-2009
  5 膨胀土地区建筑技术规范 GBJ112-87
  6 湿陷性黄土地区建筑规范 GB50025-2004
  7 建筑边坡工程技术规范 GB50330-2002
------最佳解决方案--------------------
select distinct top 10 

改为试试

select top 10 distinct

------其他解决方案--------------------
order by QStandTypeID desc呢?

------其他解决方案--------------------
引用:
  语句是这样的
with cte as(select distinct top 10 QStandType.QStandTypeID,QStandType.QStandTypeName,ROW_NUMBER() over(order by QStandID asc) as num from QStand
inner join containstable(QStand,(QStandFRCon……


  跟这个没关系呢。。 asc 和 desc一个正反序的问题 没差别,结果只是倒了个个,没有解决根本,谢谢你的回答。。
------其他解决方案--------------------
你把存储过程print 出来看看。
------其他解决方案--------------------
--把QStandID 放cte然后查 看看
SELECT  *
    FROM    cte
    WHERE   num > 5
    ORDER BY QStandID ASC