Posts

Showing posts from July, 2021

SQL Server, all about the Common Table Expressions (CTEs)

Image
Hi friends! Today we talk about Common Table Expression which is much more often referred to with the acronym CTE . What are they? When someone ask me this question I answer using the simplest way I can define them : A CTE is a temporary Result set to which we associate a name and to which we can refer in the various statements.   About the CTE   CTEs were introduced few years ago with the release of the 2005 version of SQL Server. The goal was to provide an extended syntax that would allow you to write queries more easily . The basic syntax for using CTEs is really simple: it is sufficient to precede our INSERT, UPDATE or DELETE statement with the keyword WITH followed by the name we want to give to the Query, followed in turn by the keyword AS. A Simple example WITH MAXCODICE AS (SELECT MAX (CODE) AS CODE FROM LIST) SELECT * FROM MAXCODICE   Very Very important: CTEs bring the ability to write recursive queries .   We will therefore see the two cases o

SQL Server, use transactions in a smarter way. The save transaction

Image
Hi Guys, Today we will talk more about transactions ... Oh no, not again! I am hearing some of you are saying  …you have already explained it yesterday! Yes exactly but today's post is lighter and in just 5 minutes we see how to use transactions in a smarter and more flexible way! Ready!           SQL Server, such as Oracle and others RMDBS, allows you to associate a name with a transaction and even allows to you to perform a partial rollback!  If you think about it, having the ability to open a transaction and at the end of a procedure to commit or rollback alltogheter is a bit limiting . For example, in case you have to go back to the beginning of the whole procedure, which may be very long.   Between Save Transaction and Partial Rollback SQL Server provides this two commands:  SAVE TRANSACTION transaction_name ROLLBACK TRANSACTION transaction_name   Where transaction_name is the name of the transaction. With the SAVE TRANSACTION <tran> you can

SQL Server: Transactions, Lock e Deadlock. A little bit of theory explained in a simple way!

Image
Hi guys, Hello everyone and welcome again! Today I wanted to talk to you about some basic concepts related to relational databases that are absolutely worth knowing ! . We will talk about  Transactions , lock  and also about a particular type of lock called  deadlock . I'll tell you some theory but don't worry: as usual I will try to be as clear as possible ! You are ready? So happy reading!!! What is a transaction? A transaction is a sequence of operations which, if completed without errors, produces a change of state in our database . For example: BEGIN TRAN INSERT INTO TABLE_A (CAMPO1, CAMPO2) VALUES ('VAL1','VAL2') INSERT INTO TABLE_B(CAMPO1,CAMPO2,CAMPO3) VAULES ('VAL1','VAL2','VAL3') COMMIT or INSERT INTO TABLE _A (CAMPO1, CAMPO2) VALUES ('VAL1','VAL2') In the first case we are talking about explicit transaction , in the second of  implicit transaction . …and therefore w