In this article, we will learn how to create PostgreSQL stored procedures using PSQL.
Read more »Stored procedures
xp_cmdshell and sp_xp_cmdshell_proxy_account stored procedures in SQL Server
June 22, 2022This article explains the xp_cmdshell and sp_xp_cmdshell_proxy_account system stored procedures in SQL Server and how developers can use them to execute Windows commands.
Read more »Lever the TSQL MAX/MIN/IIF functions for Pinpoint Row Pivots
May 16, 2022Introduction
For a recent Tsql development project involving text document records, the customer wanted a product that would show the latest and most recent earlier version of specific document text stored in a SQL Server 2014 database. One result set row had to hold all relevant information for each document: both document versions as described above, and relevant metadata. This essentially required fine-grained row pivots. This article will describe the engineering behind the solution, and compare that solution with the SQL Server PIVOT operator.
Read more »Transfer Stored Procedures between master databases on SQL Server instances using SSDT 2017
November 9, 2021This is the second article in the series of Migrating SQL Server Objects using SSDT 2017. In this article, we will learn how to copy user stored procedures created in the SQL Server master database.
Read more »SQL Server Stored Procedure Recompile Options
September 15, 2021This article intends to give comprehensive details on how we can use the recompilation options of SQL Server stored procedures and how they behave when we use these recompilation options.
Read more »SQL Server Stored Procedure Recompilation Factors
September 6, 2021In this article, we will explore in detail the factors that cause SQL Server stored procedures to be recompiled.
Read more »Using Stored Procedures with Return Values
August 6, 2021In this article, we will learn how we can use a Stored Procedure with return values with all details.
Read more »Working with parameters in the sp_executesql stored procedure
April 16, 2021This article explains how to use the sp_executesql system stored procedure to run static and dynamic SQL queries and how to pass input and output parameters while using it.
Read more »Learn MySQL: The Basics of MySQL Stored Procedures
January 8, 2021In this article, we are going to learn about the stored procedures in MySQL. In this article, I am covering the basics of the stored procedure that includes the following
Read more »Link an Azure Cosmos DB into a SQL Server Stored Procedure
August 3, 2020Introduction
Microsoft Azure offers hundreds of cloud products, with more on the way. In the database space, Microsoft offers Azure Cosmos DB, a NoSQL database product. We can build Java, .Net, etc. applications around Cosmos DB resources. To extend this capability, this article will show how to query Cosmos DB directly from SQL Server.
Read more »Lever T-SQL for Pinpoint Control of ORDER BY in a Stored Procedure
June 1, 2020Introduction
The T-SQL ORDER BY clause sorts SQL Server SELECT statement result sets, and it becomes important when we build stored procedures. Unfortunately, the syntax offers no flexible way to directly control the ORDER BY clause behavior with argument values. This means we don’t have an easy way to control the specific column or columns that the ORDER BY clause sorts. Additionally, SQL Server does not offer a flexible way to directly control the ascending or descending order of any ORDER BY clause column with argument values. Of course, we can certainly hard-code the ORDER BY clause in a stored procedure, but this approach becomes fixed in stone. We could try a dynamic SQL solution, involving a stored procedure code that dynamically builds and executes SQL Server statements inside a stored procedure. However, this technique becomes tricky, and it can lead to SQL injection attacks. Other techniques might rely on CASE statements, and their complexity can become overwhelming as the column count grows. This article spotlights a clean, efficient, pinpoint T-SQL stored procedure technique that directly sorts one, some, or all SELECT statement result set columns. The technique avoids dynamic SQL, and it operates directly in a stored procedure. The article also shows how to set the ascending or descending sort order of specific columns.
Read more »How to update the T-SQL Toolbox database
April 6, 2020Introduction
In an earlier article, Solve Time Zone, GMT, and UTC problems using the T-SQL Toolbox database, I described T-SQL Toolbox, a free, open-source SQL Server database that handles time zone, date, and time calculations in a clean and efficient way. Available here at the CodePlex Archive, and here at GitLab, T-SQL Toolbox relies on time zone and time zone adjustment data in its two tables for many of its own calculations. However, T-SQL Toolbox does not update that data. I built primitive C-Sharp and VB.net applications that extract the latest time zone and time zone adjustment data from the Windows registry, and I discussed those applications in that earlier SQL Shack article. In the article, I explained that we can build SQL Server UPDATE statements with this extracted data, and then update the T-SQL Toolbox tables. This article describes a better approach and a better solution.
Read more »Table-Valued Parameters in SQL Server
April 6, 2020Table-Valued Parameters aka TVPs are commonly used to pass a table as a parameter into stored procedures or functions. They are helpful in a way, we can use a table as an input to these routines and we can get rid of dealing more complex steps to achieve this process.
Read more »An overview of the sp_WhoIsActive stored procedure
March 23, 2020This article gives a comprehensive overview of custom stored procedure sp_whoisactive.
Read more »Learn SQL: User-Defined Stored Procedures
March 5, 2020Stored procedures (SPs) are one more powerful database object we have at our disposal. They can help us handle many tasks and improve performance and security. Today, we’ll take a look at simple SPs and show, on examples, how to use them.
Read more »How to send database mail using a CLR stored procedure
February 3, 2020In this article, I am going to explain how we can send an email from SQL Server express edition using a CLR stored procedure. First, let me explain about the CLR stored procedures.
Read more »An overview of sp_getapplock and sp_releaseapplock stored procedures
January 9, 2020This article explains about the sp_getapplock and sp_releaselock stored procedures and their usage with example.
Read more »Using sp_executesql stored procedure for executing dynamic SQL queries
December 24, 2019The sp_executesql stored procedure is used to execute dynamic SQL queries in SQL Server. A dynamic SQL query is a query in string format. There are several scenarios where you have an SQL query in the form of a string.
Read more »Implementing SQL data types
October 23, 2019This article is about many different SQL data types that we use when working with SQL Server. We will start with a quick overview and go through some stuff like categories of data types, what objects we can work with, and how to create our own custom data types.
Read more »SQL Server stored procedures for beginners
July 29, 2019In this article, we will learn how to create stored procedures in SQL Server with different examples.
Read more »Partial stored procedures in SQL Server
June 8, 2018Introduction
SQL is an old language — almost 50 years! The first commercially-available version of SQL was released by Oracle way back in 1969. In its specifications and general, “standard” appearance, it resembles the other leading language of the day, COBOL. Language theory and computer languages have evolved considerably since then. Modern concepts such as object-oriented programming and functional programming are not well-represented in SQL. That’s a pity. It can lead to copying code which almost always increases maintenance costs and code fragility.
Read more »Restricting and monitoring SQL Server data access with SQL views and stored procedures
May 17, 2018This article explains data security for accessing sensitive data and restricts access in application using SQL Views and stored procedures.
We recently faced a leak of information for one of our employees that involved data which caused a conflict within our company, even if it was not personally identifiable information (PII data). When we investigated the issue, we uncovered that we need to organize data access for our teams and review who has access to what information. As of right now, all our users either have access to all tables directly or a subset of all our tables. What are some practices we can use in SQL Server to avoid giving direct table access to our users? Read more »
Debugging stored procedures in SQL Server Management Studio (SSMS)
February 20, 2018Debugging is one of the most important but painful parts of any software process. To find some errors you have to run the code step by step to see which section of the code is responsible for the error. This is called runtime debugging.
Read more »How to email SQL query results to your smartphone using the sp_send_dbmail stored procedure
July 24, 2017Introduction
Sometimes there are tasks that require too much time to finish during business hours and we need to leave the office with some still pending like the migration of data, backups or long running queries. In such cases, we would still want to receive an email in our smartphones to make sure that the job was completed successfully.
Read more »How to create, configure and drop a SQL Server linked server using Transact-SQL
July 5, 2017Linked servers allow getting data from a different SQL Server instance using single T-SQL statement.
This article will explain how to create, configure and drop a SQL Server linked server using system stored procedures.
Read more »