Twitter

Follow SQLQuill on Twitter

Subscribe

MN Pass

Weekly Challenges – 3/27/11

Challenges:

  • Coding on Project B
  • Gave Demo on Red Gate’s SQL Compare
  • On Call – two issues – Rollback of a partitioning job and log shipping out of sync.
  • Customized Grant Fritchey’s (blog/twitter) powershell script from the Scripting a database using Powershell section of this article.

Good Reading from the Week:

The Week Ahead:

  • Project B coding.
  • My wife is due 4/2 – tie up more loose ends at work this week!

Goals for the Year – 3 Ps – (These are my year long goals that I’ll report on each week to keep me focused, read about them here.)

  • PASSMN – Not a lot: C for the past week
  • Powershell – B+ for the past week:
    • Customized the Scripting database with Powershell script
    • Organized all my snippets of code (for sharing)
  • Present –  Gave demo on Red Gate SQL Compare: B for the week.

 

015

Weekly Challenges – 3/20/11

Challenges:

  • Coding on Project B
    • Checked a bunch of small changes in
    • Gave demonstration of our deploy process to team members
  • Attended a few 24 Hours of PASS sessions – I missed a couple I really wanted to see – they will be available in about a month – I’ll have a link to them.
  • PASSMN Meeting – SQL Azure interesting talk by Steve Hughes (blog)

Good Reading from the Week:

The Week Ahead:

  • Project B coding.
  • My wife is due 4/2 – tie up loose ends at work this week!

Goals for the Year – 3 Ps – (These are my year long goals that I’ll report on each week to keep me focused, read about them here.)

  • PASSMN – March meeting this past week – attended via Live Meeting due to my son being sick: C for the past week
  • Powershell – B- for the past week – dabbled with script to script databases the way I want it.
  • Present –  Gave demo on  our deploy process to DBA team: B for the week.

 

029

Random Error Generator

I believe I first heard this idea at PASS in Rob Farley’s (blog/twitter) session, “The Incredible Shrinking Execution Plan”.  If you want to play a cruel trick on someone, insert this somewhere in a script.  I’ve actually used this in testing my deploys to see at what point things blow up.  Here’s the script to produce a random error.

DECLARE @ErrorMessage NVARCHAR(4000),
     @ErrorSeverity INT,
     @ErrorState INT;

Set @ErrorSeverity = ROUND((RAND() * (18)),0) –severity 0 –18
Set @ErrorState = ROUND((RAND() * (255)),0)

Raiserror('Operation not allowed',
     @ErrorSeverity,
     @ErrorState) with log

24 Hours of PASS going on now!

Celebrating Women in Technology
March 15-16, 2011

What is 24 Hours of PASS?

Over two 12 hours periods, there will be twenty four, free, hour long, live meeting sessions on a variety of SQL Server and BI subjects.

More Info
  • The schedule can be found here.
  • The twitter hash tag is #24HOP

Reminder: March 2011 PASSMN Meeting Today!

Date: March 15th, 2011

Time: 3:00 PM – 5:00 PM

Location: 8300 Norman Center Drive, Bloomington, MN  55437

Sponsor: Magenic

Magenic

RSVP Here

Agenda:

  • 2:45-3:00 : Registration / hospitality / networking
  • 3:00-3:15 : Announcements etc.
  • 3:15-4:45 : Steve Hughes:  SQL Azure – Relational Data in the Cloud
  • 4:45-5:00 : Closing comments/prize giveaways

Live Meeting Info:

Presentation: SQL Azure – Relational Data in the Cloud This session will review SQL Azure capabilities and tools. We want to look at what we can do on the platform and what may be some shortcomings of the platform at this point. We will take time to evaluate what place SQL Azure has within our data environments and some viable use cases for using it. Finally, we will crack open the SQL Azure Reporting CTP to get a “first look” at this latest offering from Microsoft.

Steve Hughes is Principal Consultant at Magenic Technologies. His area of expertise is in data and business intelligence architecture on the Microsoft SQL Server platform. He was also the data architect for a SaaS company which delivered a transportation management solution for fleets across the United States. Steve has co-authored two books and regularly presents on SQL Server and data architecture.

Weekly Challenges – 3/13/11

Challenges:

  • Coding on Project B
    • Errors needed to be harsher, bumped them up to level 16
    • Security issues
    • Cranked out some table and sproc changes.
  • TSQL 2s Day – here’s my entry.

Good Reading from the Week:

The Week Ahead:

Goals for the Year – 3 Ps – (These are my year long goals that I’ll report on each week to keep me focused, read about them here.)

  • PASSMN – Didn’t do much, meeting is set up for this week: C for the past week
  • Powershell – C for the past week – Used Powershell to:
    • Worked on powershell script to script database schema based on Grant Fritchey’s script in the section called “Scripting a database using Powershell / SMO” from here.
  • Present –  Signed up to present to team next week on SQL Compare: C for the week.

009

SQL Auto Converts True or False to a bit

The title says it all, but here’s the story.  The other day I was modifying a procedure and I didn’t know if the application was going to pass in True/False or 1/0 for a bit column.  Being lazy, I created my procedure with a 1 or 0 and figured I’d update it later if the app was using True/False.  The App was passing in a True/False, but I didn’t need to update my procedure because SQL auto converts it.  More info can be found here.  It’s pretty basic, but it save me some time, so I figured I’d remind people.  Here’s an example to show it:

Create table Test (IDCol int identity(1,1),
     TestBit bit)

Insert Test Values(0)
Insert Test Values(1)

Select * from test

Insert Test Values('False')
Insert Test Values('True')

Select * from test

Here’s an XML example:

Declare @XMLTest xml
Set @XMLTest = '<Test>
     <BitTest>False</BitTest>
     </Test>
     <Test>
     <BitTest>True</BitTest>
     </Test>'

Select x.x.value('BitTest[1]', 'bit')
from @XMLTest.nodes('/Test') as x(x)

T-SQL Tuesday #016 – The Power of the CHECKSUM_AGG

This month’s T-SQL Tuesday, “Aggregate Functions” is hosted by Jes Schultz Borland (Blog/Twitter) aka grrlgeek.

The Task

One of my duties as a development DBA is to verify our databases are set up the same in each of our environments.  One of the tasks of this duty is to verify our static data/look-up tables are all the same in each environment.  I’ve got a good method for writing the insert/update statements – see this post – but I still need to verify the values are the same in our static data tables.  My tool of choice is the CHECKSUM_AGG function.

How do I do this?

The easiest way I’ve found to do this is to take the BINARY_CHECKSUM of each row in a table that has static data in it (i.e. no datetimes, timestamps, etc).  Then I take the CHECKSUM_AGG of the each row’s binary_checksum – thus giving me a Checksum for the entire table.  I do this on the same table in two different environments and I compare the value.  They will be equal if the values in the tables are the equal.  For a more in depth explanation of this, see this post.

How this grew…

This method described above is easy to do for one table manually, but I’m responsible for approximately 10 to 50 static data tables in each of our 15 databases – in each of our five environments per project, so I needed to automate it.  The good news is that we’ve got good naming standards for our tables, so all the static data tables end in “Type”.  So I created a script that would calculate a CHECKSUM_AGG of all the BINARY_CHECKSUMs of each of the tables ending in “type” in a database, I could export that to excel, run the same script on a different server and compare the results.  This worked for a bit, but I was doing the same thing on each database, so I added a bit of logic to include specified databases to my original script.  Now, I run this script on each  server and I compare the results.

The script:

A couple of highlights, the vSQLCall is included in the final output, so if there is a difference in values, you can easily run the single table script and compare the values of each row.  (I had to do this one time as an update statement that was missing a period and the values were different.)  Also, forgive the combination of temp tables and table value parameters – I like a temp table for the final results, so I can work with it and not drop it.

Create table #TestTable
(ID int NOT NULL IDENTITY (1, 1),
ServerName varchar(255),
DBName varchar(255),
TableName varchar(255),
vSQLCall nvarchar(2550),
ChecksumVal varchar(255))
--Add default for blank column
ALTER TABLE #TestTable ADD  CONSTRAINT [DF_testtable]  DEFAULT (' ') FOR [vSQLCall]

-- A work table to track the databases and the server name
Declare @TestTableDBs Table
(ID int NOT NULL IDENTITY (1, 1),
ServerName varchar(255),
DBName varchar(255))

Declare @icount int,
@TopNum int,
@sql1 nvarchar(2000)

--Initialize variables
Set @icount = 1

--Fill up the databases to do...Change DB list here if scope changes.
Insert @TestTableDBs
Select @@ServerName, Name from master.sys.databases
where name in (<<DBNAME1 goes here>>, <<DBNAME2 goes here>>, <<DBNAME3 goes here>>, ....)
Select @TopNum = Max(ID) from @TestTableDBs

--Fill in main table with all the tables needed to check
--Note any table name with '%type%' in the name
While @icount <= @TopNum
Begin
Set @sql1 = 'Insert #TestTable (ServerName, DBName, TableName) '
Select @sql1 = @sql1  + 'Select @@ServerName, Table_catalog, Table_Name from '+ DBName
+ '.information_schema.tables where table_type = ''BASE TABLE'' and table_name like ''%Type%'''
from @TestTableDBs where id = @icount
Exec (@sql1)
Set @icount = @icount + 1

End

---Now figure out the colums to of the tables and the SQL call to find the checksum
Declare @TableID int,
@CurDBName varchar(255),
@CurTableName varchar(255),
@Start int,
@loopStart int,
@end int,
@maxEnd int,
@vColumnName varchar(2000),
@sql nvarchar(2000)

Create table #TestTableColumns
(ID int NOT NULL IDENTITY (1, 1),
ServerName varchar(255),
DBName varchar(255),
TableName varchar(255),
columnNames varchar (2000))

--Initialize the variables
Select @Start = 1
Set @TableID = 1
Set @loopStart = 1

Select @maxEnd = Max(ID) from #TestTable

--Loop for each table to get the columns
While @Start <= @maxEnd
Begin

Select @CurDBName = DBName,
@CurTableName = TableName
from #TestTable where ID = @TableID

Set @vColumnName = ''
Select @sql =
'Insert #TestTableColumns
(ServerName,
DBName,
TableName,
columnNames)
Select @@serverName,
Table_Catalog,
Table_Name,
Column_Name
from ' + @CurDBName + '.information_schema.columns
Join #TestTable on DBName = Table_Catalog and TableName = Table_Name
where data_type <> ''timestamp''
and #TestTable.ID = ' + STR(@TableID)
Exec (@sql)

Select @end = Max(ID) from #TestTableColumns

--Loop through to get the column name for each table
While @loopStart <= @end
Begin
Select @vColumnName = @vColumnName + columnNames + ', ' from #TestTableColumns where id = @loopStart
Set @loopStart = @loopStart + 1
End --End of loop for all columns for each table

--Update #TestTable with the SQL call to get the checksum
update #TestTable
Set vSQLCall = 'Select CHECKSUM_AGG(BINARY_CHECKSUM(' + Left(@vColumnName, Len(@vColumnName) - 1)
+ ')) from ' + @CurDBName + '.dbo.' + @CurTableName
where ID = @TableID

Set @TableID = @TableID + 1
Set @Start = @Start + 1
End --End of loop for all tables
--No longer needed
DROP TABLE #TestTableColumns

---Finally execute the SQL in the column
Declare @Finalcount int,
@TopNum1 int,
@SQL2 nvarchar(2550)
Set @Finalcount = 1

Select @TopNum1 = Max(ID) from #TestTable

While @Finalcount <= @TopNum1
begin
Select @SQL2 = 'Update #TestTable Set CheckSumVal = (' + vSQLCall + ') where ID = ' + STR(@Finalcount) from #TestTable where ID = @Finalcount
EXEC (@SQL2)
Set @Finalcount = @Finalcount + 1
End

--Output the results
Select * from #TestTable

--Clean up
Drop table #TestTable

The Output

The Future

I’ve got plans to have Powershell run this on each server I specify, then combine and compare the results in a file and save all the supporting data.  Thus when I need the documentation, it’ll take about 3 minutes (2.5 of which will be getting coffee), instead of the 1 hour it’s currently listed in the schedule to take.

Weekly Challenges – 3/6/11

Challenges:

  • Coding on Project B
    • A couple of issues with Error Codes not being harsh enough
    • Sproc/Table changes
  • 24 hour flu – hit me Tuesday night, was by dinner on Wednesday, but slept like 18 hours in between
  • This blog is now syndicated on sqlserverpedia.com

Good Reading from the Week:

The Week Ahead:

  • Project B coding

Goals for the Year – 3 Ps – (These are my year long goals that I’ll report on each week to keep me focused, read about them here.)

  • PASSMNMarch Meeting announced: B for the past week
  • Powershell – A- for the past week:
    • Completed the book Powershell for Dummies – great for learning the basics.
    • Started to write a script to check our deploys on a bunch of servers.
    • Downloaded and watched live meeting of PASSMN December 2010 meeting – “PRACTICAL POWERSHELL & SQL” by Nick Weber
  • Present –  No activity: Inc. for the week.

 

020

March 2011 PASSMN Meeting

Date:  March 15th, 2011

Time:  3:00 PM – 5:00 PM

Location: 8300 Norman Center Drive, Bloomington, MN  55437

Sponsor: Magenic

Magenic

RSVP Here

Agenda:

  • 2:45-3:00 : Registration / hospitality / networking
  • 3:00-3:15 : Announcements etc.
  • 3:15-4:45 : Steve Hughes:  SQL Azure – Relational Data in the Cloud
  • 4:45-5:00 : Closing comments/prize giveaways

Live Meeting Info:

Presentation:

SQL Azure – Relational Data in the Cloud

This session will review SQL Azure capabilities and tools. We want to look at what we can do on the platform and what may be some shortcomings of the platform at this point. We will take time to evaluate what place SQL Azure has within our data environments and some viable use cases for using it. Finally, we will crack open the SQL Azure Reporting CTP to get a “first look” at this latest offering from Microsoft.

Steve Hughes is Principal Consultant at Magenic Technologies. His area of expertise is in data and business intelligence architecture on the Microsoft SQL Server platform. He was also the data architect for a SaaS company which delivered a transportation management solution for fleets across the United States. Steve has co-authored two books and regularly presents on SQL Server and data architecture.