Blog Posts

Blog Post

Find Most executed queries

SELECT TOP 50
QueryState.execution_count
,OBJECT_NAME(objectid)
,query_text = SUBSTRING(
qt.text,
QueryState.statement_start_offset/2,
(CASE WHEN QueryState.statement_end_offset = -1
THEN len(convert(nvarchar(max), qt.text)) * 2
ELSE QueryState.statement_end_offset
END - QueryState.statement_start_offset)/2)
,qt.dbid
,dbname = db_name(qt.dbid)
,qt.objectid
FROM sys.dm_exec_query_stats QueryState
CROSS APPLY sys.dm_exec_sql_text(QueryState.sql_handle) as qt
ORDER BY...

2018-09-18

2,103 reads

Blog Post

Get all queries running against any specific table

/*Get list of all queries hitting any specific tables*/SELECT DISTINCT TOP 100ProcedureName = OBJECT_SCHEMA_NAME(sqlTxt.objectid) + '.' + OBJECT_NAME(sqlTxt.objectid),SQLStatement = SUBSTRING(sqlTxt.Text,(QueryState.statement_start_offset/2)+1,CASE QueryState.statement_end_offsetWHEN -1 THEN DATALENGTH(sqlTxt.text)ELSE QueryState.statement_end_offsetEND...

2018-09-18

3,882 reads

Blogs

Automated SQL Server Benchmarking with HammerDB and Docker: A Complete Testing Framework

By

I’m excited to announce the release of a new open-source project that fully automates...

T-SQL Snapshot Backups on Availability Group Secondary Replicas

By

One of my favourite features of SQL Server 2022 was the T-SQL Snapshot Backups....

The Book of Redgate: Hours

By

I have said and written this many times: Redgate Software is the type of...

Read the latest Blogs

Forums

ORDER BY alias

By aniap

Comments posted to this topic are about the item ORDER BY alias

Optional Parameter Plan Optimization in SQL Server 2025: Fixing the Kitchen-Sink Search Procedure

By vgupta

Comments posted to this topic are about the item Optional Parameter Plan Optimization in...

AI Experience Gap that we keep talking about

By Pat Wright

Comments posted to this topic are about the item AI Experience Gap that we...

Visit the forum

Question of the Day

ORDER BY alias

There is a table tmp_tab:

CREATE TABLE tmp_tab (
id int,
val int
);
INSERT INTO tmp_tab VALUES
(1, 1),
(2, NULL),
(3, 3),
(4, 4),
(5, 5);
You want to order the rows ids by the following expression:
    ISNULL(val, id) + 1
Which of the following queries produces the expected ordering and why? (Select all correct)

See possible answers