Opening and Closing Periods with SQL

***never use any script found on this site without testing. All scripts are provided as-is, and without warranty of any kind.

 

I currently manage 54 production GP databases, and if I have to do something manually more than once or twice, I figure there has to be a way to automate it.

Our fiscal periods are plain vanilla - 12 monthly periods beginning on the first day of the month, however our GP users are all over the globe, so new periods must be opened automatically to 'reduce the noise'.

Attached are 3 scripts:

  1. usp_OpenCurrentPeriod - opens all 6 standard GP modules (modify as needed - notes are in the script) for the current calendar month. I created a SQL job that runs a few minutes after midnight UTC time on the first of the month to handle this for many of our companies;
  2. usp_OpenCurrentPeriodGLOnly - opens only the GP GL module (again - modify as needed - notes are in the script) for the current calendar month. This script is used for companies that only handle consolidation or other GL entries - this is also part of the SQL job noted above;
  3. usp_ClosePreviousPeriod - CLOSES all 6 standard GP modules (modify as needed - notes are in the script) for the previous calendar month. I do NOT use a SQL job for this as not all GP companies close at the same time. As written the script looks for the start date of the last calendar month and closes that period - so if I ran it today, it would close all modules for the period that started 1 Jan 2021.

A few other comments:

In my WHERE clause I am converting GETDATE() to a date format otherwise GP won't recognize it (you can also use a varchar(10)). 

The table I'm updating is [companydb].dbo.SY40100 - this contains a combination of 'header' records (one for each period) WHERE SERIES = 0. I'm setting the PSERIES_1 through PSERIES_6 to 0 to open the period for each module, and to close the period I set it to a 1.

Because I'm manually setting the header record, I also set the underlying 'detail' records, which represent the items you see in the 'Mass Close' window from the Fiscal Periods window. In 25 years working with GP I've never seen anyone use this window, but we should still clean up after ourselves... These series have a different value that the PSERIES!

-- Header records (These are columns)
PSERIES_1 = Financial

PSERIES_2 = Sales
PSERIES_3 = Purchasing
PSERIES_4 = Inventory
PSERIES_5 = Payroll
PSERIES_6 = Project

-- SERIES values - for open/close by window
(There is no series 1)
Series - 2 = Financial
Series - 3 = Sales
Series - 4 = Purchasing
Series - 5 = Inventory
Series - 6 = Payroll
No Project windows available in this column

I am using this in two different environments (GP 2016 R2 and GP 2013) as we still have one company to migrate to GP 2016. In a couple of months we'll start testing the current version of GP for a late summer upgrade...

Lastly, there are plenty of ways to calculate dates in SQL - hopefully your period begin dates are either static like a constant day of the month.

Please please please test this this for your environment!

Related Articles

... and you 'll find more on the SQL (Dynamics) Menu

/****** Object:  StoredProcedure [dbo].[usp_ClosePreviousPeriod]    Script Date: 2/23/2021 6:45:07 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
/*
 ---------------------------------------------------------------------------------
 COPYRIGHT (C) 2019 Jeffrey Pfershy
 
   This script is provided as-is without warranty, express or implied.
   As with any code, you are responsible for testing this before using it
     in a production environment.
   This procedure has not been tested with any third party products such as
     Binary Stream Multi-Entity Management (MEM) or others.
   If you have questions I can be reached at jpfershy@hotmail.com
 ---------------------------------------------------------------------------------
 usp_ClosePreviousPeriod STORED PROCEDURE CREATION SCRIPT
 ---------------------------------------------------------------------------------
 
 Revision History
 
 Date          Who    Description
 -----------   ----   --------------------------------------------------------
 12 Jun 2019   JAP    Initial creator
 ---------------------------------------------------------------------------------
 
 Stored Procedure Name:  usp_ClosePreviousPeriod
  
 Purpose: To be used to close (mark closed) the previous fiscal period
            for all modules in a GP database (company/entity).
 
 Use: This stored proc was written to CLOSE ALL GP modules for the prior fiscal period,
        which in our environment is the FIRST DAY OF THE MONTH.
      
      **YOU WILL NEED TO MODIFY THIS CODE IF YOUR FISCAL PERIODS DO NOT BEGIN ON THE FIRST DAY OF THE MONTH**
       
      **The field PERIODDT is the Fiscal Period START Date**
 
      I run this manually as needed once finance notifies me which companies can be closed.
         
      Create this stored procedure in every GP database you want to use it in,
        and add other logic as needed to match your fiscal periods.
 
 
 
PSERIES_1 = Financial
PSERIES_2 = Sales
PSERIES_3 = Purchasing
PSERIES_4 = Inventory
PSERIES_5 = Payroll
PSERIES_6 = Project
 
-- Series - for open/close by window
Series - 2 = Financial
Series - 3 = Sales
Series - 4 = Purchasing
Series - 5 = Inventory
Series - 6 = Payroll
No Project windows available in this column
 
 ---------------------------------------------------------------------------------
 
 EXEC dbo.usp_ClosePreviousPeriod
*/
  
ALTER PROCEDURE [dbo].[usp_ClosePreviousPeriod]
 
AS
 
SET NOCOUNT ON
 
BEGIN TRAN
 
UPDATE SY40100
SET PSERIES_1 = 1, PSERIES_2 = 1, PSERIES_3 = 1, PSERIES_4 = 1, PSERIES_5 = 1, PSERIES_6 = 1
WHERE PERIODDT = CONVERT(date,DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE())-1,0),120)
AND SERIES = 0
 
UPDATE SY40100
SET CLOSED = 1
WHERE PERIODDT = CONVERT(date,DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE())-1,0),120)
AND SERIES IN (2,3,4,5,6)
 
COMMIT TRAN;

 

/****** Object:  StoredProcedure [dbo].[usp_OpenCurrentPeriodGLOnly]    Script Date: 9/26/2020 3:18:46 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
/*
 ---------------------------------------------------------------------------------
 COPYRIGHT (C) 2020 Jeffrey Pfershy
 
   This script is provided as-is without warranty, express or implied.
   As with any code, you are responsible for testing this before using it
     in a production environment.
   This procedure has not been tested with any third party products such as
     Binary Stream Multi-Entity Management (MEM) or others.
   If you have questions I can be reached at jpfershy@hotmail.com
 ---------------------------------------------------------------------------------
 usp_OpenCurrentPeriodGLOnly STORED PROCEDURE CREATION SCRIPT
 ---------------------------------------------------------------------------------
 
 Revision History
 
 Date          Who    Description
 -----------   ----   --------------------------------------------------------
 29 Jan 2020   JAP    Initial creator
 ---------------------------------------------------------------------------------
 
 Stored Procedure Name:  usp_OpenCurrentPeriodGLOnly
  
 Purpose: To be used to open (unmark closed) the current fiscal period
            for GL only in a GP database (company).
 
 Use: This stored proc was written to open the GL module for the new fiscal period,
        which in our environment is the first of the month.
        I have a SQL job that runs on the first of every month and executes either
        this sp or usp_OpenCurrentPeriod (for all modules),
        depending on if my GP company is a 'GL only' company or not.
         
      If your fiscal periods fall on other days use the lines that are commented
        out below instead
         
      Create this stored procedure in every GP database you want to use it in,
        then set up a SQL job to run it either on the day of the month that your new
        fiscal period starts, or add other logic to run it daily and if that date
        is the start of the new fiscal period, run the update statement.
      
      **YOU WILL NEED TO MODIFY THIS CODE IF YOUR FISCAL PERIODS DO NOT BEGIN ON THE FIRST DAY OF THE MONTH**
       
      **The field PERIODDT is the Fiscal Period START Date**
 
PSERIES_1 = Financial
PSERIES_2 = Sales
PSERIES_3 = Purchasing
PSERIES_4 = Inventory
PSERIES_5 = Payroll
PSERIES_6 = Project
 
-- Series - for open/close by window
Series - 2 = Financial
Series - 3 = Sales
Series - 4 = Purchasing
Series - 5 = Inventory
Series - 6 = Payroll
No Project windows available in this column
---------------------------------------------------------------------------------
Sample: EXEC dbo.usp_OpenCurrentPeriodGLOnly
 
*/
  
CREATE PROCEDURE [dbo].[usp_OpenCurrentPeriodGLOnly]
 
AS
 
SET NOCOUNT ON
 
BEGIN TRAN
 
UPDATE dbo.SY40100
SET PSERIES_1 = 0
WHERE PERIODDT = CONVERT(date,DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE()),0),120)  -- First day of the month
--WHERE PERIODDT = CONVERT(date,DATEADD(DAY, DATEDIFF(DAY,0,GETDATE()),0),120)    -- Today
 
UPDATE dbo.SY40100
SET CLOSED = 0
WHERE PERIODDT = CONVERT(date,DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE()),0),120)  -- First day of the month
--WHERE PERIODDT = CONVERT(date,DATEADD(DAY, DATEDIFF(DAY,0,GETDATE()),0),120)    -- Today
AND SERIES = 2
 
COMMIT TRAN;

 

/****** Object:  StoredProcedure [dbo].[usp_OpenCurrentPeriod]    Script Date: 9/26/2020 3:59:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
/*
 ---------------------------------------------------------------------------------
 COPYRIGHT (C) 2019 Jeffrey Pfershy
 
   This script is provided as-is without warranty, express or implied.
   As with any code, you are responsible for testing this before using it
     in a production environment.
   This procedure has not been tested with any third party products such as
     Binary Stream Multi-Entity Management (MEM) or others.
   If you have questions I can be reached at jpfershy@hotmail.com
 ---------------------------------------------------------------------------------
 usp_OpenCurrentPeriod STORED PROCEDURE CREATION SCRIPT
 ---------------------------------------------------------------------------------
 
 Revision History
 
 Date          Who    Description
 -----------   ----   --------------------------------------------------------
 12 Jun 2019   JAP    Initial creator
 ---------------------------------------------------------------------------------
 
 Stored Procedure Name:  usp_OpenCurrentPeriod
  
 Purpose: To be used to open (unmark closed) the current fiscal period
            for all modules (GL, AP, AR only) in a GP database (company).
 
 Use: This stored proc was written to open all standard GP modules for the new
        fiscal period, which in our environment is the first of the month.
        I have a SQL job that runs on the first of every month and executes either
        this sp or usp_OpenCurrentPeriodGLOnly (for GL only companies),
        depending on if my GP company is a 'GL only' company or not.
         
      If your fiscal periods fall on other days use the lines that are commented
        out below instead
         
      Create this stored procedure in every GP database you want to use it in,
        then set up a SQL job to run it either on the day of the month that your new
        fiscal period starts, or add other logic to run it daily and if that date
        is the start of the new fiscal period, run the update statement.
      
      **YOU WILL NEED TO MODIFY THIS CODE IF YOUR FISCAL PERIODS DO NOT BEGIN ON THE FIRST DAY OF THE MONTH**
       
      **The field PERIODDT is the Fiscal Period START Date**
 
PSERIES_1 = Financial
PSERIES_2 = Sales
PSERIES_3 = Purchasing
PSERIES_4 = Inventory
PSERIES_5 = Payroll
PSERIES_6 = Project
 
-- Series - for open/close by window
Series - 2 = Financial
Series - 3 = Sales
Series - 4 = Purchasing
Series - 5 = Inventory
Series - 6 = Payroll
No Project windows available in this column
---------------------------------------------------------------------------------
Sample: EXEC dbo.usp_OpenCurrentPeriod
 
*/
  
ALTER PROCEDURE [dbo].[usp_OpenCurrentPeriod]
 
AS
 
SET NOCOUNT ON
 
BEGIN TRAN
 
UPDATE dbo.SY40100
SET PSERIES_1 = 0, PSERIES_2 = 0, PSERIES_3 = 0, PSERIES_4 = 0, PSERIES_5 = 0,  PSERIES_6 = 0
WHERE PERIODDT = CONVERT(date,DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE()),0),120)  -- First day of the month
--WHERE PERIODDT = CONVERT(date,DATEADD(DAY, DATEDIFF(DAY,0,GETDATE()),0),120)    -- Today
 
UPDATE dbo.SY40100
SET CLOSED = 0
WHERE PERIODDT = CONVERT(date,DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE()),0),120)  -- First day of the month 
--WHERE PERIODDT = CONVERT(date,DATEADD(DAY, DATEDIFF(DAY,0,GETDATE()),0),120)    -- Today
AND SERIES IN (2,3,4,5,6)
 
COMMIT TRAN;
GO

 

 


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