IN operator with single quotes

Hi All,
I am having problem dealing with Single Quotes with IN operator. Below procedure will explain my problem:
create or replace procedure sp_countemp
p_empnames       in   varchar2
is
v_cnt            integer:=0;
BEGIN
select count(*) into v_cnt from emp where ename in (p_empnames);
DBMS_OUTPUT.PUT_LINE(v_cnt);
END;I will be getting p_empnames as 'ALLEN','SMITH'
But I am not getting any records here.
Thanks,
Danish

Hope this helps..
DECLARE
  TYPE strarray IS TABLE OF VARCHAR2(100);
  p_empnames STRARRAY;
  v_tot INTEGER :=0;
  v_cnt INTEGER := 0;
BEGIN
  p_empnames := strarray('Allen','Smith');
  FOR i IN p_empnames.FIRST .. p_empnames.LAST LOOP
      SELECT COUNT(*) INTO v_tot FROM emp WHERE ename = p_empnames(i);
      v_cnt := v_cnt + v_tot;
  END LOOP; 
  DBMS_OUTPUT.PUT_LINE('Count '||v_cnt);
END;
Count 2

Similar Messages

  • Dynamic SQL and Data with Single Quotes in it.

    Hi There,
    I have a problem in that I am using dynamic SQL and it happens that one of the columns does contain single quotes (') in it as part of the data. This causes the resultant dynamic SQL to get confused as the single quote that is part of the data is taken to mean end of sting, when in fact its part of the data. This leaves out a dangling single quote that was meant to enclose the string. Here is my dynamic SQL and the result of the parsed SQL that I have captured:
    ****Dynamic SQL*****
    l_sql:='select NOTE_TEMPLATE_ID '||
    'FROM TMP_NOTE_TEMPLATE_VALUES '||
    'where TRIM(LEGACY_NOTE_CODE)='''||trim(fp_note_code)||''' '||
    'and TRIM(DISPLAY_VALUE)='''||trim(fp_note_text)||''' ';
    execute immediate l_sql INTO l_note_template_id;
    Because the column DISPLAY_VALUE contains data with single quotes, the resultant SQL is:
    ******PARSED SQL************
    select NOTE_TEMPLATE_ID
    FROM TMP_NOTE_TEMPLATE_VALUES
    where TRIM(LEGACY_NOTE_CODE)='INQ' and TRIM(DISPLAY_VALUE)='Cont'd'
    And the problem lies with the single quote between teh characters t and d in the data field for DISPLAY_ITEM. How can I handle this?
    Many thanks,

    I have been reliably informed that if one doesn't enclose char/varchar2 data items in quotes, the right indices may not be usedI am into oracle for past 4 years and for the first time i am hearing this.
    Your reliable source is just wrong. Bind variables are variables that store your value and which are used in SQL. They are the proper way to use values in your SQL. By default all variables in PL/SQL is bind variable.
    When you can do some thing in just straight SQL just do it. Dynamic SQL does not make any sense to me here.
    Thanks,
    Karthick.

  • How to deal with single quote (') in a field value?

    I can successfully insert value with single quoet using
    Prepared statement with placeholder(?) construct .
    I can also successfuly use value with single quote(') in
    WHERE clause.
    My question is, is there a way to use string with single
    quote if a Statement like:
    String slqString ="INSERT INTO customers (name, address) VALUES ( 'O'Reilly Bob', 'St Mary's Street') ";
    Statement sqlStmt = con.createStatement();
    sqlStmt.executeUpdate(sqlString);
    The last statement will thow an SQLException because due to single quotes
    Any ideas?

    I think the question was regarding the ' in O'Reily. Use ' twice when using the Statement interface, i.e.
    ("O''Reilly Bob", "St Mary''s Street")
    So that's two single quotes, not a double quote, to successfully insert a single quote, if you know what I mean....
    But like you said PreparedStatement does things like this for you.

  • How to concatenate a string with single quotes

    Hi all,
        how to concatenate a string with single quotes to a variable.
    Sathya

    Hi sathyabama,
    1. simple
    2. use TILDE character <b>(`)</b>
       (just left to the '1' key)
    <b> `'mystring'`</b>
    3. just copy paste
    report abc.
    data : m(100) type c.
    concatenate `'amit mittal'` 'hello' into m separated  by space.
    write m.
    regards,
    amit m.

  • Firefox is replacing double quotes with single quotes.

    In WordPress and at other sites where I input and save text, Firefox is replacing double quotes with single quotes when I save. It's also showing double quotes as single quotes on websites. I tried uninstalling and reinstalling, and it's still happening.

    Do you have the needed font installed?
    *http://en.wikipedia.org/wiki/Punctuation

  • Importing excel files - problem with single quote

    When importing excel files using 1.5, I can't get data with single quotes (') imported.
    When I run the insert statement given in SQLPlus I get "ORA-01756: quoted string not properly terminated", which is different than the error that SQL Developer gives me (see below).
    Also, I have a numeric value shown without a thousands comma-separator in the XLS file that I'm trying to load into a varchar2 field. But, the insert statements have added a thousands comma-separator which I don't want.
    REM Error starting at line 1 in command:
    REM INSERT INTO table (ID, NAME, CODE)
    REM VALUES (2427407, 'Ed-u-care Children's Center', '73,000');
    REM Error at Command Line:2 Column:37
    REM Error report:
    REM SQL Error: ORA-00917: missing comma
    REM 00917. 00000 - "missing comma"
    REM *Cause:   
    REM *Action:
    One last thing, TOAD gives a way to automap columns chosen from XLS to the columns in the database. It sure would be nice to have this functionality in SQL Developer.
    Thanks,
    Steve

    Did you consider both to be bugs (i.e., single quote issue and thousands comma separator issue)?
    Thanks

  • Incorrect SELECT statement with single quotes

    I am getting the following error trying to select a value from a database:
    SQLException caught: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'mod='Payroll''. Couldn't load driver:[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'mod='Payroll''.
    Here is the select statement code:
    PreparedStatement ps = con.prepareStatement("SELECT username FROM experts WHERE mod='"+mod+"'");
    I have tried all kinds of variations with the quotes, and single quotes but still get the same error...I know I'm on the right track but I can't seem to spot the error and was hoping someone else could??
    thanks in advance,
    chuk

    Hi,
    I tried that and now I get an even stranger syntax error which says "Syntax error (missing operator) in query expression 'mod=Pa_RaM000'."
    any ideas?

  • Replacing " (double quote) with ' (single quote)

    Hi there,
    I have the following method to manipulate user input. All I want to do is, to replace the double quote with a single quote. The implementation I tried was,
    this.replace(desc, "\"", "'");
    This does not work for me. It jsut removes the double quote and does not introduces the single quote. Could any one please advise?
    Thanks,
    Des
    public static String replace(String line, String oldString, String newString) {
              if(line != null && oldString != null && newString != null) {
                   int index = 0;
                   while ((index = line.indexOf(oldString, index)) >= 0) {
                        line = line.substring(0, index) +
                             newString +
                             line.substring(index + oldString.length());
                        index += newString.length();
              return line;
         }

    Bad luck.. I am unable to get it still.
    This is the code I am using (implemetation is in a JSP. I am using this code to test it).
    public class Test{
    public static void main(String args[]){
         String s = args[0];
         Test t = new Test();
         String doubleQuote = "\"";
         System.out.println(" output :"+ t.replace(s,doubleQuote,"'"));
         //System.out.println(" output :"+ t.replace(s,"\"","'"));
         //System.out.println(" output :"+ t.replace('"', '\''));
    public static String replace(String line, String oldString, String newString) {
              if(line != null && oldString != null && newString != null) {
                   int index = 0;
                   while ((index = line.indexOf(oldString, index)) >= 0) {
                             System.out.println(" line count :");
                        line = line.substring(0, index) +
                             newString +
                             line.substring(index + oldString.length());
                        index += newString.length();
              return line;
    }mshn02
    The main problem is I am stuck with the server's version of Java (which I have no control on it). I could not use 1.4.2

  • Error with single quotes in open document function

    Hi all,
    I have an issue with the special characters using the open document function. I'm passing a name, and I'm using the URLEncode function, but it doesn't work for single quotes, it shows a javascript error. I tried with HTMLEncode and it works for single quotes, but not for the other special characters like @,+. I tried with both functions at the same time but it doesn't work either.
    Please let me know if you know how to solve it.
    Thanks,
    Marcela

    Hi Prashant,
    I did not work it works for single quotes only, but not for other special characters.
    This one doesn't work for single quotes
    ="<a href='../../scripts/openDocument.aspx?sType=wid&sDocName=Report_Name&sWindow=NEW&sReportName=Company Details&lsSPartner Id=" + [Partner Id] +"&lsSPartner Name=" + URLEncode([Partner Name]) +"'>View catalog</a>"
    This one doesn't work for special characters
    ="<a href='../../scripts/openDocument.aspx?sType=wid&sDocName=RP-DSPP-Company_Catalog_CORETEAM&sWindow=NEW&sReportName=Company Details&lsSPartner Id=" + [Partner Id] +"&lsSPartner Name=" + [Partner Name] +"'>View catalog</a>"
    Thank you,
    Marcela

  • Passing value in procedure with single quotes intact

    HI,
    I have the following procedure.
    create or replace procedure p1( a varchar2) is
    begin
    execute immediate ' update emp set sal = 20*Sal + sal
    where ename like ' || a;
    end;
    when I try to run this and pass a value to a. the single quotes
    r removed and the entire string becomes.
    'update emp set sal = 20*sal + sal where ename like scott'
    hence the error.
    now my problem is how can i make sure the scott goes like 'scott'
    i.e the single quotes are intact. or otherwise 'scott%'
    thanx in advance
    Sreekant Vadlamani

    You can also use triple quotes to do this. the effects of
    multiple quotes can be seen below. The same stuff works in
    procedures
    Connected to:
    Oracle8i Enterprise Edition Release 8.1.5.0.0 - Production
    With the Partitioning and Java options
    PL/SQL Release 8.1.5.0.0 - Production
    SQL> select 'hello' from dual;
    'HELL
    hello
    SQL> select ''hello'' from dual;
    select ''hello'' from dual
    ERROR at line 1:
    ORA-00923: FROM keyword not found where expected
    SQL> select '''hello''' from dual;
    '''HELL
    'hello'
    SQL> select ''''hello'''' from dual;
    select ''''hello'''' from dual
    ERROR at line 1:
    ORA-00923: FROM keyword not found where expected
    SQL> select '''''hello''''' from dual;
    '''''HELL
    ''hello''
    SQL>

  • How to handle XML string with Single Quotes as a parameter to SP dynamically?

    Hi,
    I would like to know if there is a way to handle the Single Quotes in XML value when it is passed to Stored Procedure?
    I should be able to handle it without adding another Single Quote to it.
    Thanks,
    Chandra Shekar

    Hi Chandra,
    Your requirement is not precise. Based on my understanding and guessing, are you metioning something like the below sample?
    /*If the xml is generated you have no need to escape the singe quote(')*/
    DECLARE @xmlTbl TABLE (ID INT,name VARCHAR(99));
    INSERT INTO @xmlTbl VALUES(1,'Eric''s')
    INSERT INTO @xmlTbl VALUES(2,'Zhang''s')
    DECLARE @xmlDoc1 XML
    SELECT @xmlDoc1
    FROM @xmlTbl FOR XML PATH('PERSON'),ROOT('PERSONS')
    EXEC yourProcedure @xmlDoc1
    /*If your copy and paste the xml, you have to escape the single quote(') with 2s('')*/
    DECLARE @xmlDoc2 XML
    SET @xmlDoc2 = '<PERSONS>
    <PERSON>
    <ID>1</ID>
    <name>Eric''s</name>
    </PERSON>
    <PERSON>
    <ID>2</ID>
    <name>Zhang''s</name>
    </PERSON>
    </PERSONS>'
    EXEC yourProcedure @xmlDoc2
    If that is not regarding your requirement, please elaborate with more details.
    Eric Zhang
    TechNet Community Support

  • Problem with single quote when exporting insert statement

    Hi
    I'm using Oracle SQL Developer 2.1.1.64 on Ubuntu 10.04. I got some records which has single quote in it.
    For example,
    Let says Table '*TABLE_A*' has varchar2 column called '*COL_A*'.
    And there is only one record in the table and the value is:
    his friend's dog name is dog.
    When I export that table data to insert statement, i got this:
    Insert into TABLE_A (COL_A) VALUES ('his friend's dog name is dog.');
    As you can see friend's is wrong, it should be friend''s instead. (note the double single quotes).
    Anyone knows how to fix this please?

    Yes - that's a bug. But probably not what you're expecting.
    Mind you really can't use "normal" SQL on LOBs, because they're just too big to fit in the statements.
    You should export and import them through e.g. the DBMS_LOB package.
    I do remember some request on the SQL Developer Exchange to automate this, so go vote there to add weight for future implementation.
    So the bug is that the column's fields should all yield NULL inside the INSERTs.
    Regards,
    K.

  • Problem with single quote around ANYINTERACT

    CREATE TABLE JUNK
    as SELECT s.survey_id,s.shape,s.original_depth
    FROM bathyuser.sounding s,m_covr r
    WHERE SDO_GEOM.RELATE (s.shape, 'ANYINTERACT', R.geom, 0.5) = 'TRUE'
    AND R.DSNM=CNT.DSNM
    and
    s.SURVEY_ID in
    (SELECT unique s.survey_id FROM header s, m_covr r
    WHERE SDO_GEOM.RELATE (s.shape, 'ANYINTERACT', r.geom, 0.5) = 'TRUE'
    AND DSNM=CNT.DSNM)';
    This above SQL works fine in SQL prompt. But when I put it together in PL/SQL it gives me this error :
    WHERE SDO_GEOM.RELATE (s.shape, 'ANYINTERACT', R.geom, 0.5) = 'TRUE'
    ERROR at line 17:
    ORA-06550: line 17, column 39:
    PLS-00103: Encountered the symbol "ANYINTERACT" when expecting one of the
    following:
    * & = - + ; < / > at in is mod remainder not rem
    <an exponent (**)> <> or != or ~= >= <= <> and or like LIKE2_
    LIKE4_ LIKEC_ between || multiset member SUBMULTISET_
    Could someone help me fix this error. I know I have to try it with "EXECUTE IMMEDIATE" command, but I'm having problem with "||" (concatinate).
    Thanks so much
    Cuong

    This is how you go about debugging execute immediate statements. I am assuming that you fixed the first problem as marias suggested and cam up with something like this. Note, I changed the table in your surso to dual since I do not have your tables, but the effect is the same. I changed the EXECUTE IMMEDIATE to DBMS_OUTPUT.Put_Line so you can actually see what you are trying to execute. I also added some carriage returns to make the formatting a littel easier to see. This is what I got:
    SQL> DECLARE
      2     statement varchar2(1000);
      3  CURSOR C_DSNM5 IS
      4     SELECT dummy dsnm FROM dual;
      5  BEGIN
      6     FOR CNT IN C_DSNM5 LOOP
      7        STATEMENT := 'CREATE TABLE JUNK'||
      8                     ' AS SELECT s.survey_id,s.shape,s.original_depth'||CHR(10)||
      9                     ' FROM bathyuser.sounding s,m_covr r '||CHR(10)||
    10                     'WHERE SDO_GEOM.RELATE (s.shape, '||
    11                     '''ANYINTERACT'''||
    12                     ', R.geom, 0.5) = '||'''TRUE'''||CHR(10)||
    13                     ' AND R.DSNM = CNT.DSNM '||CHR(10)||
    14                     ' AND s.SURVEY_ID IN '||
    15                     ' (SELECT UNIQUE s.survey_id FROM HEADER s, m_covr r'||CHR(10)||
    16                     ' WHERE SDO_GEOM.RELATE (s.shape, '||
    17                     '''ANYINTERACT'''||
    18                     ', r.geom, 0.5) = '||
    19                     '''TRUE'''||CHR(10)||
    20                     ' AND R.DSNM = '||CNT.DSNM||' )';
    21        DBMS_OUTPUT.Put_Line (statement);
    22     END LOOP;
    23  END;
    24  /
    CREATE TABLE JUNK AS SELECT s.survey_id,s.shape,s.original_depth
    FROM bathyuser.sounding s,m_covr r
    WHERE SDO_GEOM.RELATE (s.shape, 'ANYINTERACT', R.geom, 0.5) = 'TRUE'
    AND R.DSNM = CNT.DSNM
    AND s.SURVEY_ID IN  (SELECT UNIQUE s.survey_id FROM HEADER s, m_covr r
    WHERE SDO_GEOM.RELATE (s.shape, 'ANYINTERACT', r.geom, 0.5) = 'TRUE'
    AND R.DSNM = X )
    PL/SQL procedure successfully completed.You have at least two issues with this CREATE TABLE statement. The last line needs to have the X (which came from dual) quoted since it is a string. You also still have a reference to CNT.DSNM, unless you fixed both occurences.
    Another major problem is that if your cursor will return more than one row, and by the structure of this I assume that it might, the on the second record you are going to get:
    ORA-00955: name is already used by an existing object.
    Although I see no reason why you need dynamic sql here, you really need to use bind variables whereever possible (everywhere you use a variable except ofr names of database objects. So, your procedure should look more like:
    DECLARE
       statement varchar2(1000);
       CURSOR C_DSNM5 IS
          SELECT distinct DSNM from M_COVR where dsnm like 'US5%';
    BEGIN
       FOR CNT IN C_DSNM5 LOOP
          STATEMENT := 'CREATE TABLE JUNK'||
                       ' AS SELECT s.survey_id,s.shape,s.original_depth'||
                       ' FROM bathyuser.sounding s,m_covr r '||
                       'WHERE SDO_GEOM.RELATE (s.shape, '||
                       '''ANYINTERACT'''||
                       ', R.geom, 0.5) = '||'''TRUE'''||
                       ' AND R.DSNM = :b1 '||
                       ' AND s.SURVEY_ID IN '||
                       ' (SELECT UNIQUE s.survey_id FROM HEADER s, m_covr r'||
                       ' WHERE SDO_GEOM.RELATE (s.shape, '||
                       '''ANYINTERACT'''||
                       ', r.geom, 0.5) = '||
                       '''TRUE'''||
                       ' AND R.DSNM = :b2 )';
          EXECUTE IMMEDIATE statement USING cnt.dsnm, cnt.dsnm
       END LOOP;
    END;But it is still very wrong.
    Perhaps if you can explain in words what you are trying to accomplish someone can provide a much better response.
    John

  • How can i insert values with single quote (')

    How can i insert a values like ABC's ?
    Edited by: LuKKa on Oct 6, 2010 12:48 PM

    LuKKa wrote:
    I want to insert like this :-
    INSERT INTO myTab values ('This is belong's to ABC's ');
    Then how can i do ?In a old style you can do this
    insert into mytab valeus('This is belong''s to ABC''s');

  • Problem in insertion with string containing   ' (single quote)

    i have a text field in jsp.
    when i submit the content need to be inserted/updated.
    when the text field contains character's with single quote( ' )..
    i am unable to insert/update values in database..
    where as if the text field contains characters without single quote..there is no problem in Database insertion/updation.
    i am using create statement .. and oracle database..
    can any one help ...

    The usual answer for this in the JDBC forum (where this should have been posted because it's completely about JDBC) is to use a PreparedStatement.

Maybe you are looking for

  • IWeb photo galleries cause Internet Explorer to crash

    Please help! I am a novice as far as web design goes and have spent a long time getting my site up and going. It works perfectly on Firefox, Chrome, Safari etc but when using Internet explorer the photo galleries cause the browser to crash/freeze. If

  • Image quality in ImageIO.write();

    Hello, Ive made a prog to convert a BufferedImage into a jpeg and write the jpeg to file: ImageIO.write(image, "jpg", file); does anyone know how i can specify the encoding quality? Thanks, Jim

  • TS1363 my iphone4 is not moving pass the boot screen help

    Help my Iphone4 will not go pass the boot up page its just sittes there

  • Question about "bluetooth PDA-syn" option?

    HI there, I would just like to ask under system preferences, under bluetooth, under advance, there is an option called "bluetooth pda-sync", in its box, should I check or uncheck this option? what is the option "bluetooth pda-sync" for? what is its u

  • Numbers equivalent to Excel DMAX?

    Hiya folks! Is there a Numbers equivalent to Excel's DMAX function (as described here: http://office.microsoft.com/en-us/excel-help/dcount-HP005209049.aspx)?  I'm looking to create a price book and would like essentially two tables.  The first table