Posts

Showing posts from January, 2021

SQL Server: Insert rows in a table and at the same time retrieve the id inserted with only a single statement? the OUTPUT and the OUTPUT INTO clauses

Image
Hello friends! Two or three months ago we talk about How to get the Last Inserted Identity value You know that our goal is to find always the fastest method. According to that post we found that the fastest ways is to use the @@IDENTITY, SCOPE_IDENTITY or IDENT_CURRENT(T) commands: So a good way to insert rows in a table and retrieve the last identity is this: Oh yes! Cool! but we need again 2 T-SQL commands! How to improve? The OUTPUT and the OUTPUT INTO clauses "It's easy and plan" For the example create a table named Product and a clustered index: Create table Product (id int identity(1,1), codice varchar(20)) Create clustered index pk_Product on Product(id) Then execute the following statements: Insert into Product (codice) OUTPUT inserted.id Values ('A')  You will immediately noticed that the T-SQL command return the id inserted: The simply tips is the OUTPUT clause . Very helpfuf is also the OUTPUT INTO clause: Declare @val table (id int) Insert into Prod...

Sql Server: The execution plan and the cost of the operators inside it. The clustered index scan operator. Part 1.

Image
Hi friends! Welcome back, today we will not continue to talk about the parser part of SQL Server as in the previous post, we will do it in the next post! Today we start a short new series of posts that talks about the Execution plan and how to calculate the cost of the operators inside it. Are you ready? Three, two, one ...go!   A bit of theory One of the less explored part of SQL Server is the logic contained inside the optimizer. We already talked about the optimizer in greater detail here It's goal is to find a good execution plan in order to resolve our Query. For example, is the optimizer that chooses which type of JOIN to use to resolve the T-SQL command. So you can follow me step by step, i have prepared this example for you Create table table1 (id int identity (1,1), codice varchar(40), descr varchar(80)) Create table table2 (id int identity (1,1), codice varchar(40), descr varchar(80)) Create table table3 (id int identity (1,1), codice varchar(40), de...

SQL Server is a compiler! & Where T-SQL tokens are stored?

Image
Hello friends, First of all i want to wish you an happy new yea r! I know 2020 hitted hard for many of us and hitted hard also for me too! But it is necessary to be positive, go on and working hard. In this manner who can stop us? nobody can! For the first post of this 2021 i would talk about the SQL Server parser exploring how it works . We have already talked about this topic in the beginning of the 2020 with with a series of posts: Inside the SQL Server Query Optimizer - part 1 Introduction and the input tree Inside the SQL Server Query Optimizer - part 2 All about the Simplification   Inside the SQL Server Query Optimizer - part 3 Cardinality estimation etc But now it is the time  to go into a deep detail! Are you ready? go! SQL Server is a compiler! Yes, SQL Server is a compiler. The input of the compiler is the T-SQL command you send to the engine. Every batch you send is analized and traduced into a series of more simple instructions. The heart of the compiler is a pars...