SQLException : String or binary data would be truncated

前端 未结 12 912
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 06:41

I have a C# code which does lot of insert statements in a batch. While executing these statements, I got \"String or binary data would be truncated\" error and transaction r

12条回答
  •  再見小時候
    2020-11-27 07:28

    1. Get the query that is causing the problems (you can also use SQL Profiler if you dont have the source)
    2. Remove all WHERE clauses and other unimportant parts until you are basically just left with the SELECT and FROM parts
    3. Add WHERE 0 = 1 (this will select only table structure)
    4. Add INTO [MyTempTable] just before the FROM clause

    You should end up with something like

    SELECT
     Col1, Col2, ..., [ColN]
    INTO [MyTempTable]
    FROM
      [Tables etc.]
    WHERE 0 = 1
    

    This will create a table called MyTempTable in your DB that you can compare to your target table structure i.e. you can compare the columns on both tables to see where they differ. It is a bit of a workaround but it is the quickest method I have found.

提交回复
热议问题