How To Bulk Update Segment Descriptions From A CSV

Microsoft Dynamics GPOn the Microsoft Dynamics GP Community site a question from Lisa Sorenson in October last year asking if it was possible to use the Table Import (Microsoft Dynamics GP menu ¯ Tools ¯ Integrate ¯ Table Import) feature in Microsoft Dynamics GP to update some Segment descriptions has risen to the top with Steve Cummings linking to a post where the suggestion is to use a CSV file and Word template to generate a mailmerge.

This solution will work, but can be accomplished in much less time and effort by using the SQL command BULK INSERT to load the CSV (formatted as Segment ID, Segment Number and Description), in the below SQL change the highlighted path to the location of your CSV file, into a temporary table and update Account Segment Master (GL40200) from the temp table (which is removed at the end of the script);


CREATE TABLE #SegmentDescriptions
   (SGMTNUMB VARCHAR(100)
   ,SGMNTID VARCHAR(100)
   ,DSCRIPTN VARCHAR(100))
GO

BULK INSERT
   #SegmentDescriptions
FROM
   'c:\temp\Segments\Segments.csv'
WITH
   (FIELDTERMINATOR = ','
   ,ROWTERMINATOR = '\n')
GO

UPDATE
   Segments
SET
   Segments.SGMTNUMB = NewSegments.SGMTNUMB
   ,Segments.SGMNTID = NewSegments.SGMNTID
   ,Segments.DSCRIPTN = Left(NewSegments.DSCRIPTN, 31)
FROM
   GL40200 AS Segments
INNER JOIN
   #SegmentDescriptions AS NewSegments ON NewSegments.SGMTNUMB = Segments.SGMTNUMB
      AND NewSegments.SGMNTID = Segments.SGMNTID

DROP TABLE #SegmentDescriptions

The above script is supplied as is with no warranty; if you use the script please ensure you have a backup of your company database before starting.

What should we write about next?

Your Name (required) -
Your Email (required) -

(Visited 1,687 times, 1 visits today)

5 thoughts on “How To Bulk Update Segment Descriptions From A CSV

  1. Lisa Sorensen says:

    Thanks so much for the script. It seems pretty straight forward but when I attempted to run it at a client site, I received the following message. It is during the process of running the update statement and setting the valies.

    Msg 8152, Level 16, State 14, Line 1
    String or binary data would be truncated.
    The statement has been terminated.

    Any idea what could be causing this and how to correct it? Thanks again for the information!

  2. Ian Grieve Ian Grieve says:

    Hi Lisa,

    I’ve just done some testing and it appears the error is caused by one (or more) of the descriptions being longer than the 31 characters allowed by the Segment Description Master table.

    I have updated the script above to only use the first 31 characters of the description from the CSV to prevent the error.

    Ian

Leave a Reply

Your email address will not be published. Required fields are marked *