Create a stored procedure to do two inserts.

I need to create one stored procedure and
do two inserts into separate tables. They have common
paramaters except for one.

create sprInsertIntoTicketsAndIndexTicketPOCS
(
@ticketNumber AS nvarchar(50) = null,
@ticketType AS nvarchar(50) = null,
@ticketStatus AS nvarchar(50) = null,
@ticketSeverity AS nvarchar(50) = null,
@shortDescription AS nvarchar(1000) = null,
@externalTicket AS nvarchar(1000) = null,
@reasonMenu AS nvarchar(50) = null,
@problemNote AS ntext = null,
@solutionNote AS ntext = null,
@actionNote AS ntext = null,
@followupNote AS ntext = null,
@companiesFlag AS bit = null,
@agentCreated AS nvarchar(50) = null,
@dateCreated AS datetime = null,
@dateLastModified AS datetime = null,
@agentClosed AS nvarchar(50) = null,
@dateClosed AS datetime = null,
@agentFollowup AS nvarchar(50) = null,
@followupBy AS datetime = null,
@deletedFlag AS bit = null,
@ticketTypeOld AS nvarchar(50) = null,
@assignedTo AS varchar(50) = null,
@requestType AS varchar(50) = null,
@serviceImpact AS varchar(50) = null,
@dateTimeAssigned AS datetime = null,
@priority AS varchar(50) = null,
@dateAssigned AS datetime = null,
@trackIt AS varchar(50) = null,
@contactMethod AS varchar(50) = null,
@locationSIMs AS varchar(50) = null,
@userID AS nvarchar(50) = null,
@ticketApplicationType AS nvarchar(50) = null,
@accountID AS int = null,
@ticketID INT OUTPUT,
@resultCode INT OUTPUT,
@resultMessage NVARCHAR(200) OUTPUT
)
AS
 
BEGIN
      BEGIN TRANSACTION
      BEGIN TRY
 
 
 
 
INSERT INTO
  [dbo].[tbl_tickets]
(
  [TicketNumber],
  [TicketType],
  [TicketStatus],
  [TicketSeverity],
  [ShortDescription],
  [ExternalTicket],
  [ReasonMenu],
  [ProblemNote],
  [SolutionNote],
  [ActionNote],
  [FollowupNote],
  [CompaniesFlag],
  [AgentCreated],
  [DateCreated],
  [DateLastModified],
  [AgentClosed],
  [DateClosed],
  [AgentFollowup],
  [FollowupBy],
  [DeletedFlag],
  [TicketTypeOld],
  [AssignedTo],
  [RequestType],
  [ServiceImpact],
  [DateTimeAssigned],
  [Priority],
  [DateAssigned],
  [TrackIt],
  [ContactMethod],
  [LocationSIMs],
  [UserID],
  [TicketApplicationType],
  [AccountID]
)
VALUES
(
  @ticketNumber,
  @ticketType,
  @ticketStatus,
  @ticketSeverity,
  @shortDescription,
  @externalTicket,
  @reasonMenu,
  @problemNote,
  @solutionNote,
  @actionNote,
  @followupNote,
  @companiesFlag,
  @agentCreated,
  @dateCreated,
  @dateLastModified,
  @agentClosed,
  @dateClosed,
  @agentFollowup,
  @followupBy,
  @deletedFlag,
  @ticketTypeOld,
  @assignedTo,
  @requestType,
  @serviceImpact,
  @dateTimeAssigned,
  @priority,
  @dateAssigned,
  @trackIt,
  @contactMethod,
  @locationSIMs,
  @userID,
  @ticketApplicationType,
  @accountID,
  @pocID
)

We only need to add the pocID parameter and then what we can do is when do the first insert
into table tickets retrieve the TicketID and pass this into the tbl index ticket pocs

the other parametes are included above. and agentadded
 @agentCreated, ==> agentadded
  @dateCreated, ==> dateadded
  @DateCreated = DateTime.Now
===========================================

Let's build it.
I appreciate any help. On output we want ticketID
and ticketPOCIndexID.

Thanks everyone.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 
 
 
ALTER PROCEDURE [dbo].[sprInsertIntoTickets]
(
@ticketNumber AS nvarchar(50) = null,
@ticketType AS nvarchar(50) = null,
@ticketStatus AS nvarchar(50) = null,
@ticketSeverity AS nvarchar(50) = null,
@shortDescription AS nvarchar(1000) = null,
@externalTicket AS nvarchar(1000) = null,
@reasonMenu AS nvarchar(50) = null,
@problemNote AS ntext = null,
@solutionNote AS ntext = null,
@actionNote AS ntext = null,
@followupNote AS ntext = null,
@companiesFlag AS bit = null,
@agentCreated AS nvarchar(50) = null,
@dateCreated AS datetime = null,
@dateLastModified AS datetime = null,
@agentClosed AS nvarchar(50) = null,
@dateClosed AS datetime = null,
@agentFollowup AS nvarchar(50) = null,
@followupBy AS datetime = null,
@deletedFlag AS bit = null,
@ticketTypeOld AS nvarchar(50) = null,
@assignedTo AS varchar(50) = null,
@requestType AS varchar(50) = null,
@serviceImpact AS varchar(50) = null,
@dateTimeAssigned AS datetime = null,
@priority AS varchar(50) = null,
@dateAssigned AS datetime = null,
@trackIt AS varchar(50) = null,
@contactMethod AS varchar(50) = null,
@locationSIMs AS varchar(50) = null,
@userID AS nvarchar(50) = null,
@ticketApplicationType AS nvarchar(50) = null,
@accountID AS int = null,
@ticketID INT OUTPUT,
@resultCode INT OUTPUT,
@resultMessage NVARCHAR(200) OUTPUT
) 
AS
 
BEGIN
      BEGIN TRANSACTION
      BEGIN TRY
 
 
 
 
INSERT INTO
  [dbo].[tbl_tickets]
(
  [TicketNumber],
  [TicketType],
  [TicketStatus],
  [TicketSeverity],
  [ShortDescription],
  [ExternalTicket],
  [ReasonMenu],
  [ProblemNote],
  [SolutionNote],
  [ActionNote],
  [FollowupNote],
  [CompaniesFlag],
  [AgentCreated],
  [DateCreated],
  [DateLastModified],
  [AgentClosed],
  [DateClosed],
  [AgentFollowup],
  [FollowupBy],
  [DeletedFlag],
  [TicketTypeOld],
  [AssignedTo],
  [RequestType],
  [ServiceImpact],
  [DateTimeAssigned],
  [Priority],
  [DateAssigned],
  [TrackIt],
  [ContactMethod],
  [LocationSIMs],
  [UserID],
  [TicketApplicationType],
  [AccountID]
)
VALUES
(
  @ticketNumber,
  @ticketType,
  @ticketStatus,
  @ticketSeverity,
  @shortDescription,
  @externalTicket,
  @reasonMenu,
  @problemNote,
  @solutionNote,
  @actionNote,
  @followupNote,
  @companiesFlag,
  @agentCreated,
  @dateCreated,
  @dateLastModified,
  @agentClosed,
  @dateClosed,
  @agentFollowup,
  @followupBy,
  @deletedFlag,
  @ticketTypeOld,
  @assignedTo,
  @requestType,
  @serviceImpact,
  @dateTimeAssigned,
  @priority,
  @dateAssigned,
  @trackIt,
  @contactMethod,
  @locationSIMs,
  @userID,
  @ticketApplicationType,
  @accountID
)
 
SET @ticketID=SCOPE_IDENTITY()
UPDATE tbl_tickets SET TicketNumber=CAST((@ticketID + 10) AS VARCHAR)
WHERE TicketID = @ticketID
 
            SET @resultCode = 0
            SET @resultMessage = 'ticketID ' + CAST(@ticketID  AS NVARCHAR(200)) + ': ' + @ticketNumber  + ' was created.'
            
 
      END TRY
 
      BEGIN CATCH
            IF @@TRANCOUNT > 1 ROLLBACK
            
            SET @ticketID = 0
            SET @resultCode = 1
            SET @resultMessage = 'ticketID ' + CAST(@ticketID AS NVARCHAR(200)) + ': ' + @ticketNumber + ' created error.  ' + char(13) + char(10)
                        + ERROR_NUMBER() + '. '  + char(13) + char(10)
                        + ERROR_MESSAGE() + '. ' + char(13) + char(10)
                        + ERROR_LINE() + '. ' + char(13) + char(10)
                        + ERROR_PROCEDURE() + '. ' + char(13) + char(10)
                        + ERROR_STATE() + '. ' + char(13) + char(10)
 
      END CATCH
 
  COMMIT TRAN
END
----------------------------------------------------------------------------
Second stored proc.
 
 
------------------------------------------------------------------------
ALTER PROCEDURE [dbo].[sprInsertIntoIndexTicketPOCs]
( 
     @ticketID INT = null,
     @pocID INT = null, 
     @dateAdded DATETIME = null,
     @agentAdded NVARCHAR(50) = null,
     @dateCreated DATETIME,
     @ticketPOCIndexID INT = null OUTPUT,
     @resultCode INT OUTPUT,
     @resultMessage NVARCHAR(200) OUTPUT
)
 AS  
 BEGIN  
         SET NOCOUNT ON
         INSERT INTO [tbl_index_ticket_pocs]
         (
                 [TicketID],
                 [POCID],
                 [DateAdded],
                 [AgentAdded],
                 [DateCreated]
         )
         VALUES
         (
                 @ticketID,
                 @pocID,
                 @dateAdded,
                 @agentAdded,
                 @DateCreated
         )
         SET @ticketPOCIndexID=SCOPE_IDENTITY()
         
    SET @resultCode = 0
	SET @resultMessage = 'ticketPOCIndexID ' + CONVERT(NVARCHAR(200), @ticketPOCIndexID) + ': ' + CONVERT(NVARCHAR(200), @ticketPOCIndexID)  + ' was created.'
 
 END     
 
---------------------------------------------------------------------------------------------------------

Open in new window

mathieu_cuprykAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
do u want to move the code of the second inside the first or u just need to call the second sp inside the first

ALTER PROCEDURE [dbo].[sprInsertIntoTickets]
(
@ticketNumber AS nvarchar(50) = null,
@ticketType AS nvarchar(50) = null,
@ticketStatus AS nvarchar(50) = null,
@ticketSeverity AS nvarchar(50) = null,
@shortDescription AS nvarchar(1000) = null,
@externalTicket AS nvarchar(1000) = null,
@reasonMenu AS nvarchar(50) = null,
@problemNote AS ntext = null,
@solutionNote AS ntext = null,
@actionNote AS ntext = null,
@followupNote AS ntext = null,
@companiesFlag AS bit = null,
@agentCreated AS nvarchar(50) = null,
@dateCreated AS datetime = null,
@dateLastModified AS datetime = null,
@agentClosed AS nvarchar(50) = null,
@dateClosed AS datetime = null,
@agentFollowup AS nvarchar(50) = null,
@followupBy AS datetime = null,
@deletedFlag AS bit = null,
@ticketTypeOld AS nvarchar(50) = null,
@assignedTo AS varchar(50) = null,
@requestType AS varchar(50) = null,
@serviceImpact AS varchar(50) = null,
@dateTimeAssigned AS datetime = null,
@priority AS varchar(50) = null,
@dateAssigned AS datetime = null,
@trackIt AS varchar(50) = null,
@contactMethod AS varchar(50) = null,
@locationSIMs AS varchar(50) = null,
@userID AS nvarchar(50) = null,
@ticketApplicationType AS nvarchar(50) = null,
@accountID AS int = null,
@pocID INT = null,
@agentAdded NVARCHAR(50) = null,
@ticketID INT OUTPUT,
@resultCode INT OUTPUT,
@resultMessage NVARCHAR(200) OUTPUT,
     @ticketPOCIndexID INT = null OUTPUT
)
AS
 
BEGIN
      BEGIN TRANSACTION
      BEGIN TRY
                   INSERT INTO
                    [dbo].[tbl_tickets]
                  (
                    [TicketNumber],  [TicketType],        [TicketStatus],        [TicketSeverity],        [ShortDescription],        [ExternalTicket],
                    [ReasonMenu],        [ProblemNote],        [SolutionNote],        [ActionNote],        [FollowupNote],        [CompaniesFlag],
                    [AgentCreated],        [DateCreated],        [DateLastModified],        [AgentClosed],        [DateClosed],        [AgentFollowup],
                    [FollowupBy],        [DeletedFlag],        [TicketTypeOld],        [AssignedTo],        [RequestType],        [ServiceImpact],
                    [DateTimeAssigned],        [Priority],        [DateAssigned],        [TrackIt],        [ContactMethod],        [LocationSIMs],
                    [UserID],        [TicketApplicationType],        [AccountID]
                  )
                  VALUES
                  (
                    @ticketNumber,        @ticketType,        @ticketStatus,        @ticketSeverity,        @shortDescription,        @externalTicket,
                    @reasonMenu,        @problemNote,        @solutionNote,        @actionNote,        @followupNote,        @companiesFlag,
                    @agentCreated,        @dateCreated,        @dateLastModified,        @agentClosed,        @dateClosed,        @agentFollowup,
                    @followupBy,        @deletedFlag,        @ticketTypeOld,        @assignedTo,        @requestType,        @serviceImpact,
                    @dateTimeAssigned,        @priority,        @dateAssigned,        @trackIt,        @contactMethod,        @locationSIMs,
                    @userID,        @ticketApplicationType,        @accountID
                  )
             
                  SET @ticketID=SCOPE_IDENTITY()
                  UPDATE tbl_tickets SET TicketNumber=CAST((@ticketID + 10) AS VARCHAR)
                  WHERE TicketID = @ticketID
                  
                  SET @resultCode = 0
                  SET @resultMessage = 'ticketID ' + CAST(@ticketID  AS NVARCHAR(200)) + ': ' + @ticketNumber  + ' was created.'

         INSERT INTO [tbl_index_ticket_pocs]
         ( [TicketID], [POCID], [DateAdded],[AgentAdded], [DateCreated] )
         VALUES
         ( @ticketID,@pocID,@DateCreated,@agentCreated,GETDATE()          )
         SET @ticketPOCIndexID=SCOPE_IDENTITY()
                       
             
      END TRY
 
      BEGIN CATCH
            IF @@TRANCOUNT > 1 ROLLBACK
           
            SET @ticketID = 0
            SET @resultCode = 1
            SET @resultMessage = 'ticketID ' + CAST(@ticketID AS NVARCHAR(200)) + ': ' + @ticketNumber + ' created error.  ' + char(13) + char(10)
                        + ERROR_NUMBER() + '. '  + char(13) + char(10)
                        + ERROR_MESSAGE() + '. ' + char(13) + char(10)
                        + ERROR_LINE() + '. ' + char(13) + char(10)
                        + ERROR_PROCEDURE() + '. ' + char(13) + char(10)
                        + ERROR_STATE() + '. ' + char(13) + char(10)
 
      END CATCH
 
  COMMIT TRAN
END
 


0
Aneesh RetnakaranDatabase AdministratorCommented:
mathieu_cupryk,



Aneesh
ALTER PROCEDURE [dbo].[sprInsertIntoTickets]
(
@ticketNumber AS nvarchar(50) = null,
@ticketType AS nvarchar(50) = null,
@ticketStatus AS nvarchar(50) = null,
@ticketSeverity AS nvarchar(50) = null,
@shortDescription AS nvarchar(1000) = null,
@externalTicket AS nvarchar(1000) = null,
@reasonMenu AS nvarchar(50) = null,
@problemNote AS ntext = null,
@solutionNote AS ntext = null,
@actionNote AS ntext = null,
@followupNote AS ntext = null,
@companiesFlag AS bit = null,
@agentCreated AS nvarchar(50) = null,
@dateCreated AS datetime = null,
@dateLastModified AS datetime = null,
@agentClosed AS nvarchar(50) = null,
@dateClosed AS datetime = null,
@agentFollowup AS nvarchar(50) = null,
@followupBy AS datetime = null,
@deletedFlag AS bit = null,
@ticketTypeOld AS nvarchar(50) = null,
@assignedTo AS varchar(50) = null,
@requestType AS varchar(50) = null,
@serviceImpact AS varchar(50) = null,
@dateTimeAssigned AS datetime = null,
@priority AS varchar(50) = null,
@dateAssigned AS datetime = null,
@trackIt AS varchar(50) = null,
@contactMethod AS varchar(50) = null,
@locationSIMs AS varchar(50) = null,
@userID AS nvarchar(50) = null,
@ticketApplicationType AS nvarchar(50) = null,
@accountID AS int = null,
@pocID INT = null,
@agentAdded NVARCHAR(50) = null,
@ticketID INT OUTPUT,
@resultCode INT OUTPUT,
@resultMessage NVARCHAR(200) OUTPUT,
     @ticketPOCIndexID INT = null OUTPUT
) 
AS
 
BEGIN
	  declare @now datetime 
	  SELECT @now  = GETDATE() 
      BEGIN TRANSACTION
      BEGIN TRY
 			INSERT INTO
			  [dbo].[tbl_tickets]
			(
			  [TicketNumber],  [TicketType],	  [TicketStatus],	  [TicketSeverity],	  [ShortDescription],	  [ExternalTicket],
			  [ReasonMenu],	  [ProblemNote],	  [SolutionNote],	  [ActionNote],	  [FollowupNote],	  [CompaniesFlag],
			  [AgentCreated],	  [DateCreated],	  [DateLastModified],	  [AgentClosed],	  [DateClosed],	  [AgentFollowup],
			  [FollowupBy],	  [DeletedFlag],	  [TicketTypeOld],	  [AssignedTo],	  [RequestType],	  [ServiceImpact],
			  [DateTimeAssigned],	  [Priority],	  [DateAssigned],	  [TrackIt],	  [ContactMethod],	  [LocationSIMs],
			  [UserID],	  [TicketApplicationType],	  [AccountID]
			)
			VALUES
			(
			  @ticketNumber,	  @ticketType,	  @ticketStatus,	  @ticketSeverity,	  @shortDescription,	  @externalTicket,
			  @reasonMenu,	  @problemNote,	  @solutionNote,	  @actionNote,	  @followupNote,	  @companiesFlag,
			  @agentCreated,	  @dateCreated,	  @dateLastModified,	  @agentClosed,	  @dateClosed,	  @agentFollowup,
			  @followupBy,	  @deletedFlag,	  @ticketTypeOld,	  @assignedTo,	  @requestType,	  @serviceImpact,
			  @dateTimeAssigned,	  @priority,	  @dateAssigned,	  @trackIt,	  @contactMethod,	  @locationSIMs,
			  @userID,	  @ticketApplicationType,	  @accountID
			)
		 
			SET @ticketID=SCOPE_IDENTITY()
			UPDATE tbl_tickets SET TicketNumber=CAST((@ticketID + 10) AS VARCHAR)
			WHERE TicketID = @ticketID
			 
			SET @resultCode = 0
			SET @resultMessage = 'ticketID ' + CAST(@ticketID  AS NVARCHAR(200)) + ': ' + @ticketNumber  + ' was created.' 
 
 
            exec [dbo].[sprInsertIntoIndexTicketPOCs]  
				 @ticketID  = @ticketID ,
				 @pocID  = @pocID, 
				 @dateAdded = @DateCreated,
				 @agentAdded = @agentCreated,
				 @dateCreated = @now ,
				 @ticketPOCIndexID = @ticketPOCIndexID OUTPUT,
				 @resultCode = @resultCode OUTPUT,
				 @resultMessage = @resultMessage OUTPUT
      END TRY
 
      BEGIN CATCH
            IF @@TRANCOUNT > 1 ROLLBACK
            
            SET @ticketID = 0
            SET @resultCode = 1
            SET @resultMessage = 'ticketID ' + CAST(@ticketID AS NVARCHAR(200)) + ': ' + @ticketNumber + ' created error.  ' + char(13) + char(10)
                        + ERROR_NUMBER() + '. '  + char(13) + char(10)
                        + ERROR_MESSAGE() + '. ' + char(13) + char(10)
                        + ERROR_LINE() + '. ' + char(13) + char(10)
                        + ERROR_PROCEDURE() + '. ' + char(13) + char(10)
                        + ERROR_STATE() + '. ' + char(13) + char(10)
 
      END CATCH
 
  COMMIT TRAN
END

Open in new window

0
rob_farleyCommented:
I think you just need to be able to grab the identity value from the first INSERT, so that you can pass that into the second one.

Try using the OUTPUT clause. It's very much your friend. I've posted about it here: http://msmvps.com/blogs/robfarley/archive/2008/10/04/output-clause-knowing-what-goes-in-and-what-you-ve-accidentally-taken-out.aspx

Rob


0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

mathieu_cuprykAuthor Commented:
Ane, is the first stored procedure returning the ticketid?
0
mathieu_cuprykAuthor Commented:
Guys does the above return the ticketid to the next stored produre. I appreciate your help guys.
0
Aneesh RetnakaranDatabase AdministratorCommented:
Did you check my second post ?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mathieu_cuprykAuthor Commented:
Sorry, about the delay I got sick today. I will check it out right away.
Thanks.
0
mathieu_cuprykAuthor Commented:
Awesome work. I have to add now just another store INSERT INTO
I appreciate your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.