Scripting SQL Server 2005 Stored Procedures to Individual Files

Posted by Adrian on Jun 27th, 2008

I had an interesting problem. I have to do a compare between our development database server and source control for all stored procedures and if needed sync them. So the way I was going to do it was to script out all stored procs and use WinDiff to compare the folder I dumped to from SQL Server and our Source Control folder. Problem was how do I script like 2000 ( yes two thousand ) stored procedures to individual files !? I am pretty sure that in Enterprise Manager you could just right click the whole selection and say script to files or something of the sort. In 2005 that functionality seems to be missing. Well it’s not… It just got moved. After some Googling ( yeah, that’s a verb now ), I found this blog :

http://blog.sqlauthority.com/2007/08/21/sql-server-2005-create-script-to-copy-database-schema-and-all-the-objects-stored-procedure-functions-triggers-tables-views-constraints-and-all-other-database-objects/

This shows how to script the database entirely, triggers, constraints, etc. I just wanted the procs. So what to do?? Well if you look through his tutorial, you don’t want all the objects in the database so where he tells you to check this box:

Proc Gen

Don’t … Leave that blank since you don’t want all the objects.

The next screen will have a whole bunch of properties, I didn’t really touch those… least I don’t think I did. Just hit Next and you will arrive at this screen where you want to select Stored Procedures :

SQL Server Proc Script

Hit Next to go to the following screen where you will see all of your stored procs. I chose Select All since that is what I needed but you can pick and choose here.

SQL Server Proc Generation 3

Once you have chosen which procs to script out you can hit Next. You will arrive at the last screen. This is where you specify a folder to script the stuff out to:

SQL Server Proc Gen 4

On this screen you want to specify that you want to Script to file, you want one File per object, and then the File name or in our case the directory. If you are doing file per object just put in something like C:\SQLDump\ . That will dump all your stored procs to that folder.

That’s it… just get ready to wait if you have a lot of procs. The server I did this on had a few thousand so that took like 5 minutes.

Two responses before the comments get posted :)

1. I took the screens off of our Dev server so thats why I blanked out some of the information.
2. If this seems painfully obvious to you I am sorry… I searched for how to do this and all I found was a lot of people complaining that they can’t script stuff out anymore and how SQL Server 2005 is a step back.

Anyway, if you want to compare what you scripted out with another source, just use WinDiff to compare directories. I had to use this Bulk Rename Utility to name the scripted files the same as the source files.

PS: Big thanks to Pinal Dave for the initial post on how to script that DB out that got me on the right track!!

What exactly is Google Maps trying to tell me?

Posted by Adrian on Jun 23rd, 2008

I am not sure but if I did this on my motorcycle I wouldn’t be around for long :

Google Maps

Turning Rows Into Columns In SQL Server

Posted by Adrian on Jun 12th, 2008

So I had a recent need to take a bunch of rows that I select and turn them into columns so I could put a string together for a specific column. What am I talking about? Well say that you have a table called Employees.



SELECT * FROM EMPLOYEES


ID FIRST_NAME LAST_NAME
———– ———————————— ————————————
1 Adrian P
2 Ryan S
3 Dan W
4 Bruce A



(4 row(s) affected)

At this point say that what you want to throw back to your application is :

“Adrian, Ryan, Dan, Bruce are online!”

So you are probably thinking cursor in the stored proc or loop on the application side. That’s where we can use a recursive trick to save ourselves a bit of time and cursor writing. Don’t worry it’s not hard.




DECLARE @EMPLOYEE_LIST VARCHAR(50)

SELECT @EMPLOYEE_LIST = COALESCE(@EMPLOYEE_LIST + ‘, ‘,”) + FIRST_NAME
FROM EMPLOYEES

SELECT @EMPLOYEE_LIST AS EMPLOYEES_ONLINE




EMPLOYEES_ONLINE
————————————————-
Adrian, Ryan, Dan , Bruce

(1 row(s) affected)

That’s it no cursor or special stuff to do. It works because of the way you are assigning @EMPLOYEE_LIST to itself. Kinda like a recursive CTE… only without the CTE :) I have only done this in SQL Server 2005 so if it does not work in 2000, or 2008 ( although I would assume everything from 2005 should move up ) you may end up with a cursor or some loop.

Note: Watch the result set! I haven’t tried this with a few million rows, but I would assume you may bog down the CPU if you try to do this with a few thousand rows or something. The last thing you want is a few DBAs at your desk giving you the stare LOL :)

F*CK Wal-Mart

Posted by Adrian on Jun 6th, 2008

So I was watching this video on You-Tube

The dude is pretty funny so I watch his show. This is outrageous! I know I work for the competition but for crying out loud! How can you sue a brain damaged woman and her family and leave them in the poor house, and still sleep at night?

Message of the day STOP SHOPPING AT WAL-MART! Their products suck, most stores are a pit, and the company is EVIL! Quit giving these MONSTERS money! Here is the link to the article on CNN:

http://www.cnn.com/2008/US/03/25/walmart.insurance.battle/index.html

Turns out you don’t need a Wizard After All…

Posted by Adrian on Jun 5th, 2008

BUYOUT_SELLOUT

You just need 28.1 billion dollars…

Number of Weeks In A Month

Posted by Adrian on May 23rd, 2008

So I am working on a .NET web app that needs to calculate how many weeks there are in a month. I have come upon some solutions but I put one together that I like better than the rest…

public int Weeks(int year, int month, DayOfWeek firstDayOfWeek)
{

CultureInfo ciCulture = new CultureInfo(”en-US”);
System.Globalization.Calendar cal = ciCulture.Calendar;

return cal.GetWeekOfYear(DateTime(year, month, DateTime.DaysInMonth(year, month)), ciCulture.DateTimeFormat.CalendarWeekRule, firstDayOfWeek) - cal.GetWeekOfYear(DateTime(year, month, 1), ciCulture.DateTimeFormat.CalendarWeekRule, firstDayOfWeek);
}

This lets the .NET Framework do the thinking for me, and it is Globalization ready. Anywho the logic is::

Find the week number for the end of the month, subtract the week number for the start of the month… bam! Number of weeks.

The method is dependent on what day of the week a new week starts. So if your week starts on a Monday throw that it’s way.

Should this be in the framework? Probably …

PS:

using System.Globalization; //<-- ADD THAT;

MySQLdb error …

Posted by Adrian on May 10th, 2008

So I have been looking at the Django Project ( framework… whatever ). Installing that is pretty easy I would say. It was easier than installing SQL Server 2005 on Vista, definitely fewer warnings. It’s all fun and games until you come to the installation of MySQL and the driver for Python.

The driver can be found at :: http://www.djangoproject.com/r/python-mysql/ , or it’s respective SourceForge location. Anyway…

First off I didn’t have XCode installed so gcc did not exist. That was an easy fix I just downloaded XCode 3 and thought I’d be good to go. Problem came when I tried to run the setup.py script and found that gcc exits with an error. The problem is that XCode 3 installs gcc4 and there is a file in this driver called _mysql.c and it needs gcc3. This is due to the declaration of a variable or 2 and some pre processor directives that need to be changed/removed from that file.

The exact error was :

“/usr/include/sys/types.h:92: error: two or more data types in declaration specifiers”

So I tried to install gcc3 first, that’s a trap! You can try it but that failed for me for several reasons. So after much bangage of my head on the table I found this page that explained how to change that file::

http://www.keningle.com/?p=11

Fun stuff! You can follow the instructions there or you can download the modified package here:

mysql-python-122.zip

Note: You may want to check the actual SourceForge release of the python driver, it may be working by the time you read this. This is specific to version 1.2.2 .

Note 2: If you break your system running / doing anything you read or downloaded here I am not responsible, nor my webhost, or anyone else other than yourself.

For Shame !

Posted by Adrian on Apr 29th, 2008

So sad….

Suggestion

A Nirvana suggestion on a Metallica station…

GM EV1

Posted by Adrian on Apr 27th, 2008

So I had never heard of these cars before someone mentioned them to me, along with a documentary on them.

http://en.wikipedia.org/wiki/General_Motors_EV1

There are tons of videos on YouTube showing how they drive, pickup etc.

I can honestly say I REALLY want one!!! The WikiPedia article said that they could cost somewhere around 33k - 44k … I could deal with that. I paid somewhere around 22k for my TrailBlazer ( yeah I know … I wanted a Jetta but someone talked me into a friggin SUV ) and with gas prices right now I get both raped and pillaged at the pump. I don’t really drive a lot but I did the math and my SUV gets about 14 mpg. GM claims it gets about 16/22 - city/hwy. but I figure its 3 years old, tire pressure and so on affects that and the 16 is OPTIMAL results so 14 is about the expected range.

Anyway, there is a documentary Who Killed the Electric Car?, buy it here ,that chronicles the story of the electric car, and it’s destruction. It really is a shame that car companies are so damn short sighted. I cannot express my glee when I read about how GM is getting their ass handed to them lately. I remember reading that they lost somewhere around 38 billion last year. (article here)

Hope it was worth getting into bed with all the oil companies. I remember talking to a few folks that work for Genital Motors and their grand plan about a year and a half ago was to build more BIG TRUCKS! These trucks would have basically the old Northstar system where 4 cylinders out of 8 shut off…. Horrible idea! I drive in the city, and that is it. I might hit the highway once in a blue moon, but most of my driving would not take advantage of that. Such is the case with most people, and guess what I don’t hear folks buzzing about the great gas mileage that these new trucks are providing. Most people that I work with are looking at Toyota, being that they offer some of the more efficient hybrids. That’s not to say they’re angels, they too had an electric car and nuked it.

So by now I probably sound like a whore spreading the gospel… with my SUV and all. Well fear not. I drink my own cool aid. About a month ago I bought a Kawasaki Ninja 250R ( 70 mpg ), mind you I had no clue how to operate a motorcycle, but I fixed that problem. There are classes offered by community colleges around here for 25 bucks. I went to a week long class and bam, had my endorsement. Last week I spent $6.73 on gas for my motorcycle and, $15.00 for the SUV ( it’s still cold & rainy in Michigan so I can’t ride every day ). Point is though, that even as gas prices soared this week to $3.80 / gallon I actually had a decrease in gas usage / cost. Fun thing is that I’m not the only one, I’ve been hanging around the motorcycle dealership I went to and there are tons and tons of folks there looking for efficient transportation.

I figure once the lease ( yeah … i know ) is up on my wife’s car I can start looking into something more efficient than a Malibu for her, and I can start looking to sell my SUV and get something else. Now I still want an electric car since the EV1 looked nipply so I’ve been pokin around and found the following:

Tesla Motors:
http://www.teslamotors.com/

Phoenix Motorcars:
http://www.phoenixmotorcars.com/index.php

Aptera:
http://www.aptera.com/

Miles Electric Vehicles:
http://www.milesev.com/index.asp

Now then, out of all of these I think Phoenix or Miles would be the choice for me. Here’s why:

1. Tesla Motors offers a great roadster, I would say out of all of them it’s probably the best choice if you have 89k to spare… I for one do not.
2. Phoenix Motorcars offers 2 versions of their vehicle. The SUV ( 50k-ish ) and the SUT ( 40k-ish ). I could deal with that. Most likely the price will come down as demand goes up, but they have to do more advertising.
3. The Aptera is waaaaayyyy too futuristic looking for my taste. I’m just trying to get around not pilot a spaceship.
4. Miles Electric has some “Low Speed” options that go up to 25mph … that sucks. I don’t know where that’s an acceptable speed unless I want to drive down my road and back… even that hits 35mph in some spots… so not an option. They do offer an alternative that will do 80mph, and that I think would be golden for me. I don’t need to get very far, in a given day I drive about 20-30 miles. So the range isn’t an issue. Cost may be. I was unable to find the cost of their “Highway Speed” sedan.

I know two things:

1. I am sick of fueling the global terror machine by buying their oil ( don’t kid yourself you know it’s true )
2. I am sick of being a slave to our nations enemies, and taking it in the rear every time I need gas.

Hopefully more people start to wake up and smell the coffee!

This has got to be the funniest thing …

Posted by Adrian on Apr 26th, 2008

Freakin awesome !

http://flickr.com/photos/greggoconnell/57215394/

Gives a whole new meaning to tree hugger!

EDIT: AHAHAHAHAH!!!! Someone posts this as a response to that pic on Flickr
http://www.flickr.com/photos/stevewall/44148944/

Next »

Join the EFF!
Check Page Ranking