single quotes and inserting into table

  • i am creating an package where it gets from a webservice and retrieve the data thru xml task and insert into a sql table

    it works fine in cases where the name has single quotes like o'connor, o'rielly it throws an error.

    My current insert statement is below. i get and i am replacing with null. This works for all data except with single quotes.

    "insert into MyTable(column1) values ('" + REPLACE((DT_STR, 25, 1252) @[User::XMLTestVar1], "", "") + "')"

    Can anybody let me know how to handle those type of names too?

  • Try this

    "insert into MyTable(column1) values ('" + REPLACE((DT_STR, 25, 1252) @[User::XMLTestVar1], "'", "''") + "')"

    I have a ssis that logs on error on event handler. and I have used this one, should work too for you

  • You need to escape the single-quote/apostrophe in the name. This should work:

    "insert into MyTable(column1) values ('" + QuoteName(REPLACE((DT_STR, 25, 1252) @[User::XMLTestVar1], "", ""), '''') + "')"

    You can run this to see how it works:

    DECLARE @name VARCHAR(20),

    @sql VARCHAR(100)

    SET @name = 'o''sullivan'

    SET @sql = 'Select ' + QUOTENAME(@name, '''')

    EXEC(@sql)

  • I tried it worked.

    i have one more scenario

    How do i implement the single quotes in the attached file. any help

  • I think this is what you need.

    "insert into MyTable(column1) values (QuoteName(" + REPLACE((DT_STR, 25, 1252) @[User::XMLTestVar1], "", "") + ", ''''))"

    You may have to tweak it a little to make sure quotes get in the right place. Basically you want your query to wrap the quotename function around the string.

Viewing 5 posts - 1 through 4 (of 4 total)

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