用标准sql的with实现递归查询(sql2005以上肯定支持,sql2000不清楚是否支持): with subqry(id,name,pid) as ( select id,name,pid from test1 where id = 5 --指定id union all select test1.id,test1.name,test1.pid from test1,subqry where test1.pid = subqry.id)select* from subqry;
转自:walkingp 1.显示行号 如果数据没有删除的情况下主键与行号是一致的,但在删除某些数据,行号就与主键不一致了,这时需要查询行号就需要用新的方法,在SQL Server2005之前,需要使用临时表,但在SQL Server2005中,使用ROW_NUMBER()非常方便. select row_number() over (order by UseriD) as rowNum,* from UserInfo 查询结果: 2.随机查询 有的时候我们需要查询出的数据是随机排序的,newid
在做一个功能的时候,需要在oracle数据库中查询指定某一天的数据. 如果是简单的当前日期前后几天,也好办 AND TO_CHAR(Rct.Creation_Date, 'YYYY-MM-DD')=to_char(sysdate-1,'yyyy-MM-dd') 即可 但是指定日期就不好弄了 可以这样做 select * from test1 t where t.end_datebetween to_date('2014-10-24 00:00:00','yyyy-mm-dd hh24:mi:ss
SQLServer 查询数据死锁语句 我们可以通过以下SQL 语句用来查询,当前数据中存在的死锁语句,定位数据库出现死锁的根本原因. --每秒死锁数量 SELECT * FROM sys.dm_os_performance_counters WHERE counter_name LIKE 'Number of Deadlocksc%'; --查询当前阻塞 WITH CTE_SID ( BSID, SID, sql_handle ) AS ( SELECT blocking_session