Wednesday, August 20, 2014

Create Hash of Multiple Columns

Josh,

So, you need to compare several columns of one table to several columns of another.  Perhaps you are checking to see if data has changed before updating a row, or you need to track if multiple columns have changed.  Besides doing a bunch of AND statements, a nifty feature of SQL is the HASHBYTES function.

Friday, August 8, 2014

Search for Table Columns Using the System Schema

Josh,

So you want to search if a column exists in another table, but can't remember which table.  Here's a handy script to be able to search a specific schema for a certain column name:

Thursday, August 7, 2014

Using Custom DLL's in an SSIS Script Task

Josh,

So you've created an awesome class library you want to use in an SSIS package.  Instead of worrying about registering it in the GAC, you can do the following to make sure your code will work in production.

Tuesday, July 22, 2014

Select Into Creates Identity Column on New Table

Josh,

Say you have a table like this:

CREATE TABLE MyOldTable (MyId INT IDENTITY(1,1),Name VARCHAR(100)
)


Then, you need to create another table from this for simplicity sake:

SELECT
     
MyId,
     
Name,
     
row_number() OVER(ORDER BY Name DESCAS MyNewIdINTO
     
MyNewTableFROM
     
MyOldTable

Looking at the "MyNewTable", you'll notice the MyId column is defined as an INT IDENTITY(1,1)!  Most likely you didn't intend for this to happen.  The fix is pretty simple:

Wednesday, July 16, 2014

Using ROW_NUMBER() to Get the Last Event for Each Category

Josh,

So the scenario is you have a table with a list of events like this:

CREATE TABLE [dbo].[Events](
   
[EventDate] [datetime] NULL,
   
[Category] [varchar](10) NULL,
   
[EventDescription] [varchar](60) NULL
)
  


You only want the latest record for each Category.  One way to accomplish this is the ROW_NUMBER() function...

Monday, June 16, 2014

Sql Check if Object Exists

Josh,

You know those situations where you want to drop and recreate something, or create something if it doesn't exist?  You can use the object_id function to check if an object exists in the database.

Monday, June 9, 2014

SSAS Dynamic Prior Period

Josh,

When using the prior period function and you want the calculation to work on any level of the hierarchy, you'll need to remember the "CurrentMember.Level" syntax: