简体   繁体   English

SQL数据中的Apostrophe('),Oracle 11g

[英]Apostrophe(') in SQL data,oracle 11g

I need to insert data into a sql table using a csv file with apostrophe(') and ('') in few rows. 我需要在几行中使用带有trotrophe(')和('')的csv文件将数据插入sql表中。 I was able to handle it using the below method. 我可以使用以下方法来处理它。

Get open_quote and close_quote and put the username and email_id between these two variable. 获取open_quote和close_quote并将用户名和email_id放在这两个变量之间。

SELECT  CHR(113)||CHR(39)||CHR(91) INTO OPEN_QUOTE FROM dual;
SELECT  CHR(93)||CHR(39) INTO CLOSE_QUOTE FROM dual; 

enter image description here It looks ugly.I could have used replace but i opted for this method. 在这里输入图片描述看起来很丑。我可以使用replace,但是我选择了这种方法。

Could you please let me know of any other method so that my code looks good? 您能否让我知道其他方法,以便我的代码看起来不错? Attached is the screenshot of the dynamic sql. 附件是动态sql的屏幕截图。

You can have a single quote in a string by doubling it. 通过将字符串加倍,可以在字符串中使用单引号。 For instance: 例如:

select 'It''s Bob''s book'
from dual;

As of Oracle 10g in PL/SQL you can have: 从PL / SQL中的Oracle 10g开始,您可以:
V_SQL_STATEMENT := q'[It's Bob's book]';
See Oracle SQL Reference for details on text literals. 有关文本文字的详细信息,请参见《 Oracle SQL参考 》。

Use the alternative-quoting mechanism and several REPLACE s instead of concatenation. 使用替代引用机制和几个REPLACE代替串联。 It's a little extra work but it makes it clearer what the final SQL statement will look like. 这是一项额外的工作,但使最终的SQL语句看起来更加清晰。

v_sql_statement := replace(replace(replace(replace(
    q'[
        insert into login
        (USER_ID,CLIENT_ID,EMAIL,PSWD_HASH,NETWORK_ID,FAILED_LOGIN_ATTEMPTS,NAME)
        VALUES
        (
            LOGIN_SEQ.nextval,
            #P_CLIENT_ID#,
            '#PARSE_EMAIL#',
            #V_PSWD_HASH#,
            NULL,
            0,
            '#USER_NAME#'
        )
    ]'
    , '#P_CLIENT_ID#', p_client_id)
    , '#PARSE_EMAIL#', parse_email(lower(c1.email)))
    , '#V_PSWD_HASH#', v_pswd_hash)
    , '#USER_NAME#',   nvl(c1.name, generate_user_name(parse_email(c1.email))))
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM