Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Microsoft SQL Server > Re: Please Help...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 11190 of 11517
Post > Topic >>

Re: Please Help - Can't check for duplicates

by Ed Murphy <emurphy42@[EMAIL PROTECTED] > Jun 29, 2008 at 10:57 PM

Lester wrote:

> Should I be using a trigger at the database level, or try to 'translate'

> this VBA/SQL to work with SQL Server?

The former will be bulletproof against any front end whatsoever.

> sqlstring = "SELECT Events.HallsID, Events.StartTime, Events.EndTime, 
> Events.StatusID, Events.EventName, Events.Date, Halls.HallsID, 
> Halls.[Hall Name] FROM Halls INNER JOIN Events ON Halls.HallsID = 
> Events.HallsID WHERE ((((Events.StatusID)=2 Or 
> (Events.StatusID)=3))AND((((Events.StartTime) Between #12/30/1899 ' & 
> StartTime & '# And #12/30/1899 ' & EndTime & '#) OR ((Events.EndTime) 
> Between #12/30/1899 ' & StartTime & '# And #12/30/1899 ' & EndTime & 
> '#)) AND ((Events.Date)= #' & BookDate & '# ) AND (Not(Events.EventName=

> '" & EventName & "'  ))AND((Halls.HallsID)= ' & Hall & ' )));"

This definitely won't work without some fixing up, but should at least
be in the ballpark:

create trigger Events_IU on Events as
begin

declare @[EMAIL PROTECTED]
 varchar(30)
declare @[EMAIL PROTECTED]
 varchar(30)
declare @[EMAIL PROTECTED]
 datetime
declare @[EMAIL PROTECTED]
 datetime
declare @[EMAIL PROTECTED]
 datetime

SELECT	@[EMAIL PROTECTED]
 = e.EventName,
	@[EMAIL PROTECTED]
 = h.[Hall Name],
	@[EMAIL PROTECTED]
 = e.Date,
	@[EMAIL PROTECTED]
 = e.StartTime,
	@[EMAIL PROTECTED]
 = e.EndTime
FROM	Events e
  INNER JOIN	Halls h ON h.HallsID = e.HallsID
WHERE	e.Date = inserted.Date
  AND	(e.StartTime between inserted.StartTime and inserted.EndTime
	  OR e.EndTime between inserted.StartTime and inserted.EndTime)
  AND	h.HallsID = inserted.HallsID
  AND	e.StatusID in (2,3) -- booked, tentative
  AND	e.EventName <> inserted.EventName

if @[EMAIL PROTECTED]
 is not null
begin
	raiserror('This conflicts with the %s booked in the %s Banquet Hall on
%s between %s and %s',
	  16, 1, @[EMAIL PROTECTED]
 @[EMAIL PROTECTED]
 @[EMAIL PROTECTED]
 @[EMAIL PROTECTED]
 @[EMAIL PROTECTED]
)
	rollback transaction
end

end

Note that this will always generate a false positive if you try to
change the EventName of an existing row.
 




 4 Posts in Topic:
Please Help - Can't check for duplicates
Lester <noreply@[EMAIL  2008-06-28 23:10:14 
Re: Please Help - Can't check for duplicates
"Albert D. Kallal&qu  2008-06-29 03:06:43 
Re: Please Help - Can't check for duplicates
Ed Murphy <emurphy42@[  2008-06-29 22:57:24 
Re: Please Help - Can't check for duplicates
Ed Murphy <emurphy42@[  2008-06-29 22:57:58 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 1:01:44 CST 2008.