Microsoft DynamicsFree generatorPDF, Excel, Word exportMicrosoft Dynamics GPAll Microsoft Dynamics GP tools

Dynamics GP Restore Live to Test Company Script Generator (KB 871973)

This generator writes the full T-SQL sequence to refresh a Dynamics GP test company from live: copy-only backup, restore with MOVE over the test database, the Microsoft KB 871973 COMPANYID and INTERID update, the DYNSA owner fix and the post-restore steps. Fill in your database names and paths and run it block by block.

Fill in

Confirm with RESTORE FILELISTONLY.

Generated code
Download:

0. Before you start

-- 1. The test company TEST already exists, created in GP Utilities with its own company ID.
-- 2. All users are out of GP; Management Reporter services are stopped if MR is used.
-- 3. If Analytical Accounting is active in TWO, activate AA in TEST before restoring.
-- 4. Run every statement below on the test side only. Never run block 4 or later in TWO.

1. Back up live (copy-only, does not break the backup chain)

BACKUP DATABASE [TWO]
TO DISK = N'D:\SQLBackup\TWO_copy.bak'
WITH COPY_ONLY, INIT, COMPRESSION, STATS = 10;
-- remove COMPRESSION if your SQL Server edition does not support it

2. Check logical file names

RESTORE FILELISTONLY FROM DISK = N'D:\SQLBackup\TWO_copy.bak';
-- LogicalName column: use these values in the MOVE clauses below; do not rename them

3. Restore over the test database

USE master;
ALTER DATABASE [TEST] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

RESTORE DATABASE [TEST]
FROM DISK = N'D:\SQLBackup\TWO_copy.bak'
WITH MOVE N'GPSTWODat.mdf' TO N'D:\SQLData\GPSTESTDat.mdf',
     MOVE N'GPSTWOLog.ldf' TO N'L:\SQLLogs\GPSTESTLog.ldf',
     REPLACE, STATS = 10;

ALTER DATABASE [TEST] SET MULTI_USER;
ALTER DATABASE [TEST] SET RECOVERY SIMPLE;  -- optional for test

4. Update COMPANYID and INTERID (Microsoft KB 871973 logic, run in the test database)

USE [TEST];
GO
declare @cStatement varchar(255)
declare G_cursor CURSOR for
select case when UPPER(a.COLUMN_NAME) in ('COMPANYID','CMPANYID')
  then 'update '+a.TABLE_NAME+' set '+a.COLUMN_NAME+' = '+ cast(b.CMPANYID as char(3))
  else 'update '+a.TABLE_NAME+' set '+a.COLUMN_NAME+' = '''+ db_name()+'''' end
from INFORMATION_SCHEMA.COLUMNS a, DYNAMICS.dbo.SY01500 b
  where UPPER(a.COLUMN_NAME) in ('COMPANYID','CMPANYID','INTERID','DB_NAME','DBNAME')
    and b.INTERID = db_name() and COLUMN_DEFAULT is not null
    and rtrim(a.TABLE_NAME)+'-'+rtrim(a.COLUMN_NAME) <> 'SY00100-DBNAME'
  order by a.TABLE_NAME
set nocount on
OPEN G_cursor
FETCH NEXT FROM G_cursor INTO @cStatement
WHILE (@@FETCH_STATUS <> -1)
begin
  exec (@cStatement)
  FETCH NEXT FROM G_cursor INTO @cStatement
end
close G_cursor
DEALLOCATE G_cursor
set nocount off
GO
-- if a duplicate key error names PKxxxxx, fix INTERID/COMPANYID in table xxxxx manually

5. Human Resources attendance setup (only if HR is used)

UPDATE TAST0130 SET COMPANYCODE_I = 'TEST';
-- or re-enter Tools > Setup > Human Resources > Attendance > Setup in the test company and save

6. Database owner

USE [TEST];
EXEC sp_changedbowner 'DYNSA';
-- equivalent on newer SQL Server versions:
-- ALTER AUTHORIZATION ON DATABASE::[TEST] TO DYNSA;

7. After the restore

-- Analytical Accounting: run the AAG00102 next-number fix from the Microsoft-linked AA test company article before entering transactions.
-- Fixed Assets: re-enter Fixed Assets Company Setup in the test company.
-- SSRS / Excel report drilldowns: run GP Database Maintenance, Functions and Stored Procedures plus Views, for all databases.
-- Workflow and email: change approver emails and SMTP settings so test does not mail real users.
-- Integrations, eConnect jobs, scheduled SmartConnect or SQL Agent jobs: point them at TEST or disable them.
-- Optional: rename the company display name in GP so users can see they are in test.

Block 4 follows the script in Microsoft's 'Set up a test company' article (KB 871973) with the system database name taken from your input; if your GP version's article differs, copy Microsoft's current script. The SY00100-DBNAME exclusion keeps the system database name intact. Confirm logical file names with block 2 before running block 3. Take a fresh backup of the test database first if it holds work you need.

Want the test refresh automated?

Send us your GP version, modules and SQL layout, and we will review the script and suggest a scheduled refresh job.

Optional. The tool and every export work without this. No spam.

Was this tool useful?

How to use it

Create the test company once in GP Utilities and grant users access; after that you only repeat the refresh. Enter the live and test database names, the system database name, a backup path the SQL Server service can write to, and the physical paths of the existing test .mdf and .ldf files (from the test database's Files page). Run block 2 first to read the logical names, correct the fields if needed, then run blocks 1, 3, 4 and 6 in order. Log in to the test company and check a few documents before letting users in.

  • COPY_ONLY keeps your live backup chain intact
  • MOVE paths must point at the test files, not live
  • Block 4 must run in the test database
  • Keep the logical file names from live

Why the INTERID script is needed

A company database stores its own company ID and database name in many tables (COMPANYID, CMPANYID, INTERID, DB_NAME, DBNAME columns). After restoring live over test, those columns still say live. GP then reads test data with live identifiers, which shows up as wrong company names, posting errors or errors opening windows. The script reads the correct company ID from SY01500 in the system database for the current database name and updates every such column, except SY00100.DBNAME, which holds the system database name and must not change.

Worked example

Live TWO, test TEST, system DYNAMICS. Backup to D:\SQLBackup\TWO_copy.bak. RESTORE FILELISTONLY shows logical names GPSTWODat.mdf and GPSTWOLog.ldf. The restore moves them onto D:\SQLData\GPSTESTDat.mdf and L:\SQLLogs\GPSTESTLog.ldf with REPLACE. In TEST, the cursor sets every COMPANYID or CMPANYID column to the TEST company ID from DYNAMICS..SY01500 and every INTERID, DB_NAME or DBNAME column (except SY00100.DBNAME) to 'TEST'. sp_changedbowner sets the owner to DYNSA. Users then log in to TEST and see live data as of the backup.

Gotchas

Restoring over the wrong database is the real risk: double-check test_db before running block 3, and never point MOVE at live files. Analytical Accounting must already be active in test before the restore, and the AAG00102 next numbers need the separate fix or new transactions hit duplicate keys. Fixed Assets company setup and HR attendance setup (TAST0130) need re-entry. Integrations, workflow emails and scheduled jobs copied in data can act on real customers from test, so disable them. Management Reporter needs its services stopped during the refresh.

Frequently Asked Questions

How do I copy live data into a Dynamics GP test company?

Create the test company in GP Utilities, back up the live database, restore that backup over the test database with MOVE to the test files and REPLACE, then run Microsoft's INTERID update script in the test database and set the owner to DYNSA. This generator writes each step with your names.

What does the KB 871973 script do?

It loops over every column named COMPANYID, CMPANYID, INTERID, DB_NAME or DBNAME in the test database and updates it to the test company's ID and database name taken from SY01500 in the system database. Without it, test keeps live identifiers and GP behaves incorrectly.

Do I have to recreate the test company every time?

No. Create it once in GP Utilities so it exists in SY01500 with its own ID and has user access. Each refresh is just backup, restore over the same database, the INTERID script and the post steps. Re-grant security only if you changed users since.

What breaks after restoring live to test?

Usually HR attendance setup (TAST0130), Fixed Assets company setup, Analytical Accounting next numbers in AAG00102, SSRS drilldown links and any integration or email that still points at live users. Work through block 7 of the output before letting users test.

If you want an AI assistant that answers GP questions from a safe test or reporting copy on your own servers, we can set that up and keep it refreshed.

ArchRay builder
Design the on-prem AI stack for Microsoft Dynamics GP: model, GPUs, RAG pipeline, guardrails
See memory, speed and cost as you swap parts, then export the diagram. Free, in your browser.
Open the builder

Keep exploring

Free tools

ERP fixes and how-tos

AI on your ERP

Integrations