Quick Tip: Passing Variables to SQL Server

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

Wow, the week is almost over and I am yet to post something.

This week I was working on a small development project using Visual Basic for Applications (VBA) to execute some commands at the SQL Server level after some user interface events. The code to be executed needed to have values from the windows passed through to the Transact-SQL.  Now most people just write the code and pass through the value of a string field concatenated before and after with single quotes.

sqlstring = "select * from IV00101 where ITEMNMBR = '" & CStr(ItemNumber) & "'"

Continue reading

SQL Server Error: Cannot resolve the collation conflict

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

Today I have a post on the solution to a tricky SQL issue.

When working with SQL Server and creating a join that links tables in two databases, all goes well when the two databases have the same collation. However, if the databases have different collations the query it will fail in a writhing blubbery gelatinous slimy mess of confused character mappings, or at least return something similar to the following error:

Msg 468, Level 16, State 9, Line 4
Cannot resolve the collation conflict between “Latin1_General_CI_AS_KS_WS” and “Latin1_General_CI_AS” in the equal to operation.

Continue reading

Granting Access and Binding Defaults when recreating SQL Tables

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

Added to the Useful SQL Scripts Series.

This is a follow on post from my previous article: Backing up and Restoring data when recreating SQL Tables which explains how to recreate tables (usually to fix table structure issues) while making sure that the data is preserved.

Continue reading

Do not use the DEX_ROW_ID table column in your Customizations and Integrations

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

Today, I responded on a post on the Partner Forum asking “Can the DEX_ROW_ID change for a record over time?“. In this case the partner had seen the values for DEX_ROW_ID change on the GL20000 (GL_YTD_TRX_OPEN) and GL30000 (GL_Account_TRX_HIST) tables. This caused problems for an integration to an external system that used the DEX_ROW_ID as a key field.

Continue reading

Quick Tip: Using SQL to return the number of records in a Table

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

Now I am sure there are people out there that will be thinking that this is a silly topic for a blog article. Surely everyone knows how to get the number of records from a SQL table using the COUNT(*) function?

Continue reading

Quick Tip: Making sure Activity Tracking is enabled for all users

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

This Quick Tip comes to you courtesy of an idea from my friend, Robert Cavill.

On a Microsoft Dynamics GP system where Activity Tracking is being used, it needs to be activated for each User ID/Company ID that you want it enabled for. In most cases, you would want the Activity Tracking enabled for all valid User ID and Company ID combinations.

Continue reading

The Importance of Selecting the Correct Extender Key Fields

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

I have had a couple of recent cases which highlighted the importance of selecting the correct Key fields when creating Extender objects. Both of the cases involved adding additional user defined fields to an existing Microsoft Dynamics GP window using an Extender Window Object. In both cases, the incorrect fields were selected in the Key Fields section of the Extender Windows setup window which caused undesired behaviour.

Continue reading

How to read a Dexterity Script Profile to solve Performance Issues

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

Today, I thought I would spend a little time explain how to read a Dexterity Script Profile to resolve a performance issue. To demonstrate this I will use a support case I had where the customer was intermittent long delays when clicking on the checkbox on the Apply Sales Document window.

Continue reading

Dynamics Report Writer is the Best Report Writer in the World cont.

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

This is a follow up post to my earlier post, which demonstrated how Report Writer when combined with the power of Visual Basic for Applications (VBA) was able to create a customisation that would not have been possible with Report Writer alone.

Continue reading

Login Performance Issue for Microsoft Dynamics GP 10.0 SP5 and GP 2010 SP1

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

It has been brought to my notice that we have another issue with a dictionary adding records to the syMenuMstr (SY07110) table each time a user logs into Microsoft Dynamics GP.  This issue is caused by the new Dynamics Online Services dictionary which was installed as part of Microsoft Dynamics GP 10.0 Service Pack 5 and Microsoft Dynamics GP 2010 Service Pack 1.  This is similar to the Microsoft Dynamics GP 10.0 Service Pack 5 Login Performance issue found previously.

Continue reading

Backing up and Restoring data when recreating SQL Tables

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

Added to the Useful SQL Scripts Series.

There are times when you need to recreate a table to change its structure but don’t want to lose the data stored in the table.  I have had this situation a number of times when working on upgrade support cases where (for some unknown reason) a table does not have the correct table structure to allow the Dexterity Utilities to upgrade it.

Disclaimer: I know that later versions of the Professional Services Tools Library (PSTL) does support recreating a table while maintaining the data.  However, you might not have PSTL installed and the script in this post is simple to use.

The script makes a backup of the current table to a new table of the same name with the suffix BAK.  Once the backup is created, you can use the SQL Maintenance (File >> Maintenance >> SQL) window in Microsoft Dynamics GP to drop and create the table and its Auto Stored Procedures.

Note: Please see the post Granting Access and Binding Defaults when recreating SQL Tables for more information on creating tables (especially when related to upgrades).

Once the table has been recreated, you can run the rest of the script to copy the contents of the backup table into the newly recreated table.  Then the final step is to remove the backup table.

Note: Please make sure you have a current backup of the database before using the code in this article.

T-SQL Script Code

-- Written by David Musgrave of Winthrop Development Consultants

-- Last Modified: 12-Nov-2025

/* This Script is designed to be executed in sections */
/* Please read the instructions included as comments  */
/* Use Find and Replace to change the table name      */
/* Highlight each Section and click Execute or F5     */


/* 1 - Make a backup of the table so the data is saved */
-- Make backup of table
select * into TEMP_GL00105 from GL00105 
select count(*) from TEMP_GL00105 
/* 1 - End of Section ................................ */


/* 2 - Optional for testing: Remove data from original table */
-- Test code to remove data
select count(*) from GL00105
delete from GL00105
select count(*) from GL00105
/* 2 - End of Section ...................................... */


/* 3 - Drop and recreate original table using correct structure */
-- Recreate table at this stage using SQL Maintenance or T-SQL
-- This will leave a blank table of the correct structure

/* 3 - End of Section ......................................... */


/* 4 - Re-populate original table from the previously backed up table */
-- Declare variable for SQL 2005/2008
declare @srcfieldlst varchar(max)
declare @dstfieldlst varchar(max)
declare @dstvalues varchar(max)
-- Declare variable for SQL 2000
-- declare @fieldlst varchar(8000)
-- declare @newfieldlst varchar(8000)
-- declare @dstvalues varchar(8000)

-- Insert data from backup table
set @srcfieldlst = ''
select @srcfieldlst = @srcfieldlst + case when len(@srcfieldlst) > 0 then ', ' else '' end + '[' + dst.cn + ']' 
from ( 
	select o.name tn , c.name cn , c.colid from sysobjects o join syscolumns c on o.id = c.id 
	where o.name = 'TEMP_GL00105'  
	and c.name != 'DEX_ROW_ID'  
--	and c.name != 'DEX_ROW_TS'   
) src
join ( 
	select o.name tn, c.name cn , c.colid from sysobjects o join syscolumns c on o.id = c.id 
	where o.name = 'GL00105'  
	and c.name != 'DEX_ROW_ID'  
--	and c.name != 'DEX_ROW_TS'   
) dst
on src.cn = dst.cn
order by src.colid 

set @dstfieldlst = @srcfieldlst
set @dstvalues = ''
select @dstfieldlst = @dstfieldlst + case when len(@dstfieldlst) > 0 then ', ' else '' end + '[' + dst.cn + ']' 
	 , @dstvalues = @dstvalues + ', ' + dst.emptyvalue
from ( 
	select o.name tn , c.name cn , c.colid from sysobjects o join syscolumns c on o.id = c.id 
	where o.name = 'TEMP_GL00105'  
	and c.name != 'DEX_ROW_ID'  
--	and c.name != 'DEX_ROW_TS'   
) src
right outer join ( 
	select o.name tn, c.name cn , c.colid, c.xtype, t.name,
	case 
		when t.name in ('tinyint','smallint', 'int', 'bigint','bit','uniqueidentifier') then '''' + '0' + '''' 
		when t.name in ('numeric', 'real','decimal','float','money') then '''' + '0.0' + '''' 
		when t.name in ('char', 'varchar','nchar','nvarchar','text','ntext','sysname') then '''' + '' + '''' 
		when t.name in ('binary','varbinary','image') then 'cast(' + '0x0' + ' as ' + t.name + ')'
		when t.name in ('datetime','smalldatetime','datetime2','timestamp') then '''' + '1900-01-01 00:00:00.000' + '''' 
		when t.name in ('date') then '''' + '1900-01-01' + '''' 
		when t.name in ('time') then '''' + '00:00:00.000' + '''' 
		else '''' + '' + '''' 
	end emptyvalue
	from sysobjects o join syscolumns c on o.id = c.id join systypes t on c.xtype = t.xtype and c.xusertype = t.xusertype
	where o.name = 'GL00105'  
	and c.name != 'DEX_ROW_ID'  
--	and c.name != 'DEX_ROW_TS'   
) dst
on src.cn = dst.cn
where src.tn is null
order by src.colid 
 
--print @dstfieldlst
--print @srcfieldlst + @dstvalues

exec ( 'insert GL00105 ( ' + @dstfieldlst + ' ) ' + 'select ' + @srcfieldlst + @dstvalues + ' from TEMP_GL00105 ' ) 
select count(*) from GL00105
/* 4 - End of Section ............................................... */


/* 5 - After verifying that the recreated table has the correct data */
-- Remove Backup Table
drop table TEMP_GL00105 
/* 5 - End of Section .............................................. */



select * from GL00105

Note: The declaration of the varchar for Microsoft SQL Server 2000 cannot use the “max” syntax.

To use this script (also attached at the bottom of this article), please highlight the portions you need to execute and press F5.  Only run the next section of code once you are satisfied that the previous code has executed successfully.

Here is the script:

Hope you find this script useful.

David

11-Feb-2011: Added comments to better explain how script works.
06-Dec-2011: Added link to Granting Access and Binding Defaults when recreating SQL Tables post.
12-Mar-2019: Updated with latest code.
15-Apr-2025: Updated to fix varchar conversion error for added binary columns.
12-Nov-2025: Updated to fix issue with duplicate column error when custom column types are used. Thanks to Matt Connor for finding this issue and providing the solution.

 

Working with Temporary Tables in Dexterity

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

Dexterity developers often need to use temporary tables to perform tasks, such as displaying data in a different order, consolidating or totalling data, to store data while it is being edited, or to combine data from tables so it can be sorted using an index using fields from multiple tables.

Continue reading

How Disk Cleanup and Windows Update broke Microsoft Dynamics GP

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

Yesterday, I had a really interesting escalated case for a “Site Down” situation.

The story was that after installing the regular Windows Updates for Windows Server 2003 on their SQL Server machine, the customer rebooted to complete the update installation. When the server came back online, no-one could log into Microsoft Dynamics GP anymore.

Continue reading

Stored Procedure Executing In Wrong Database

Patrick Roth - Click for blog homepageThis is a reposting of an article Patrick Roth originally wrote on my Developing for Dynamics GP blog.

We ran into an odd issue recently that I’d like to share today.

An ISV reported that in their customer was getting an error when the ISV application attempted to execute a stored procedure.

[SQL Server] Could not find stored procedure DYNAMICS.dbo.SomeStoredProcedure

Easy you say – the proc doesn’t exist.  Just create it and you’ll be fine.

Continue reading

Microsoft Dynamics GP 10.0 Service Pack 5 Login Performance issue

David Meego - Click for blog homepageThis is a reposting of an article I originally wrote on my Developing for Dynamics GP blog.

It has come to our notice that there is an issue where the Analytical Accounting dictionary for Microsoft Dynamics GP 10.0 Service Pack 5 is adding 4 records to the syMenuMstr table every time the Microsoft Dynamics GP application is launched.

Continue reading

Using SQL DB Update Trigger With Text Fields

Patrick Roth - Click for blog homepageThis is a reposting of an article Patrick Roth originally wrote on my Developing for Dynamics GP blog.

An ISV had a goal of creating an audit table of the Dynamics record notes table – SY03900 – but was running into an issue.

He mentioned that he was using an “instead of update” SQL trigger and not just a normal “update” trigger.  The specific issue was that while his audit table was being updated, the SY03900 table was not.

After discussing the goals of his application and the issue, I set out to discover why exactly the normal update trigger wouldn’t work, why the SY03900 table wasn’t being updated, and lastly what we could do about it.

Continue reading