SQL Server

Identity sloupce a práce s nimi

Dnes se podíváme na stručný přehled všech důležitých funkcí, globálních proměnných a DBCC příkazů pro práci s IDENTITY hodnotami v SQL Serveru. Programátoři často chybují, kdy použít kterou z nich, zejména pak z neznalosti toho, jak se jednotlivé funkce chovají v rámci kontextu, ve kterém jsou volány (tělo uložené procedury,…

Read more
SQL Server

Why it matters if COLLATE is used in WHERE

There is a lot of things we shouldn’t do or use in WHERE clause, i.e scalar functions or data types conversion. They can lead to a suboptimal execution plan and cause serious performance degradation. A similar situation exists for the COLLATE statement. If it is used in WHERE it prevents…
SQL Server

Spojení řetězců pomocí FOR XML PATH

Spojení textových řetězců z více sloupců na řádek můžeme v SQL Serveru v edicích 2016 a nižších vyřešit pomocí FOR XML PATH a je to i vůbec nejpoužívanější způsob. V SQL Serveru 2017 dosáhneme stejného výsledku mnohem elegantnější cestou pomocí funkce STRING_AGG(), která ve všem plně nahradí FOR XML PATH.
SQL Server

Why to avoid functions in WHERE?

It’s well known that we should use functions in the WHERE clause because they can have a negative impact or query execution because of the suboptimal execution plan is generated. I have prepared a really simple example of how to demonstrate this. It also demonstrates one rule that we should…

Read more
SQL Server

CONTEXT_INFO()

Session context information enables applications to set binary values of up to 128 bytes that can be referenced in multiple batches, stored procedures, triggers, or user-defined functions operating on the same session. You can set a session context value by using the SET CONTEXT_INFO statement and retrieve it later by…
SQL Server

CONVERT() datetime to string - Table of output values

I’m converting DATETIME values to string nearly every day when doing regular T-SQL development. After years I remember a lot of format parameter values to get the right string representation of input DATETIME value. But still time to time something specific is needed. This is why I have created a…
SQL Server

ISNUMERIC()? No, thank you!

Can we trust ISNUMERIC() function when checking if it is safe cast string value to numeric data type? Definitely not, because it´s more or less unusable for this purpose, because we can´t be sure what is hidden in string to be converted. And trust me: there is lot of surprises…

Read more
SQL Server

Change in Query Plan Can Affect Query Result Set (Table Spool)

It´s legitimate expectation that a SELECT statement will return consistent results independently on number of rows returned or an internal implementation of query processing. But it is not always true. I will demonstrate it on a simple query using NEWID() function. This function should return new value on every call…
SQL Server

How to prevent views with outdated metadata?

SQL Server is persisting various metadata when a new view is created in SQL Server. When underlying objects are changed later then view metadata aren’t updated automatically and this outdated metadata can cause several issues later. Let’s see one simple example. We will create a dbo.SampleTable table and create view…