taSopGetMasterNumber

Today I have a client that wants me to provide an incremented SOP Master Number. In the past, I've just written my own code to increment the SOP40100.NXTMSTNO field.

That's always seemed heavy handed to me, and I knew that there must be a native function to do it... so I went looking.

This article is a code example for how to call the taSopGetMasterNumber stored procedure and get an incremented number.   

Note:

As soon as I got this all documented, I went to install it at the client. Who didn't have eConnect. So... I had to revert to the stored proc method. I've included the full text of that proc at the bottom of the article.

This article will be listed in the SQL - Scripts specific to Dynamics menu

DECLARE @RC int
DECLARE @O_iOUTMasterNumber int
DECLARE @O_iErrorState int
  
SELECT @O_iOUTMasterNumber =''
@O_iErrorState =0
  
EXECUTE @RC = [dbo].[taSopGetMasterNumber] 
   @O_iOUTMasterNumber OUTPUT
  ,@O_iErrorState OUTPUT
  
SELECT @O_iOUTMasterNumber ,
@O_iErrorState

If eConnect is not installed, here's our own implementation

IF EXISTS (SELECT name 
       FROM   sysobjects 
       WHERE  name = N'DD_SOPGetNextMaster' 
       AND    type = 'P')
    DROP PROCEDURE DD_SOPGetNextMaster
GO
  
CREATE PROCEDURE DD_SOPGetNextMaster 
  
  
@intMaster int output
  
AS
  
declare @bOkToExit int
  
--initialize
set @bOkToExit = 0
      
begin tran
    while @bOkToExit = 0 begin
        set @bOkToExit = 1 --assume success
  
        --get the next master number    
        select @intMaster = nxtmstno
            from sop40100 with (nolock)
          
        --increment the next master number
        update sop40100 set nxtmstno = @intMaster + 1
      
        --see if the number that we're going to use is in use
        --if it is, loop around again
        if exists (select mstrnumb 
                        from sop10100 with (nolock)
                        where mstrnumb = @intMaster)
            set @bOkToExit = 0
  
        if exists (select mstrnumb 
                        from sop30200 with (nolock)
                        where mstrnumb = @intMaster)
            set @bOkToExit = 0
  
        if exists (select intMasterNumber  
                        from ngboed..masterorders with (nolock)
                        where intMasterNumber = @intMaster)
            set @bOkToExit = 0
  
    end
commit tran
  
  
  
  
GO
  
grant all on DD_SOPGetNextMaster to public
  
  
      
--select nxtmstno,* from sop40100
-- update sop40100 set nxtmstno = 139000
  
--select intmasternumber from ngboed..masterorders where vchrcompanyidgp = 'stone' order by 1 desc

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences