Sql-Server

T-SQL 101: 130 What are Default Constraints in T-SQL?

Default constraints allow us to automatically provide values for columns when: we don’t supply them ourselves they are marked as NOT NULL so they are required When you’re building an INSERT statement, you don’t have to give values for all columns. Columns that are declared as NULL aren’t mandatory, so you don’t have to supply those columns at all. Those columns can just be NULL. But if you have a column that is declared as NOT NULL, so it requires a value, but you haven’t supplied it in the INSERT statement, then a default constraint can provide the value.

2025-03-16

SQL Interview: 26 Whole of data operations when adding columns in SQL Server

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database. Section: Administration Level: Medium Question: You need to add additional columns to a table. You are concerned that you do not want to lock the table for long periods. You plan to avoid any operation that would rewrite every row i.

2025-03-15

T-SQL 101: 129 Inserting Multiple Rows at Once with VALUES clause in SQL Server T-SQL

Prior to SQL Server 2008, we could only insert a single row at a time. SQL Server 2008 added the ability to have multiple rows of data in the VALUES clause. Each row is created from what are typically called row constructors. In the example above, I’m inserting 3 rows into the Cinema Groups table. The syntax allows for up to 1000 rows to be inserted at once. Before you get to that number of rows though, you might exceed the maximum allowable length for a T-SQL statement, if you have a lot of columns, each with a lot of data.

2025-03-14

SDU Tools: Reset Sequence using SQL Server T-SQL

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. Users that are new to working with sequences often don’t have simple options for resetting them back to a specific value. Many business intelligence ETL or ELT processes need to have a way to do this. So we added a tool that can help. It’s called ResetSequence. The procedure takes three parameters.

2025-03-13

T-SQL 101: 128 Inserting Data into a Table using SQL Server T-SQL

To insert new rows into a table, you will mostly use the INSERT statement. In the example above, I’ve inserted one row into the dbo.CinemaGroups table. There are a few aspects to this statement. First, you should always use two-part names for the table i.e., use dbo.CinemaGroups not just CinemaGroups. Second, there is an optional INTO keyword for INSERT statements. You can say INSERT INTO dbo.CinemaGroups. While I’m generally pretty pedantic about these things, I can’t see any value that the word INTO adds here and I don’t use it.

2025-03-12

SQL Interview #25: Extended Stored Procedures in SQL Server

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database. Section: Development Level: Advanced Question: You are reviewing stored procedures in the master database. You note both Stored Procedures, and Extended Stored Procedures. What is the difference between these types of procedures? Answer: Stored Procedures are written in T-SQL or SQL CLR and run in the standard user memory space of a SQL Server session.

2025-03-11

T-SQL 101: 127 Querying the SQL Server System Catalog

There are times that you need to be able to query the database and its internal structures rather than querying the user tables. In the example above, I’ve asked for details of all the databases, and then for details of all the schemas in the current database, followed by details for all the tables and columns in the current database. These are a well-designed set of system views that are easy to work with.

2025-03-10

SDU Tools: List User-Defined Data Types in SQL Server

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. I do a lot of reviewing of database designs and one thing I always check for is the use of user-defined data types. I’ve had situations where these have caused me substantial issues. So we added a tool that can help to find these. It’s called ListUserDefinedDataTypes. The procedure takes one parameter.

2025-03-09

T-SQL 101: 126 Executing Dynamic SQL Statements in SQL Server T-SQL

It’s also possible to create the command dynamically before you execute it. In the example above, I’ve set a number of different parts of a SQL statement into variables, and then used them to construct a complete SQL statement that I’ve then executed. I just have to create a valid SQL statement as a string. Warning While this might seem really, really convenient, and it can be incredibly useful, it is something you need to be extraordinarily careful with.

2025-03-08

SQL Interview #24: DROP and CREATE vs ALTER for T-SQL Functions

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database. Section: Development Level: Medium Question: You are writing a script to change the code in a T-SQL function. You can choose to either DROP IF EXISTS and CREATE the procedure, or to use an ALTER statement. What advantage would you get from using an ALTER statement?

2025-03-07