Select Page

Getting friendly with MERGE

Here’s a simple example of how to use MERGE. I’ll do a follow up post on this later. SET NOCOUNT ON; /* TARGET TABLE */ DECLARE @Movies TABLE ( id INT IDENTITY(1,1) PRIMARY KEY CLUSTERED ,Title VARCHAR(20) ,Qty INT NOT NULL ,CreateDate DATETIME...

Get row count from all tables

I was reviewing some of my older scripts and I came across one in particular that stood out. I wrote it quite a while ago and looking back does indicate where I came from in terms of logic. To give a little background my script was written to grab the row counts from...

Format Seconds into HH:MM:SS

Without having to use replication monitor I decided to put together a stored procedure that will return the same results. In the MSmerge_sessions table of the Distribution database the field duration is returned in raw seconds. Meaning that if the process took 2 mins...

Pad then Add Leading Zeros

In the event you need to add leading zeros to an integer, the process is pretty straightforward. I am using the AdventureWorks database and SQL Server 2008 R2. For this example I will use the Sales.SalesPerson table for their SalesPersonID field which contains 3 digit...

Select Multiple Rows Into Multiple Columns

Clearly I am having a sql brain fart but for some reason I cannot think of a better way to select data from a column and have it return in a single row. To better illustrate what I am looking to accomplish I will reference the sys.messages system view. Here is an...