Featured Post

#SQLSat51 Nashville Post-Mortem

Well, another successful SQL Saturday has been logged in the books. I had a single session for this one for Change Data Capture and went out of my way to ensure that it was crammed packed with information for the people attending….even to the point that when the volunteer monitor in the room exclaimed  “I guess you know you have less than 10 minutes left.” . I politely replied I possess an advanced degree in Computation Physics. I am the Master of Space and Time. For those of you that would like to see the presentation along with the script files you can pick them up here. So now I would like to share some of my thoughts on how it all went. First off, what was great was that I received the feedback from my attendees before then end of the DAY! Yep, that’s right! Not a week later ….the same DAY! Just to let you know, I believe this is the first SQL Saturday that I have actually received my speaker feedback at all. How cool is that! For those of you that are wondering, here are the ole’ resultos: 5 (Excellent)  =  57.14 % 4                        =  35.57 % 3                        =  7.14 % 2                        = 0 % 1(You suck)    =  0 % So overall, really good. I only had 1 person give me less than a 4 but since I will be giving the presentation again in SQL Saturday #50 East Iowa, I will brush it up a little to see if I can max it out. Now on to some specific areas of the event that those  of you planning or thinking about planning can take to heart. Slide Templates The 2 sets of slide templates that were provided to us prior to the event were great. I think that the other SQL Saturday’s should definitely follow suite. Maybe even put forth the effort to have one of the peeps in you local group brand them specifically for you event. Here are some examples to show you what I mean: You win over a lot of IT people when you show Sneaky Ninja DBA Tips ala Naruto! Event Venue The venue was great. I talked with Kevin Kline and Joe Webb and they saved a lot of money by having the event at Nashville State Community College. The rooms were more than adequate in size and had all of the AV equipment needed. They even had one of the college technicians on hand to address any of the difficulties we had. I know of at least a couple of instances where that came in handy.   Sponsor Area This was one of the best setups for the sponsors that I have been at for a non-paid user group event EVER! We each had a a seperate area along the main drag with not only a extra long table but also a couple of comfy chairs with small tables. If we wanted to we could have pulled people in to have in depth conversations or show them something else that they were wanted to ask questions on. Definitely not the cramped quarters off in a separate room that I am used to. I know that these events are for the users but we have to make sure that there is something in it for the sponsors because they are ultimately the ones that are fronting the money to put these on. I am lucky enough that my employer wants me to go and speak at these and they also frequently pony up the money for sponsorships. Women in Technology Who woulda thunk it? But there are women in IT. All kidding aside, there were three high caliber women presenters in attendance. Hopefully, this is a continually growing trend as I believe this is a good thing for the community. I will probably be writing more on this in the future. Volunteers Not only did this event have a great cast of volunteer help but it was executed flawlessly. You had a volunteer in the room with you while your talk was going to do things like…get you water, keep track of time, address issues with AV, collect the feedback as people leave. This not only ensured that everything ran smoothly but that the feedback was collected from everyone. What a great idea!   Overall Overall, I give this event an A+! Big kudos go out to Kevin Kline, Joe Webb, Christina Leo, Louis Davidson  and everyone else involved. I will definitely plan on coming down next year! Cheers! AJ                          

Read More

SQL Server 2005 T-SQL: Mimic MERGE with OUTPUT

Posted by Arie | Posted in Featured, SQL Server | Posted on 22-07-2010

Tags: , , , , , , ,

0

Following up on my previous post a couple of people have asked me how they could accomplish this same thing because MERGE is not available in SQL Server 2005. So let’s take a look at how you can use the OUTPUT statement which is available to produce sort of the same affect.

So first we will set up our test data once again from the AdventureWorks database. This will act as the data that is coming into our target table

SELECT TOP 1000 [ContactID]
      ,[Title]
      ,[FirstName]
      ,[MiddleName]
      ,[LastName]
      ,[EmailAddress]
  INTO [AdventureWorks].[Person].[Contact_Temp]
  FROM [AdventureWorks].[Person].[Contact]

Then we’ll put some data into the target table…

  SELECT TOP 10 *
  INTO [AdventureWorks].[Person].[Contact_Base]
  FROM [AdventureWorks].[Person].[Contact_Temp] 

We change the email address to ensure that our T-SQL will in fact update what we are wanting to update

  UPDATE [AdventureWorks].[Person].[Contact_Base]
  SET [EmailAddress]='arie.jones@programmersedge.com'

Now we’ll take a look at the data to make sure it’s what we are expecting…..

image

So now we are going to write out statement that will mimic the MERGE stuff we did in the previous post. To clarify, we basically want to update the target table and if a particular ContactID exists then we want to update the EmailAddress column. If no such ContactID exists then we would like to insert the row.

Now OUTPUT statement works a lot like an inline TRIGGER in that you have access to an Inserted table. So you have access to the data that is actually being updated/inserted/whatever by the particular statement. So it’s not only good for this instance but also for other scenarios like logging. So we will first go and create as simple table variable to hold the ContactID values that we are going to update with our initial statement. Then we will be using the OUTPUT statement to fill up the table in conjunction with our UPDATE and lastly running an INSERT statement. Kind of a long way around the track when you look at the MERGE statement in the previous post…but hey ya’ gotta work with the tools you got.

 SET IDENTITY_INSERT [AdventureWorks].[Person].[Contact_Base] ON;  

 DECLARE @tblContacts table (ContId int); 

 UPDATE [AdventureWorks].[Person].[Contact_Base]
 SET EmailAddress=b.EmailAddress
 OUTPUT inserted.ContactID INTO  @tblContacts
 FROM [AdventureWorks].[Person].[Contact_Base] a INNER JOIN
 [AdventureWorks].[Person].[Contact_Temp] b ON a.ContactID=b.ContactID
;

INSERT INTO [AdventureWorks].[Person].[Contact_Base]
([ContactID],[Title],[FirstName],[MiddleName]
  ,[LastName],[EmailAddress])
SELECT [ContactID],[Title],[FirstName]
      ,[MiddleName],[LastName],[EmailAddress]
      FROM [AdventureWorks].[Person].[Contact_Temp]
WHERE ContactID NOT IN (SELECT ContId from @tblContacts);

Now when we look at the table with a simple SELECT statement we see that everything is as it should be….

image

SUCCESS! Hopefully, this helps someone else that may be in a jam because of using an older version of SQL Server ….

Cheers,

AJ

Write a comment