How to ignore "String or binary data would be truncated"

  • Hello, I have a script to be used to backup a specific table in a weekly basis, here is the approach what I take, if you have a better recommendation, it would be appreciated if you can share it with me (and others reading this post):

    1. script the source table's schema to a create

    2. the new script:

    If not exists (select * from sysobjects where name='EventlogHistory' and xtype='U')

    CREATE TABLE [dbo].[EventlogHistory](

    [LogID] [int] IDENTITY(1,1) NOT NULL,

    [ProjectID] [int] NOT NULL,

    [Description] [nvarchar](max) NOT NULL,

    [EventType] [varchar](10) NOT NULL,

    127.0.0.1 [varchar](50) NOT NULL,

    [UserLogon] [varchar](30) NOT NULL,

    [CreatedOn] [datetime] NOT NULL,

    ArchivedOn datetime default getdate())

    insert into EventlogHistory

    It throws me the error message saying "String or binary data would be truncated" which is nonsense to me, I need to let sql ignore the error, it is ridiculous to do a max(len()) to find out as it should never happen, right?

    Thank you.

  • halifaxdal (1/16/2015)


    Hello, I have a script to be used to backup a specific table in a weekly basis, here is the approach what I take, if you have a better recommendation, it would be appreciated if you can share it with me (and others reading this post):

    1. script the source table's schema to a create

    2. the new script:

    If not exists (select * from sysobjects where name='EventlogHistory' and xtype='U')

    CREATE TABLE [dbo].[EventlogHistory](

    [LogID] [int] IDENTITY(1,1) NOT NULL,

    [ProjectID] [int] NOT NULL,

    [Description] [nvarchar](max) NOT NULL,

    [EventType] [varchar](10) NOT NULL,

    127.0.0.1 [varchar](50) NOT NULL,

    [UserLogon] [varchar](30) NOT NULL,

    [CreatedOn] [datetime] NOT NULL,

    ArchivedOn datetime default getdate())

    insert into EventlogHistory

    It throws me the error message saying "String or binary data would be truncated" which is nonsense to me, I need to let sql ignore the error, it is ridiculous to do a max(len()) to find out as it should never happen, right?

    Thank you.

    no way i know of to ignore errors, you have to explicitly use LEFT functions in the SELECT or something to work around this issue.

    based on your table, only columns [EventType] [varchar](10) or [UserLogon] [varchar](30) seem to stand out to me to be candidates for strings bigger than the destination.

    if you SELECT *

    INTO #temp

    From YourSourceTable, and compare the column sizes, can you see what is the issue?

    select

    colz.name As ColumnName,

    TYPE_NAME(colz.system_type_id) As DataType,

    CASE

    WHEN TYPE_NAME(colz.system_type_id) IN('nchar','nvarchar')

    THEN colz.max_length / 2

    WHEN TYPE_NAME(colz.system_type_id) IN('char','varchar')

    THEN colz.max_length

    ELSE NULL

    END,

    colz.column_id

    from tempdb.sys.columns colz

    where object_id=object_id('tempdb.dbo.#temp')

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • halifaxdal (1/16/2015)


    Hello, I have a script to be used to backup a specific table in a weekly basis, here is the approach what I take, if you have a better recommendation, it would be appreciated if you can share it with me (and others reading this post):

    1. script the source table's schema to a create

    2. the new script:

    If not exists (select * from sysobjects where name='EventlogHistory' and xtype='U')

    CREATE TABLE [dbo].[EventlogHistory](

    [LogID] [int] IDENTITY(1,1) NOT NULL,

    [ProjectID] [int] NOT NULL,

    [Description] [nvarchar](max) NOT NULL,

    [EventType] [varchar](10) NOT NULL,

    127.0.0.1 [varchar](50) NOT NULL,

    [UserLogon] [varchar](30) NOT NULL,

    [CreatedOn] [datetime] NOT NULL,

    ArchivedOn datetime default getdate())

    [font="Arial Black"]insert into EventlogHistory [/font]

    It throws me the error message saying "String or binary data would be truncated" which is nonsense to me, I need to let sql ignore the error, it is ridiculous to do a max(len()) to find out as it should never happen, right?

    Thank you.

    There seems to be a wee bit o' code missing in the area that I've embolded above.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (1/16/2015)


    halifaxdal (1/16/2015)


    Hello, I have a script to be used to backup a specific table in a weekly basis, here is the approach what I take, if you have a better recommendation, it would be appreciated if you can share it with me (and others reading this post):

    1. script the source table's schema to a create

    2. the new script:

    If not exists (select * from sysobjects where name='EventlogHistory' and xtype='U')

    CREATE TABLE [dbo].[EventlogHistory](

    [LogID] [int] IDENTITY(1,1) NOT NULL,

    [ProjectID] [int] NOT NULL,

    [Description] [nvarchar](max) NOT NULL,

    [EventType] [varchar](10) NOT NULL,

    127.0.0.1 [varchar](50) NOT NULL,

    [UserLogon] [varchar](30) NOT NULL,

    [CreatedOn] [datetime] NOT NULL,

    ArchivedOn datetime default getdate())

    [font="Arial Black"]insert into EventlogHistory [/font]

    It throws me the error message saying "String or binary data would be truncated" which is nonsense to me, I need to let sql ignore the error, it is ridiculous to do a max(len()) to find out as it should never happen, right?

    Thank you.

    There seems to be a wee bit o' code missing in the area that I've embolded above.

    You are right Jeff, [font="Arial Black"]select * from Eventlog[/font] is the missing part. Thanks for pointing that.

  • no way i know of to ignore errors, you have to explicitly use LEFT functions in the SELECT or something to work around this issue.

    based on your table, only columns [EventType] [varchar](10) or [UserLogon] [varchar](30) seem to stand out to me to be candidates for strings bigger than the destination.

    No, the two fields' max len returns as 6 and 18.

  • maybe this is not a good way to back up a specific table.

    can anyone share their experience on back up a specific table in a weekly basis (scheduled in a job)? Thanks.

    What I originally thought is to add a datetime (the timestamp of backup) in the backup table.

    The table I need to back up has 30+ fields of different types

  • halifaxdal (1/16/2015)


    no way i know of to ignore errors, you have to explicitly use LEFT functions in the SELECT or something to work around this issue.

    based on your table, only columns [EventType] [varchar](10) or [UserLogon] [varchar](30) seem to stand out to me to be candidates for strings bigger than the destination.

    No, the two fields' max len returns as 6 and 18.

    could the order of columns in the target table be differnet than the desitnaation table?

    ie if you expicitly name the insert column names as wella s the names of the SELECT columns?, are they in the same order as SELECT *?

    INSERT INTO EventlogHistory

    ([LogID],[ProjectID],[Description],[EventType],127.0.0.1,[UserLogon],[CreatedOn])

    SELECT [LogID],[ProjectID],[Description],[EventType],127.0.0.1,[UserLogon],[CreatedOn]

    FROM EventLog

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • halifaxdal (1/16/2015)


    no way i know of to ignore errors, you have to explicitly use LEFT functions in the SELECT or something to work around this issue.

    based on your table, only columns [EventType] [varchar](10) or [UserLogon] [varchar](30) seem to stand out to me to be candidates for strings bigger than the destination.

    No, the two fields' max len returns as 6 and 18.

    Its fine the way you have it. If your history table has exactly the same schema and datatypes as the source table, then there's something very strange going on that's going to require a deeper dive on your part.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Lowell (1/16/2015)


    halifaxdal (1/16/2015)


    no way i know of to ignore errors, you have to explicitly use LEFT functions in the SELECT or something to work around this issue.

    based on your table, only columns [EventType] [varchar](10) or [UserLogon] [varchar](30) seem to stand out to me to be candidates for strings bigger than the destination.

    No, the two fields' max len returns as 6 and 18.

    could the order of columns in the target table be differnet than the desitnaation table?

    ie if you expicitly name the insert column names as wella s the names of the SELECT columns?, are they in the same order as SELECT *?

    INSERT INTO EventlogHistory

    ([LogID],[ProjectID],[Description],[EventType],127.0.0.1,[UserLogon],[CreatedOn])

    SELECT [LogID],[ProjectID],[Description],[EventType],127.0.0.1,[UserLogon],[CreatedOn]

    FROM EventLog

    I might have to do that explicitly although I am unwilling to because of the number of fields in the real table I am working on.

  • Jeff Moden (1/16/2015)


    halifaxdal (1/16/2015)


    no way i know of to ignore errors, you have to explicitly use LEFT functions in the SELECT or something to work around this issue.

    based on your table, only columns [EventType] [varchar](10) or [UserLogon] [varchar](30) seem to stand out to me to be candidates for strings bigger than the destination.

    No, the two fields' max len returns as 6 and 18.

    Its fine the way you have it. If your history table has exactly the same schema and datatypes as the source table, then there's something very strange going on that's going to require a deeper dive on your part.

    You can see my script is to create an exactly the same schema with only one extra field added which is ArchivedOn (to record the back up time)

  • halifaxdal (1/16/2015)


    Jeff Moden (1/16/2015)


    halifaxdal (1/16/2015)


    no way i know of to ignore errors, you have to explicitly use LEFT functions in the SELECT or something to work around this issue.

    based on your table, only columns [EventType] [varchar](10) or [UserLogon] [varchar](30) seem to stand out to me to be candidates for strings bigger than the destination.

    No, the two fields' max len returns as 6 and 18.

    Its fine the way you have it. If your history table has exactly the same schema and datatypes as the source table, then there's something very strange going on that's going to require a deeper dive on your part.

    You can see my script is to create an exactly the same schema with only one extra field added which is ArchivedOn (to record the back up time)

    Dunno for sure, but that could be the reason why you're getting the error. You might need to list the columns both in the INSERT and the SELECT feeding it.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (1/16/2015)


    halifaxdal (1/16/2015)


    Jeff Moden (1/16/2015)


    halifaxdal (1/16/2015)


    no way i know of to ignore errors, you have to explicitly use LEFT functions in the SELECT or something to work around this issue.

    based on your table, only columns [EventType] [varchar](10) or [UserLogon] [varchar](30) seem to stand out to me to be candidates for strings bigger than the destination.

    No, the two fields' max len returns as 6 and 18.

    Its fine the way you have it. If your history table has exactly the same schema and datatypes as the source table, then there's something very strange going on that's going to require a deeper dive on your part.

    You can see my script is to create an exactly the same schema with only one extra field added which is ArchivedOn (to record the back up time)

    Dunno for sure, but that could be the reason why you're getting the error. You might need to list the columns both in the INSERT and the SELECT feeding it.

    I prefer doing it that way all the time. The exception to that is the SELECT INTO syntax, which I use for one-offs.

  • halifaxdal (1/16/2015)


    Lowell (1/16/2015)


    halifaxdal (1/16/2015)


    no way i know of to ignore errors, you have to explicitly use LEFT functions in the SELECT or something to work around this issue.

    based on your table, only columns [EventType] [varchar](10) or [UserLogon] [varchar](30) seem to stand out to me to be candidates for strings bigger than the destination.

    No, the two fields' max len returns as 6 and 18.

    could the order of columns in the target table be differnet than the desitnaation table?

    ie if you expicitly name the insert column names as wella s the names of the SELECT columns?, are they in the same order as SELECT *?

    INSERT INTO EventlogHistory

    ([LogID],[ProjectID],[Description],[EventType],127.0.0.1,[UserLogon],[CreatedOn])

    SELECT [LogID],[ProjectID],[Description],[EventType],127.0.0.1,[UserLogon],[CreatedOn]

    FROM EventLog

    I might have to do that explicitly although I am unwilling to because of the number of fields in the real table I am working on.

    Or like this:

    insert into EventlogHistory

    select *, getdate()

    from Eventlog

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • To be sure, the point I'm trying to make is that this error happens for a reason. You say there are a lot of columns in this table... are you absolutely 100% sure that the columns in the history table are in the exact same order as the original table and that they both start with the same thing in the first column?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • halifaxdal (1/16/2015)


    Hello, I have a script to be used to backup a specific table in a weekly basis, here is the approach what I take, if you have a better recommendation, it would be appreciated if you can share it with me (and others reading this post):

    If not exists (select * from sysobjects where name='EventlogHistory' and xtype='U')

    CREATE TABLE [dbo].[EventlogHistory](

    [LogID] [int] IDENTITY(1,1) NOT NULL,

    [ProjectID] [int] NOT NULL,

    [Description] [nvarchar](max) NOT NULL,

    [EventType] [varchar](10) NOT NULL,

    127.0.0.1 [varchar](50) NOT NULL,

    [UserLogon] [varchar](30) NOT NULL,

    [CreatedOn] [datetime] NOT NULL,

    ArchivedOn datetime default getdate())

    Problem #1 : The identity column means you can't be inserting without specifying the columns.

    Problem #2 : That table is not an exact replica of the source table, it has at least one extra column, so you can't be inserting into it from a SELECT *.

    Problem #3 : Don't ever use insert without a column list, and don't use select *

    Problem #4 : If you are using an IDENTITY column it's because you want to have a new set of numbers in this archive table for LogID, or else you would just have it as an int, wouldn't you?

    It seems like you might be trying to obfuscate the real problem (as we have all had to do from time to time) and in the process you may be hiding the real problem, because there is no way that an insert into that table without a column list from a select star would even get compiled, let alone run and produce that error.

    Of course, it is late and I could be wrong 😎

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

Viewing 15 posts - 1 through 15 (of 17 total)

You must be logged in to reply to this topic. Login to reply