If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Subquery returned more than 1 value

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-10, 13:17
dorkygrin dorkygrin is offline
Registered User
 
Join Date: Feb 2010
Posts: 4
Subquery returned more than 1 value

Using SQL Server 2008 Express. Making a trigger to pull the employee# based on the EmployeeName.

I can't figure out why I'm getting the message that the subquery returned more than 1 value. Does it have something to do with nulls?

Here's my trigger:


USE [Personnel]
GO
/****** Object: Trigger [dbo].[GetEmpNo] Script Date: 02/08/2010 12:44:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER TRIGGER [dbo].[GetEmpNo]
ON [dbo].[Disciplinary_Action_Form]
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for trigger here
DECLARE @Name VARCHAR(50)
DECLARE @EEno VARCHAR(5)
DECLARE @FULL VARCHAR(50)

SELECT @Name = (Select Name from Inserted)
Select @Full = (Select "Full" from dbo.Employees$)
SELECT @EEno = (Select EmpNo from dbo.Employees$ where "Full" = @Name)

UPDATE dbo.Disciplinary_Action_Form
SET Name=@Name, EEno=@EEno
Where F_DocumentID = (select F_DocumentID from inserted)

END
Reply With Quote
  #2 (permalink)  
Old 02-08-10, 13:21
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 3,362
So, how many records do you expect this to return:

Select "Full" from dbo.Employees$
Reply With Quote
  #3 (permalink)  
Old 02-08-10, 14:07
dorkygrin dorkygrin is offline
Registered User
 
Join Date: Feb 2010
Posts: 4
Yep, that was it. I don't know why I had that in there. Always nice to have an extra set of eyes. Thanks!
Reply With Quote
  #4 (permalink)  
Old 02-08-10, 23:35
TerryP TerryP is offline
Registered User
 
Join Date: Feb 2007
Posts: 38
Hi

Apart from the issue you have for multiple returned FULL, your trigger will also be in trouble if multiple rows inserted at the same time like below:

INSERT INTO [dbo].[Disciplinary_Action_Form]
SELECT top 100 *
FROM [dbo].[Other_Disciplinary_Action_Form]

Your trigger will only take care of the last row entered out of 100 rows.

You need to loop through all the rows in the INSERTED table as below:

DECLARE Disciplinary_Action_Form_Cursor CURSOR FOR
SELECT Name
FROM Inserted

OPEN Disciplinary_Action_Form_Cursor
FETCH NEXT FROM Disciplinary_Action_Form_Cursor INTO @Name

-- Loop through cursor until finished
WHILE @@FETCH_STATUS = 0
BEGIN

-- place your code here for after insert action


--Fetch_Next:
FETCH NEXT FROM Disciplinary_Action_Form_Cursor INTO @Name

END

This routine also should be applied to UPDATE and DELETE triggers as well.
Reply With Quote
  #5 (permalink)  
Old 02-09-10, 09:26
MCrowley MCrowley is offline
Wage drone 24601
 
Join Date: Jan 2003
Location: Massachusetts
Posts: 4,227
Rather than the cursor (which is usually suspect for performance problems), you should probably join to the inserted table in this case. Instead of
Code:
UPDATE dbo.Disciplinary_Action_Form
SET Name=@Name, EEno=@EEno
Where F_DocumentID = (select F_DocumentID from inserted)
You might have
Code:
UPDATE dbo.Disciplinary_Action_Form daf
SET Name=i.Name, EEno=@EEno
from inserted i 
Where daf.F_DocumentID = i.F_DocumentID
You will of course need to work EEno in there, but this should get you started.
Reply With Quote
Reply

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On