How Implicit Transactions Work in SQL Server

There are four transaction modes in SQL Server. One of these is implicit mode.

In SQL Server, an implicit transaction is when a new transaction is implicitly started when the prior transaction completes, but each transaction is explicitly completed with a COMMIT or ROLLBACK statement.

This is not to be confused with autocommit mode, where the transaction is started and ended implicitly.

Continue reading

SQL Transactions Tutorial

In SQL, transactions are used to maintain data integrity by ensuring that a sequence of SQL statements execute completely or not at all.

Transactions manage sequences of SQL statements that must be executed as a single unit of work, so that the database never contains the results of partial operations.

When a transaction makes multiple changes to the database, either all the changes succeed when the transaction is committed, or all the changes are undone when the transaction is rolled back.

Continue reading

How to Fix “Conversion failed when converting the value to data type” in SQL Server

SQL Server error Msg 245, Level 16 tells us that there was a problem when trying to convert a value to a specific data type.

You’ll get this error if you try to insert the wrong data type into a column.

To fix this issue, make sure the data type of the value you’re trying to insert, matches the column’s type.

Continue reading

3 Ways to Return the Number of Rows in Each Partition in SQL Server (T-SQL)

If you’ve previously created a partitioned table in SQL Server, and you now want to know how many rows are being stored in each partition, here are three queries you can use.

In particular, you can:

  • Query the sys.dm_db_partition_stats view
  • Query the sys.partitions view
  • Use the $PARTITION function in a query

Below are examples of all three.

Continue reading

SQL Not Equal To (<>) Operator for Beginners

In SQL, the not equal to operator (<>) compares the non-equality of two expressions. That is, it tests whether one expression is not equal to another expression.

If either or both operands are NULL, NULL is returned.

SQL also has another not equal to operator (!=), which does the same thing. Which one you use may depend on your DBMS, which one you’re the most comfortable using, and perhaps also whether your organisation has any coding conventions that dictate which one should be used.

Continue reading

SQL Not Equal To (!=) Operator for Beginners

In SQL, the not equal to operator (!=) compares the non-equality of two expressions. That is, it tests whether one expression is not equal to another expression.

If either or both operands are NULL, NULL is returned.

SQL also has another not equal to operator (<>), which does the same thing. Which one you use may depend on your DBMS, which one you’re the most comfortable using, and perhaps also whether your organisation has any coding conventions that dictate which one should be used.

Continue reading