Archive for November 21, 2013

SQL Bacon Bits No. 2 – Dropping Temporary SQL Objects

Bacon file8121243652304This is my second SQL Tidbit and I have already decided to change the name to SQL Bacon Bits. Why? Because it’s my blog and I can. These quick posts are supposed to be simple and yummy, just like bacon.

Today’s featured product is ER/Studio Data Architect.

The Need

Whenever a change script is created from an ER/Studio Data Architect model, the original objects are kept with a date time stamp as part of their name. These original copies are then left for the developer to determine if they are still needed.

When I first came across these objects in my database, I was flummoxed. “Why would they leave such a mess?”, I asked myself. But they didn’t leave a mess. They were actually being considerate. Sometimes the SQL Objects have issues because of the changes being brought into the database. Since the original objects are kept as back up copies, you can go look at them before deleting them.

So how do you get rid of these objects once you have confirmed they are no longer needed?

Well, you create a script. This script is based on a version that my co-worker Chris Henry created. I’ve added to it and generalized it.

Let’s break it down

I found that ER/Studio Data Architect uses UTC as the time for the timestamp on the temporary objects. (That drove me nuts until I figured it out!). By using the GetUTCDate() function you’ll be able to programmatically create the date to filter the SQL objects that you want to get rid of.

DECLARE @Date AS varchar(10)

SELECT
    @date = '%' + CONVERT(varchar(2), MONTH(GETUTCDATE())) 
				+ ( CASE 
						WHEN DAY(GETUTCDATE()) < 10 THEN '0'
                        ELSE ''
                        END ) 
				+ CONVERT(varchar(2), DAY(GETUTCDATE())) 
				+ CONVERT(varchar(4), YEAR(GETUTCDATE())) + '%'

 

I then use UNION ALL to join the different types of objects with the proper DROP syntax.

SELECT
    'ALTER TABLE [' + s.name + '].[' + p.name + '] DROP CONSTRAINT  [' + o.name + ']' AS ItemToDrop
	,o.type

FROM
    sys.objects AS o
	JOIN sys.objects AS p ON o.parent_object_id = p.object_id
	JOIN sys.schemas AS s ON p.schema_id = s.schema_id
WHERE
    o.name LIKE @date
    AND o.type = 'F'

UNION ALL

SELECT
    'Drop Trigger [' + s.name + '].[' + o.name + ']' AS ItemToDrop
	,o.type
FROM
    sys.objects AS o
	JOIN sys.objects AS p ON o.parent_object_id = p.object_id
	JOIN sys.schemas AS s ON p.schema_id = s.schema_id
WHERE
    o.name LIKE @date
    AND o.type = 'TR'

UNION ALL

SELECT
    'drop table [' + s.name + '].[' + o.name + ']' AS ItemToDrop
	,o.type
FROM
    sys.objects AS o
	JOIN sys.schemas AS s ON o.schema_id = s.schema_id
WHERE
    o.name LIKE @date
    AND o.type  ='U'

UNION ALL

SELECT
    'drop proc [' + s.name + '].[' + o.name + ']' AS ItemToDrop
	,o.type
FROM
    sys.objects AS o
	JOIN sys.schemas AS s ON o.schema_id = s.schema_id
WHERE
    o.name LIKE @date
    AND o.type  ='P'
ORDER BY
	o.type

 

After it executes, I copy the statements out of the results pane and execute them.

Bonus Tip

Since this is a script you’ll use again and again…and again. You’ll want to turn it into a template. Two of the many ways to do that are as follows.

  • SSMS comes with a Template Browser. You can save your script in the Template Browser for future use. Each you need it, simply double click on the script in the Template Browser and copy will be created for you. By adding the following code at the top of the script, you can use Ctrl+M to pick which database you want to use.
USE <DatabaseName, string,>
GO

 

  • If you are a SQL Prompt addict like me, then  you can add your script as a snippet. Instead of adding the code I just showed you, add the following code and a default database name will appear as a default when you use Ctrl+M.
USE <DatabaseName, string,$DBNAME$>
GO

You can download full script from here.

SQL Bacon Bits

SQL Tidbits: No.1– Outputting from ER/Studio Data Architect Directly to SQL Server Management Studio

 

SQL Tidbits: No.1 – Outputting from ER/Studio Data Architect Directly to SQL Server Management Studio

I’ve been tossing around some ideas for my blog. One of which is to provide a quick tech tip… or a SQL Tidbit. The idea is to keep me writing until I get used to blogging EVERY week. Hopefully in a few months I’ll have more SQL Tidbits than there are Grape Leaves in this picture.

So let’s get started with the first SQL Tidbit.

In the beginning…

I’ve been using ER/Studio Data Architect for a few years now. If you’re not familiar with it, it’s used for modeling and maintaining database schemas. I love this product, but I’m not a big fan of the default application (Universal ISQL) that the change script is sent to. Mostly because Ctrl+A doesn’t work in the query window and I find it clunky.

At my previous company, one of my coworkers created an application that ran in the background. When it detected that a change script was ready to be deployed, it would capture it and open it in SSMS.

But there is an easier way

When I started at my current job, I was setting all the defaults that I like in ER/Studio Data Architect and I stumbled upon this setting that I didn’t know existed. This setting lets YOU pick the editor for the change scripts to be deployed to. I changed the path to SSMS and I didn’t have to see the default application any more. (WIN!)

To get to the setting, do the following.

1. Click on the Tools/Options menu item.

2. Click on the Tools tab on the right hand side of the dialog box.

3. Change the ISQL Path setting.

 

And that’s a wrap

In the next SQL Tidbit I’ll share a script for cleaning up temporary SQL objects.

Once Upon a Time, In A PASS Summit Far Away

Once upon a time, in a PASS Summit far away, there was a princess named Buttercup. When she turned twenty something her father introduced her to SQL Society in grand style. There were many sessions that she had to attend and many parties. Everyone loved her.

dsc_0461

One night there was a wonderful party full of singing and dancing. Everyone was having such a swell time.

SummitStory2013_-006

Princess Buttercup had heard about the party. Her father had forbidden her to go because it was well known that SQL Hippo was known to hang out there. Princess Buttercup didn’t listen to her father though. She set up some Powershell scripts to make it look like she was doing homework and headed out. When she arrived at the party she met three suspicious looking attendees. They were asking her all sorts of database questions.

SummitStory2013_-003

After a few hours had passed, Tim heard the three attendees talking about princess Buttercup, so he went looking for her. He couldn’t find her anywhere and became concerned. He tried to get everyone’s attention and said, “Hey everyone! Where’s princess Buttercup? Has anyone seen her? Maybe the notorious Oracle gang has made off with her! We should send someone out to find her!”

SummitStory2013_-005

At that very moment, the notorious Scary DBA himself entered the party. He sauntered over to Tim, looked down at him, and said, “I’ll go search for her!”

SummitStory2013_

And without another word he jumped into his really cool car to head out!

SummitStory2013_-015

Tim ran after him, and yelled, “WAIT!” you have to get your eyes checked first so that you don’t miss any clues”

SummitStory2013_-005

So, before the Scary DBA could head out in his really cool car, he stopped to get his eyes checked by Dr. Emmett Brown. He told the Scary DBA, “If my calculations are correct, when this baby hits 88 miles per hour… You’re gonna see some serious shit.” Then he put his hand on the Scary DBA’s shoulder and told him, “So if you don’t want to go blind, keep it under 88 miles per hour son.”

SummitStory2013_-010

The next morning, the Scary DBA headed out looking for clues, and he found some scribbled on the sidewalk. He knew they would lead him to the infamous SQL Gator.

SummitStory2013_-008SummitStory2013_-008 SummitStory2013_-008

EdThe Scary DBA followed the clues to a little bar in UpTown where SQL Gator was known to linger on Sunday nights. He wasn’t disappointed either. In the very back of the bar, he found SQL Gator. With a great big knowing grin on his face, SQL Gator said, “I hear you’ve been looking for GrantPrincess Buttercup.”

The Scary DBA replied, “Why yes I am. Do you know anything? I’m not leaving until you spill the beans.”

SQL Gator chuckled and took a sip of his drink. “Your execution plans might be the fastest in town, but this is my town and you don’t scare me. “ He paused for a moment. “I’ll tell you what. I’ll help you out this once, because I like Princess Buttercup.“ SQL Gator looked behind him to make sure he wasn’t being watched and whispered, “Go see the Quiz Bowl council. They’re overseen by The SQL Agent Man and Dr SQL. Maybe they can point you in the right direction.”

The Scary DBA looked SQL Gator in the eye and said, “You better not be leading me astray, or I’ll sic my parameter sniffing dogs on you.”

The Scary DBA jumped into his really cool car and headed over to the see the council. It took two hours, but he was finally brought before them.

SummitStory2013_-002

They told him in order to finish his quest, he would have to find the Knights who say Ni.

FirstTimerBuddies

After many nights of searching, he stumbled upon a great battle and watched with much interest from above. The battle seemed to stand still at times, but finally it came to a gruesome conclusion.

SummitStory2013_-012

He walked down to the battleground searching for the survivors of the Knights Who Say Ni. The Scary DBA came across seven knights in kilts and asked, “Are you the Knights Who Say Ni?”

Knight one replied, “We are now no longer the Knights who say Ni.” Knight two hastily added, “NI.” The other knights shushed him. Knight one ignored his brethren and in a strong voice announced, “ We are now the Knights who say… “Ekki-Ekki-Ekki-Ekki-PTANG. Zoom-Boing. Z’nourrwringmm.”

SummitStory2013_-016

The Scary DBA was very confused and started to get frustrated when he was approached by a Jedi Microsoft Certified Master. The Jedi whispered in a haunting voice, “These are not the kilts you are looking for.”

SummitStory2013_-014

“Look to the east where the SQL Sisters live. They lured princess Buttercup away from the party with promises of chocolate and faster queries.” Then he vanished.

JulieAndMickeyMentors

It took some time, but the Scary DBA was able to track down the kidnappers. He cornered them in the community zone and had them arrested.

SummitStory2013_-013

The Scary DBA was so pleased to see that Princess Buttercup had not been harmed. Princess Buttercup had him kneel before her and she dubbed him Sir Knight of the Red Gate.

SummitStory2013_-004

That night there was much rejoicing at the return of their beloved princess Buttercup.

SummitStory2013_-007


The End

 

No Database Developers were injured in the making of this tale. 99% of the pictures were taken by Pat Wright. Please visit his website for the originals and other great pictures.

%d bloggers like this: