troubleshooting Question

Get Next number from Stored Procedure

Avatar of anthonytr
anthonytrFlag for United Kingdom of Great Britain and Northern Ireland asked on
Microsoft SQL ServerVBASQL
8 Comments1 Solution142 ViewsLast Modified:
Hi,

I am working on the following Store Procedure in MSSQL to INSERT, UPDATE, SELECT & DELETE from a table.  I currently have 2 stored procedures which I think I could merge into one, but can't see how to go about it.  To make things simpler and concise, how would I add the next WO number (which I generate in the second stored procedure) into the first Stored Procedure and make it available as an OUTPUT variable to pass back to my form?  

ALTER PROCEDURE [dbo].[p_CreateWO]

(

    @WOID         INTEGER,
	@CustomerID  INTEGER,
	@DeptID	INTEGER,
	@SiteID INTEGER,
	@WODate DATE,
	@WODueDate DATE,
	@WONumber nvarchar(255),
	@WORequestedByID INTEGER,
	@WOPreparedByID INTEGER,
	@WONotes nvarchar(MAX),
	@WOTotalVAT INTEGER,
	@WOTotalPrice INTEGER,
	@WOSignedForID INTEGER,
	@WODeliveryDate Date,
	@WOCompletedDate Date,
	@StatementType nvarchar(20) = '' 
  )

 

AS

BEGIN

IF @StatementType =  'Insert'

BEGIN

insert into tblWorkOrderHead (CustomerID, DeptID, SiteID, WODate, WODueDate, WONumber, WORequestedByID, WOPreparedByID, WONotes, WOTotalVAT, WOTotalPrice, WOSignedForID, WODeliveryDate, WOCompletedDate) values( @CustomerID, @DeptID, @SiteID, @WODate, @WODueDate, @NextWO, @WORequestedByID, @WOPreparedByID, @WONotes, @WOTotalVAT, @WOTotalPrice, @WOSignedForID, @WODeliveryDate, @WOCompletedDate)    

END 

 

IF @StatementType =  'Select' 

BEGIN

select * from tblWorkOrderHead WHERE WOID = @WOID

END  

 

IF @StatementType =  'Update' 

BEGIN

UPDATE tblWorkOrderHead SET

            DeptID = @DeptID, SiteID = @SiteID, WODate = @WODate, WODueDate = @WODueDate, WORequestedByID = @WORequestedByID, WOPreparedByID = @WOPreparedByID, WONotes = @WONotes, WOTotalVAT = @WOTotalVAT, WOTotalPrice = @WOTotalPrice, WOSignedForID = @WOSignedForID, WODeliveryDate = @WODeliveryDate, WOCompletedDate = @WOCompletedDate

      WHERE WOID = @WOID

END 

 

else IF @StatementType = 'Delete'

BEGIN

DELETE FROM tblWorkOrderHead WHERE WOID = @WOID

END

 end

My Stored Procedure to get the Next WO number is:

ALTER Procedure [dbo].[p_GetNewWONumber]

(@NextWO INT OUT
)
AS
BEGIN

SET NOCOUNT ON;

SELECT @NextWO = COALESCE(MAX(WONumber), 0) +1
FROM tblWorkOrderHead

RETURN @NextWO

END
ASKER CERTIFIED SOLUTION
Vitor Montalvão
IT Engineer
Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros