T-SQL 101: 138 Inserting Rows Returned by a SQL Server Stored Procedure

Another way you can get rows to insert into a table is by executing a stored procedure or, in fact, even by executing dynamic SQL. Either way, these both use an EXEC statement. In the example shown, I’m doing an INSERT into the dbo.CinemaGroups table. I’ve declared the two columns that are being inserted. The stored procedure returns the rows that I want to insert. This is again fairly easy to troubleshoot because the EXEC clause can usually be executed by itself, and you can see the rows coming back.

2025-04-01

SDU Tools: List Columns and Data Types in SQL Server Views

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 data types, including when they’ve been used in views. So we added a tool that can help to find these. It’s called ListViewColumnsAndDataTypes. The procedure takes four parameters: @DatabaseName is the database to process (not used in the Azure SQL DB version) @SchemasToList is a comma-delimited list of schema names or ALL @ViewsToList is a comma-delimited list of view names or ALL @ColumnsToList is a comma-delimited list of column names or ALL

2025-03-31

T-SQL 101: 137 Inserting rows with data from another SQL Server table

When you’re performing an INSERT, you can also get values from a query., rather than specifying the values in the INSERT statement. In the example shown above, I’ve queried for a distinct list of order contacts and their phone number from the dbo.Orders table. I’m then inserting those values into the dbo.OrderContacts table. These types of statements are easy to debug because I can just highlight the SELECT clause and run it to see what comes back.

2025-03-30

SQL Interview: 32 Using ALTER FUNCTION 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: Medium Question: You have a scalar T-SQL function in SQL Server and want to change it. Apart from permissions, what would stop you from using ALTER FUNCTION to change it? Answer: You can’t change from an inline function to a multi-statement function.

2025-03-29

T-SQL 101: 136 Deleting Rows from a SQL Server Table

The DELETE statement is one of the most common SQL statements. We use it to remove rows from a table. In the example, I’ve said to delete rows from the dbo.CinemaGroups table where the value in the CinemaGroupID column is greater than 3. You need to make sure that your DELETE statements have WHERE clauses, unless what you’re intending to do is delete all the rows in the table. You don’t want to make the common mistake where people highlight a DELETE statement in SQL Server Management Studio and forget to include the WHERE clause as well.

2025-03-28

Book Review: How to Land an A330 Airbus by James May

I recently finished another Audible audio book. It was How to Land an A330 Airbus: And Other Vital Skills of the Modern Man by James May. I need to start by saying that I am a really, really big fan of James May. After watching his Toy Stories series, I decided that when I grow up, I want to be James May. It was brilliant, inventive, and just a bunch of fun.

2025-03-27

T-SQL 101: 135 What is a Foreign Key Constraint in SQL Server?

Foreign keys are mostly used where you store the primary key of another table, and want to check that it’s valid. This is used to ensure database integrity. For example, I might have a CustomerID stored in an Orders table, and I want to make sure that the customer already exists. In the example above, I have a Courses table and I want to make sure that the CollegeID that’s stored for each course, actually matches a college that’s already in the Colleges table.

2025-03-26

SQL Interview: 31 Create a copy of an Azure SQL Database on the same logical 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: Intro Question: You have an Azure SQL Database. How can you create a copy of the Azure SQL Database on the same logical server? Answer: The easiest option is to use the CREATE DATABASE AS COPY OF command.

2025-03-25

T-SQL 101: 134 What is a Unique Constraint in SQL Server?

In the previous post, I talked about check constraints. Another type of constraint in SQL Server is a unique constraint. What I want to specify is that a value in a column is unique i.e., no other row has that same value in that column. While a check of the values can be manually requested, the constraint is normally checked when SQL Server is inserting or updating the row. It is also possible to specify a unique constraint at the table level, rather than at the column level.

2025-03-24

SQL Interview: 30 Temporary 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: Administration Level: Advanced Question: Your session is connected to a database named PublicWorks. You want to create a temporary stored procedure, that only lasts until your session has ended. It should be available to all other sessions while your session is still active.

2025-03-23