MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 27th, 2004, 12:27 AM
wlit wlit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 12 wlit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Question on insert Trigger? Plz Help.

I am beginner of MS SQL, currently work on a school project that require the use of trigger on insert. I am trying to create a trigger that no duplicate last and first name accept. Here is the code for the table:

create table employee(
emp_id numeric(5,0) identity primary key,
job_id numeric(5,0) not null foreign key references job(job_id),
emp_last varchar(20) not null,
emp_first varchar(20) not null,
emp_phone varchar(15) not null,
emp_add varchar(30) not null,
emp_city varchar(20) not null,
emp_state varchar(2) not null,
emp_zip numeric(5,0) not null,
emp_dob smalldatetime NOT NULL default '0000-00-00',
emp_hiredate smalldatetime NOT NULL default '0000-00-00',
emp_payrate numeric(5,2) not null,
total_sales numeric(9,2) not null default 0,
termination varchar(3) not null default 'NO',
termin_date nulldate null)


Here is the code for the trigger:

create trigger trg_add_emp
on employee
for insert as

declare @job_id numeric(5,0),
@emp_last varchar(20),
@emp_first varchar(20),
@emp_phone varchar(15),
@emp_add varchar(30),
@emp_city varchar(20),
@emp_state varchar(2),
@emp_zip numeric(5,0),
@emp_dob datetime,
@emp_hiredate datetime,
@emp_payrate numeric(5,2),
@total_sales numeric(9,2),
@termination varchar(3),
@termin_date datetime

select @job_id=(select job_id from inserted)
select @emp_last=(select emp_last from inserted)
select @emp_first=(select emp_first from inserted)
select @emp_phone=(select emp_phone from inserted)
select @emp_add=(select emp_add from inserted)
select @emp_city=(select emp_city from inserted)
select @emp_state=(select emp_state from inserted)
select @emp_zip=(select emp_zip from inserted)
select @emp_dob=(select emp_dob from inserted)
select @emp_hiredate=(select emp_hiredate from inserted)
select @emp_payrate=(select emp_payrate from inserted)
select @total_sales=(select total_sales from inserted)
select @termination=(select termination from inserted)
select @termin_date=(select termin_date from inserted)

ELSE IF EXISTS
(SELECT 1 from employee
WHERE @emp_first=employee.emp_first
AND @emp_last=employee.emp_last)
Begin
Print'Employee '+ @emp_first + ' ' + @emp_last
+ ' already exists'
Print'Insert Fails'
ROLLBACK TRANSACTION
END


The problem i have is that no matter the First name and Last name exist or not, it will rollback the transaction. I kinda know the reason why, bcuz tigger is being insert first then compare the same insert value. How can I fix this problem?

The second question I have is that on the termination date, is there anyway I can store NULL instead of the system store a default day which is 1900/xx/xx. I have try to use the following.

EXEC sp_addtype nulldate, datetime, 'NULL'
GO


Any comment would be appreciate. Thank you!!

Reply With Quote
  #2  
Old April 27th, 2004, 03:00 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
Code:
create trigger trg_add_emp
on employee
for insert as

IF EXISTS
(SELECT 1 from employee join inserted 
WHERE inseretd.emp_first=employee.emp_first
AND inserted.emp_last=employee.emp_last)
Begin
Print 'Duplicate employee name'
Print'Insert Fails'
ROLLBACK TRANSACTION
END


You must handle the case with multiple rows being inserted so you can not use variables like in your example.

You shouldn't use a trigger for this. Use a unique constraint.

Reply With Quote
  #3  
Old April 27th, 2004, 10:02 AM
wlit wlit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 12 wlit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How can I have a unique constraint for both First & Last Name together, but not unique constraint on individual attribute?

Reply With Quote
  #4  
Old April 27th, 2004, 10:09 AM
wlit wlit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 12 wlit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
BTW, the code you have does work... it show me an error message about the "where" cause...????

Reply With Quote
  #5  
Old April 27th, 2004, 10:36 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
Code:
create table ogrish(... , constraint uniqueName unique(firstName,lastName), ...


What error do you get? I assume you corrected the misspelling of inserted.

Reply With Quote
  #6  
Old April 27th, 2004, 12:33 PM
wlit wlit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 12 wlit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question

swampBoogie, thank you for all your reply. I have another question about trigger on deleteion.

create trigger trg_del_emp
on employee
for delete as

IF EXISTS
(SELECT 1 FROM employee
WHERE employee.termination='NO')
Begin
Print'Employee are not yet terminate due to he/she is active employee'
Print'Delete Failed'
ROLLBACK TRANSACTION
END


why this doesn't work even thou if an employee is has a termination of "YES"? How could i fix it?

Reply With Quote
  #7  
Old April 27th, 2004, 12:54 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
Shouldn't you check for this condition in the deleted table only?

Code:
create trigger trg_del_emp
on employee
for delete as

IF EXISTS
(SELECT 1 FROM deleted
WHERE termination='NO')
Begin
Print'Employee are not yet terminate due to he/she is active employee'
Print'Delete Failed'
ROLLBACK TRANSACTION
END

Reply With Quote
  #8  
Old April 28th, 2004, 03:32 PM
wlit wlit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 12 wlit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question

I am trying to write a trigger on insert. However, on the phone field I want to limited the area code that is allow to enter. The code below doesn't seem working. Any suggestion?

create trigger trg_add_cust
on customer
for insert as

declare @cust_last varchar(20),
@cust_first varchar(20),
@cust_phone varchar(15),
@cust_add varchar(30),
@cust_city varchar(20),
@cust_state varchar(2),
@cust_zip numeric(5,0),

select @cust_last=(select cust_last from inserted)
select @cust_last=(select cust_last from inserted)
select @cust_phone=(select cust_phone from inserted)
select @cust_add=(select cust_add from inserted)
select @cust_city=(select cust_city from inserted)
select @cust_state=(select cust_state from inserted)
select @cust_zip=(select cust_zip from inserted)

IF NOT EXISTS
(SELECT 1 FROM CUSTOMER
WHERE @cust_phone = '410%'
OR @cust_phone ='301%')
Begin
Print 'Area code enter must be either 410 or 301'
Print 'Please re-enter'
ROLLBACK TRANSACTION
END

Reply With Quote
  #9  
Old April 28th, 2004, 03:48 PM
wlit wlit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 12 wlit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I think I got it, does this code look ok?

IF NOT EXISTS
(SELECT 1 FROM CUSTOMER
WHERE @cust_phone like '[410]%'
OR @cust_phone like '[301]%')

Reply With Quote
  #10  
Old April 29th, 2004, 02:09 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
You shouldn't use a trigger for that. Use a check constraint.

Reply With Quote
  #11  
Old April 29th, 2004, 10:21 AM
punkisdead's Avatar
punkisdead punkisdead is offline
Moron
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Michigan, USA
Posts: 170 punkisdead User rank is Private First Class (20 - 50 Reputation Level)punkisdead User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 31 m 37 sec
Reputation Power: 5
Quote:
Originally Posted by wlit


ELSE IF EXISTS
(SELECT 1 from employee
WHERE @emp_first=employee.emp_first
AND @emp_last=employee.emp_last)
Begin
Print'Employee '+ @emp_first + ' ' + @emp_last
+ ' already exists'
Print'Insert Fails'
ROLLBACK TRANSACTION
END




I could be wrong but shouldn't this read:


ELSE IF EXISTS
(SELECT 1 from employee
WHERE employee.emp_first = @emp_first
AND employee.emp_last = @emp_last)
Begin
Print'Employee '+ @emp_first + ' ' + @emp_last
+ ' already exists'
Print'Insert Fails'
ROLLBACK TRANSACTION
END


Using the comparison of employee.emp_first = @emp_first....
Instead of the assignment of @emp_first = employee.emp_first?

Reply With Quote
  #12  
Old April 29th, 2004, 11:15 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
Nope. It is not considered to be an assignment in this context.

Whether you use

Code:
where @a = c


or

Code:
where c = @a


does not matter.

SQL server will have compatibilty issues with implementing a boolean type though.

Reply With Quote
  #13  
Old April 29th, 2004, 11:54 PM
wlit wlit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 12 wlit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Question on Datediff function.

Once again I thank you for all the reply. Especially one from “swampBoogie.” I do learn a lot by asking. As of the original post, I decide to use unique constraint when I create the table. I make so much sense, even thou it take away my cheesy respond message from trigger.

The second or third question I have is about DATEFIFF:

create table time_detail(
emp_id numeric(5,0) not null foreign key references employee(emp_id),
time_in smalldatetime not null default getdate(),
time_out smalldatetime not null default '')


select 'Total Hours' = datediff(minute, 4/26/2004 9:00 4/26/2004 17:15) from time_detail


Is there are anyway I can compare two day and time to have a result that tell me how many hours with minute different. For the example above, it is 8 hours and 15 mins different. In mySQL is there a way to do something like this?

In other word, I want the system to calculate and list all emp_id, time_in, time_out, and an extra column that tell me how many hours with minutes an employee work? Thank you in advance.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Question on insert Trigger? Plz Help.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump