I am not able to retrieve the values from a Trigger calling a stored procedure?

Please help! Hi, I'm trying to generate retrieve default values from a master table when I insert a detail table.

I am trying to do the following:

A. using a Stored Procedure to retrieve the values from and updating the master table and update  
B. using a 'Trigger' to insert default data into a detail table by calling my stored procedure with return values.

The problem is that I am not able to retrieve the values?

Trigger

ALTER TRIGGER Citations.InsertCaseNo_LogNo
    ON  Citations.LogHeader  
    AFTER INSERT    
AS      
       DECLARE
         @trgLogNo AS NVARCHAR(10),
         @trgLogType AS NVARCHAR(10),
         @trgLogTypeLogNo AS NVARCHAR(10),
         @retval int
         ;
    BEGIN        
         SET NOCOUNT ON;
         EXEC Citations.usp_RegNextCaseNo @trgLogNo OUTPUT,@trgLogType OUTPUT; --Execute Stored Procedure to retreive Defaluts
         SELECT @trgLogTypeLogNo = i.LogNo From Inserted I
         SET @trgLogTypeLogNo = @trgLogType + @trgLogNo  
     END;




Stored Procedure
The Stored Procedure returns values when executed separately

USE [TR_CASEMANAGER]
GO
/****** Object:  StoredProcedure [Citations].[usp_RegNextCaseNo]    
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [Citations].[usp_RegNextCaseNo]
   @DefaultCaseNo NVARCHAR(10) OUTPUT,
   @DefaultCaseType NVARCHAR(10) OUTPUT
AS

SELECT
 @DefaultCaseNo = DefaultCaseNo,
 @DefaultCaseType = (SELECT CitationID FROM Maintenance.LogTypeCodes WHERE INT_ID = DefaultLogTypeCode)
FROM SysConfiguration.MasterConfiguration
WHERE INT_ID = 1

UPDATE SysConfiguration.MasterConfiguration
SET DefaultCaseNo = @DefaultCaseNo
WHERE INT_ID = 1

RETURN @DefaultCaseNo
RETURN @DefaultCaseType
MIREESEAsked:
Who is Participating?
 
MIREESEAuthor Commented:
Ok I was able to get the trigger to work by using update logic to update just the record that was inserted

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON

GO
ALTER TRIGGER Citations.InsertCaseNo_LogNo
    ON  Citations.LogHeader  
    AFTER INSERT    
AS      
       DECLARE
         @trgLogNo AS NVARCHAR(10),
         @trgLogType AS NVARCHAR(10),
             @trgLogTypeID AS INT,
             @trgLogTypeLogNo AS NVARCHAR(10),
         @INT_ID INT
         ;
    BEGIN        
         SET NOCOUNT ON;
             SELECT * FROM
             Citations.LogHeader
             WHERE INT_ID = (SELECT i.INT_ID FROM Inserted i)
      
             EXEC Citations.usp_RegNextCaseNo @trgLogNo OUTPUT,@trgLogType OUTPUT,@trgLogTypeID OUTPUT; --Execute Stored Procedure and Return Values
             SET @trgLogTypeLogNo = @trgLogType + @trgLogNo;                                           --Concatenate VALUES returned FROM stored procedures

                UPDATE Citations.LogHeader                                                                          --Update Row that has just been inserted    
             SET LogNo = @trgLogTypeLogNo, RecordTypeCodeID = @trgLogTypeID
             WHERE INT_ID = (SELECT i.INT_ID FROM Inserted i)

           END;
0
 
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
In order to use the values returned by the OUTPUT parameters, you need to store them in the variables in your triggers. I do not have access to SSMS right now, but it would be something like this:

ALTER TRIGGER Citations.InsertCaseNo_LogNo
    ON  Citations.LogHeader  
    AFTER INSERT    
AS      
       DECLARE
         @trgLogNo AS NVARCHAR(10),
         @trgLogType AS NVARCHAR(10),
         @trgLogTypeLogNo AS NVARCHAR(10),
         @retval int
         ;
    BEGIN        
         SET NOCOUNT ON;
         EXEC Citations.usp_RegNextCaseNo @trgLogNo   = @trgLogNo OUTPUT, 
                                          @trgLogType = @trgLogType OUTPUT; --Execute Stored Procedure to retreive Defaluts
         SELECT @trgLogTypeLogNo = i.LogNo From Inserted I
         SET @trgLogTypeLogNo = @trgLogType + @trgLogNo  
     END;

Open in new window


However, I do have a couple of concerns with this implementation:
#1. The RETURN clause is not required in the stored procedure definition
#2. I belive your design is based on the assumption that you will always be updating one record. This may or may not be the case. The implementation will fail when you update multiple records at the same time
#3. Please note that you cannot "return" data from a trigger to a calling code. Hence, I assume that @trgLogTypeLogNo and @retval are being used in the trigger down the line
0
 
MIREESEAuthor Commented:
Hi Nakul


This trigger will be used to update a single record. I was using the @trgLogNo and @trgLogType as the variables to store the returned data from the stored procedure. Not sure what you are saying regarding #3 but I would like to have the trigger populate required columns only.
0
 
MIREESEAuthor Commented:
Ok the trigger retrieving the data from the stored procedure, However, an additional row is generated when I perform an insert and the trigger fires.

Trigger
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON

GO
ALTER TRIGGER Citations.InsertCaseNo_LogNo
    ON  Citations.LogHeader  
    AFTER INSERT    
AS      
       DECLARE
         @trgLogNo AS NVARCHAR(10),
         @trgLogType AS NVARCHAR(10),
             @trgLogTypeLogNo AS NVARCHAR(10),
         @INT_ID INT
         ;
    BEGIN        
         SET NOCOUNT ON;
       SELECT @INT_ID = i.INT_ID, @trgLogNo = i.LogNo From Inserted I        
           EXEC Citations.usp_RegNextCaseNo @trgLogNo OUTPUT,@trgLogType OUTPUT;      --Execute Stored Procedure and Return Values
       SET @trgLogTypeLogNo = @trgLogNo + @trgLogType;                                                     --Concatenate VALUES returned FROM stored procedures

             INSERT INTO Citations.LogHeader (LogNo) VALUES (@trgLogTypeLogNo);    --Insert Concatenated value in Table  

             END;
0
 
MIREESEAuthor Commented:
I was able to resolve issue
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.