Sql-Server

SDU Tools: SQL Server Reporting Services Catalog Types

Lately, I’ve been needing to write queries against the SQL Server Reporting Services catalog. And if you’ve ever tried that, you’ll find that items in the catalog have a type, but there’s no table or view that turns that type (a number) into a name.

So, in our free SDU Tools for developers and DBAs, we added a simple view that does just that. It’s called RSCatalogTypes.

Find out more

You can see it in action in the main image above, and in the video here:

2019-10-09

Opinion: Data professionals shouldn't be quick to mock Excel and Power Query

Knocking Access was a popular sport over the last decade or more. Many data professionals saw Access as a real problem. Lots of silos of unmanaged data grew up across organizations and things could get out of hand pretty quickly. I saw all the expected problems that come from a lack of centralized management of data.

Some issues were quite nasty. I remember doing work for a company that did aircraft maintenance and had depots all over the country. Every depot had a copy of an Access database, but every single one had then modified it in different, and in many cases, conflicting ways. Then they decided to centralize the data, and oh what a pain.

2019-10-08

T-SQL 101: 38 Approximate numbers in SQL Server

Today, I’m continuing the discussion on the variety of data types supported by SQL Server. Last time I mentioned exact decimal numbers, but there are also inexact (or approximate) decimal numbers. Here are the available types:

These two data types float and real are approximate data types.

You probably realize that in decimal, there are values we can’t store exactly like 1/3. No matter how many 3’s we write when we write 0.33333333, we are still never going to have the exact value.

2019-10-07

Snowflake for SQL Server Users - Part 9 - Stages

Snowflake has the normal options in its SQL language for using INSERT statements to add data to a table. But it also has bulk options for rapidly adding a lot of data to a table via the COPY command.  The same command can be used for bulk export as well.

A curious aspect of this though, is that because it’s a cloud-only database system, you can’t just use COPY to get data to/from a local file system. COPY works to/from what’s called a stage.

2019-10-04

SQL: Create missing stored procedures using a linked server

I answered an interesting question the other day on Stack Overflow. What the person who posted was after was a way to create all missing stored procedures on one database that were present on another database on another server.

Here’s an example of how to do that:

USE tempdb;
GO

-- Copying from [GREGT580] to local server

SET NOCOUNT ON;

DECLARE @MissingProcedures TABLE
(
    MissingProcedureID int IDENTITY(1,1) PRIMARY KEY,
    SchemaName sysname,
    ProcedureName sysname,
    ProcedureDefinition nvarchar(max)
);

INSERT @MissingProcedures 
(
    SchemaName, ProcedureName, ProcedureDefinition
)

SELECT s.[name], p.[name], sm.definition
FROM [GREGT580].AdventureWorks.sys.procedures AS p
INNER JOIN [GREGT580].AdventureWorks.sys.schemas AS s
ON p.schema_id = s.schema_id
INNER JOIN [GREGT580].AdventureWorks.sys.sql_modules AS sm
ON sm.object_id = p.object_id 
WHERE NOT EXISTS (SELECT 1 
                  FROM sys.procedures AS pl
                  INNER JOIN sys.schemas AS sl
                  ON sl.schema_id = pl.schema_id
                  AND sl.[name] = s.[name] COLLATE DATABASE_DEFAULT 
                  AND pl.[name] = p.[name] COLLATE DATABASE_DEFAULT);

DECLARE @SchemaName sysname;
DECLARE @ProcedureName sysname;
DECLARE @ProcedureDefinition nvarchar(max);
DECLARE @Counter int = 1;

WHILE @Counter < (SELECT MAX(MissingProcedureID) FROM @MissingProcedures AS mp)
BEGIN
    SELECT @SchemaName = mp.SchemaName,
           @ProcedureName = mp.ProcedureName,
           @ProcedureDefinition = mp.ProcedureDefinition
    FROM @MissingProcedures AS mp
    WHERE mp.MissingProcedureID = @Counter;

    EXEC SDU_Tools.ExecuteOrPrint @ProcedureDefinition; -- Change to EXEC (@ProcedureDefinition) to create

    SET @Counter += 1;
END;

I start by querying sys.procedures, sys.schemas, and sys.sql_modules on the remote database to find the ones that aren’t present on the local system. I needed sys.sql_modules to get the definition of the procedure.

2019-10-03

SDU Tools: ROT13 Encode and Decode in SQL Server T-SQL

If you used computers much in the 1980’s or 1990’s you’ll remember jokes that circulated all the time, but encoded in ROT13. It was a type of Caesar Cypher or Shift Cypher where the letters in the original words were shifted by a fixed number of positions in the alphabet to create the cyphertext.

ROT13 was a specific type of shift cypher where, for our alphabet of 26 characters, a 13 character shift is applied. That means that the same function can be used to encode text and then to decode the same text.

2019-10-02

T-SQL 101: 37 Exact decimal numbers in SQL Server

Today, I’m continuing the discussion on the variety of data types supported by SQL Server. Last time I mentioned exact whole numbers, but there are also exact decimal numbers. Here are the available types:

When I need to work with numbers with decimal places in them, the data type that I normally use is decimal.

It has a fixed precision and scale so for example, if I say:

decimal(18,3)

2019-09-30

Snowflake for SQL Server Users - Part 8 - Case Sensitivity

There are many things I like about Snowflake. How they handle case and collations is not one of them.

There are currently no rich options for handling case like you have in SQL Server, with detailed options around both collations, and case sensitivity.

I’ve previously written about how I think that case-sensitivity is a pox on computing. I see absolutely no value in case-sensitivity in business applications, and a significant downside.

2019-09-27

SQL: Maintaining Online Website Data during Full Data Refreshes - Part 2 Code Example

I had a number of responses to my blog post about maintaining an online website’s data during full data refreshes by using partitioning. Lots of people asked for a code sample, so I’ve provided it here.

It’s really common to want to keep a website online as much as possible, but need to rebuild the data that it’s displaying periodically. It might be a website for members of a superannuation fund, and you need to let them see balances as of last night. The problem is that you can’t have the data missing for long or the website isn’t going to keep working. You can’t just truncate the tables, and start repopulating them again. You need to make the changeover as quick as possible.

2019-09-26

SDU Tools: Extracting tokens from CSV rows in T-SQL

There are a few things in IT that seem to rarely change. One is the use of CSV (comma-separated value) files. It’s a pretty basic file format and sometimes we see other delimiters like tabs (aka TSV files) or pipe symbols but these types of files are still everywhere.

So it’s hardly surprising that people want to work with them in T-SQL as well. In our free SDU Tools for developers and DBAs, we added two functions to help: NumberOfTokens and ExtractToken.

2019-09-25