Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8137335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:05:05+00:00 2026-06-06T11:05:05+00:00

I am writing a utility, C#, to import a large CSV with about 80

  • 0

I am writing a utility, C#, to import a large CSV with about 80 columns into a SQL Server 2008 table.

The CSV needs manipulation before import e.g. merging some columns, converting dates etc. I am therefore constructing an INSERT INTO statement like so:

INSERT INTO table VALUES ('abc','blah',...)

My problem is that the table contains a column of type UNIQUEINDENTIFIER. It’s a problem because I would like to re-use an existing method which takes a collection of values and returns the string of values to pop directly into my INSERT INTO statement with apostrophes dealt with, injection attacks munged out etc.

Is there a way of generating a valid literal for a UNIQUEIDENTIFIER that I can pass to this method? I tried Guid.NewGuid.ToString(), which gives me a literal like so 'bfdb0297-2018-4555-ac12-88064944a671', but my query then errors with a data truncation exception. I also tried with the hyphens removed. I expected 32 hex characters to work.

I know that a parameterised query is the correct way to go but that would mean a significant re-work of existing code.

I’m pretty good with C# and bluff-worthy with SQL Server.

[EDIT]

I’ve done several things. First, wrote a query which updates that column with Guid.NewGuid at it works. Therefore, the truncation error is not from there. Checking the SQL Server reference leads me to believe that a truncation error is not returned from a bad value in an id column, rather it’s an invalid value error. I’ve also taken into account Aaron’s comment that significant work is not an excuse for not doing it right and have created a method to generate a parameterised query which is now working fine.

Many thanks to all responders.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:05:07+00:00Added an answer on June 6, 2026 at 11:05 am

    Since this works fine:

    DECLARE @x TABLE(y UNIQUEIDENTIFIER); 
    
    INSERT @x SELECT 'bfdb0297-2018-4555-ac12-88064944a671';
    

    And this fails with the generic error message that you’re currently getting:

    DECLARE @x TABLE
    (
      y UNIQUEIDENTIFIER,
      z CHAR(10)
    ); 
    
    INSERT @x 
      SELECT 'bfdb0297-2018-4555-ac12-88064944a671',
             '0123456789A';
    

    Result:

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

    I have to question whether the error is really happening on the UNIQUEIDENTIFIER column. I suspect that either (a) the error is happening on a different column in the table, and you haven’t verified all of your string lengths, or (b) there is a trigger on the table and the error is happening there.

    If you use proper parameters instead of just throwing a string together, you can make sure that each parameter is defined exactly as it is in the table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Python utility that needs to parse a large, regularly-updated CSV
Is there any utility available to Import the table data from CSV file. Since
I'm writing a simple alarm utility in Python. #!/usr/bin/python import time import subprocess import
I am writing a generic sqldump utility that takes a DSN and a table
I'm writing an import utility that is using phone numbers as a unique key
I am writing a utility jar file to be shared into multiple project. Basically
I'm writing a utility program with C# in WPF that allows users to create
I am writing a utility that would allow me to monitor the health of
I'm writing a little utility to load Entity Data Models from an assembly and
I am writing a small command line utility. It should hopefully be able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.