SQL Script Failure

Samuel Thomas 1 Reputation point
2020-10-27T11:25:23.437+00:00

Please help: I am experiencing failure running the below script at the section like 'Apple_For_Lunch - breakfast earlier'.

INSERT INTO TableB
                  (LoopValue, Columnvalue)
VALUES     ('Apple_For_Lunch - breakfast earlier', N'Select Columnvalue from TableA where Columnvalue like 'Apple_For_Lunch - breakfast earlier'')
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,688 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,566 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,625 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Dan Guzman 9,236 Reputation points
    2020-10-27T12:05:54.667+00:00

    You need to escape single quotes in literals with 2 quotes. Try:

    INSERT INTO TableB (LoopValue, Columnvalue)
    VALUES     ('Apple_For_Lunch - breakfast earlier', N'Select Columnvalue from TableA where Columnvalue like ''Apple_For_Lunch - breakfast earlier''');
    
    1 person found this answer helpful.
    0 comments No comments

  2. Viorel 116.7K Reputation points
    2020-10-27T12:09:21.23+00:00

    Maybe you need something like this:

    INSERT INTO TableB (LoopValue, Columnvalue)
    Select 'Apple_For_Lunch - breakfast earlier', Columnvalue
    from TableA
    where Columnvalue = 'Apple_For_Lunch - breakfast earlier'

    Probably some column names need adjustments. If you want to find substrings, then try where Columnvalue LIKE '%Apple_For_Lunch - breakfast earlier%'.

    0 comments No comments

  3. EchoLiu-MSFT 14,581 Reputation points
    2020-10-28T02:28:49.283+00:00

    Hi @Samuel Thomas

    By default, ' is the boundary character of the string, if you include ' in the string, you must use two ', and the first ' is the escape character.

    I have provided an example, please refer to:

        select 'Apple_For_Lunch - breakfast earlier' col1,  
        N'Select Columnvalue from TableA where Columnvalue like ''Apple_For_Lunchbreakfast earlier''' col2  
        into #test  
          
        select * from #test  
    

    35516-image.png

    When the escape character is missing, the statement returns an error:

        select 'Apple_For_Lunch - breakfast earlier' col1,  
        N'Select Columnvalue from TableA where Columnvalue like 'Apple_For_Lunchbreakfast earlier'' col2  
        into #test  
    

    35606-image.png

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.