Last executed queries for a specific database

This works for me to find queries on any database in the instance. I’m sysadmin on the instance (check your privileges): SELECT deqs.last_execution_time AS [Time], dest.text AS [Query], dest.* FROM sys.dm_exec_query_stats AS deqs CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest WHERE dest.dbid = DB_ID(‘msdb’) ORDER BY deqs.last_execution_time DESC This is the same answer that Aaron Bertrand provided … Read more

SQL Server equivalent of substring_index function in MySQL

Try this solution based on T-SQL and XQuery((root/row)[position() <= sql:variable(“@count”)]): T-SQL Scalar function: CREATE FUNCTION dbo.SUBSTRING_INDEX ( @str NVARCHAR(4000), @delim NVARCHAR(1), @count INT ) RETURNS NVARCHAR(4000) WITH SCHEMABINDING BEGIN DECLARE @XmlSourceString XML; SET @XmlSourceString = (SELECT N'<root><row>’ + REPLACE( (SELECT @str AS ‘*’ FOR XML PATH(”)) , @delim, N'</row><row>’ ) + N'</row></root>’); RETURN STUFF ( … Read more

Why the SQL Server ignore the empty space at the end automatically?

SQL Server is following the ANSI/ISO standard for string comparison. The article How SQL Server Compares Strings with Trailing Spaces explains this in detail. SQL Server follows the ANSI/ISO SQL-92 specification… on how to compare strings with spaces. The ANSI standard requires padding for the character strings used in comparisons so that their lengths match … Read more

Adding a query hint when calling Table-Valued Function

I came across this: https://entityframework.codeplex.com/wikipage?title=Interception And it appears that you can do something like this: public class HintInterceptor : DbCommandInterceptor { public override void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext) { command.CommandText += ” option (recompile)”; base.ReaderExecuting(command, interceptionContext); } } And register it like this (I did it in Application_Start of global.asax.cs): DbInterception.Add(new HintInterceptor()); And it will … Read more

Sequence vs identity

I think you will find your answer here Using the identity attribute for a column, you can easily generate auto-incrementing numbers (which as often used as a primary key). With Sequence, it will be a different object which you can attach to a table column while inserting. Unlike identity, the next number for the column … Read more