Sunday, December 12, 2010

Misc queries

sp_who2
go
xp_cmdshell 'tasklist /FI "IMAGENAME eq Robocopy.exe"'
go
sp_MSforeachtable 'print ''select ''''?'''' as dbn, COUNT(*) cnt, CHECKSUM_AGG(checksum(*)) crc from db1.? (nolock) union all'''
go
sp_MSforeachtable 'print ''select ''''?'''' as dbn, COUNT(*) cnt, CHECKSUM_AGG(checksum(*)) crc from db2.? (nolock) union all'''

Sunday, December 5, 2010

Returning the Top X row for each group by By Dave Ballantyne, 2010/12/06

IF OBJECT_ID('tempdb..#RunnersBig') IS NOT NULL drop table #RunnersBig 
go
Create Table #RunnersBig
(
RunnerId integer identity ,
Time integer not null,
Age integer not null
)
go
insert into #runnersbig ( Time , Age )
select top 1000000 ABS ( checksum ( newid ()))% 1000 ,
ABS ( checksum ( newid ()))% 99
from sys . columns a cross join sys . columns b cross join sys . columns c
go
create index idxrunnersbig on #runnersbig ( age , time ) include ( runnerid )

with cteN
as
(
select number from master .. spt_values
where type = 'p' and number between 0 and 100
)
Select *
from cteN cross apply ( Select top ( 2 ) * from #RunnersBig where #RunnersBig . Age = cteN . number order by Time ) as runners
order by cteN . number , runners . Time