Stored Procedure to compare dates

boblarson

Smeghead
Local time
Today, 13:36
Joined
Jan 12, 2001
Messages
32,059
Can anyone help with the syntax to create a stored procedure to compare the date in one field (first record) to one field (first record) in another table?

I'm still an "infant" in the SQL Server world but I am in the middle of a job which requires me to do some work in it.
 
Compare and do what? Return the 2 dates, return whether they match or not, etc.

Any good golf courses around there? My wife and I will be traveling through there next month.
 
Compare and do what? Return the 2 dates, return whether they match or not, etc.
Right - Kick off a DTS package if they do not match.

Any good golf courses around there? My wife and I will be traveling through there next month.
Don't know as I'm not much of a golfer. In fact, I have such a wicked slice that I have to aim 45 degrees to the left to get it to go straight. :D

So are you driving up for the Summit and then using that time to do things on the way up and back?
 
All right, I'm not the best with SQL Server but this should work. We'll start by just checking the dates:

Code:
CREATE PROCEDURE [dbo].[procBobTest] 

AS

Declare @Test1 datetime, @Test2 datetime

SELECT TOP 1 YourDateField
INTO #Test1
FROM FirstTable
--add appropriate WHERE/ORDER BY clauses

SELECT @Test1 = YourDateField FROM #Test1

SELECT TOP 1 YourDateField
INTO #Test2
FROM SecondTable
--add appropriate WHERE/ORDER BY clauses

SELECT @Test2 = YourDateField FROM #Test2

If @Test1 <> @Test2
  Begin
    Select 'Diff'
  End
Else
  Begin
    Select 'Same'
  End

--or, to check the 2 dates being returned:
--SELECT @Test1, @Test2
GO

Test from Query Analyzer with

exec procBobTest

When that's doing what it should this should fire off the DTS package:

exec master..xp_cmdshell 'dtsrun /Sservername /npackagename /Uusername /Ppassword '

I was torn on going, but the wife has been wanting to go to Seattle anyway, so what the heck. Yes, weather permitting we're planning on a little recreation on the way.
 
I was torn on going, but the wife has been wanting to go to Seattle anyway, so what the heck. Yes, weather permitting we're planning on a little recreation on the way.

Thanks for the assistance on the SP. I will work on putting in my actual names, etc. and see how I do in getting it all to work. I still have a bit to go on learning all about SQL Server and Stored Procedures. Haven't needed them up until now, so I'm still learning that.

Well, if you come through Portland, maybe we can get together briefly just to say hi, or have breakfast, lunch, or dinner (depending on your schedule). It would be great to actually get to meet. Let me know. I just recently got to meet a few of the Seattle MVP's and one of the UA VIP's. So that was cool.
 
Yeah, it would be nice to finally meet somebody. I've been within a few miles of a guy on the other side of the country 3 times, and each time he's been too busy to meet. I don't think he actually wants to get together. We haven't worked out the specifics, so I don't know if we'll be coming up through Portland or not. There are some courses that could take us up the central/eastern part of the state.

Let me know how the SP works out. I tested that and got the "diff" or "same" as appropriate, and I copied the DTS call right out of a working SP, so it should be a matter of changing the names and then dropping the DTS call where

Select 'Diff'

is, and drop the Else bit.
 
I haven't incorporated the DTS part yet but it is appearing to work as needed. So, thanks again. Eventually I'll get to be able to write these myself. It just takes repetition and work.

As for meeting - don't worry about it if it doesn't work out. Go the way that is most pleasing (and possibly fuel efficient :D ) to you. You should enjoy this time and the trip.
 

Users who are viewing this thread

Back
Top Bottom