Simple query concat quotes question

Posted on 2006-11-06
Last Modified: 2008-01-09

I am trying a fairly simple thing in pl/sql (oracle 9i). I need to check if a table exist or not. If is does then do some stuff else email user.

The problem is with the quotes around the string. my query is

SELECT COUNT(*) into tblYesNo  FROM sys.all_objects WHERE object_type = 'TABLE' AND object_name =  || CHR(39) || MY_TABLE || CHR(39) AND owner = CHR(39) || MY_OWNER || CHR(39);

Here tblYesNo, MY_TABLE, MY_OWNER are all variables and are being passed to this statement. As you know for comparison in where clause they should be concatinated with the quote e.g.'name'. I am not sure what is the way to concatinate a quote on both left and right side of my variable. I tried using its ASSI code which is CHR(39). It show correct statement when i debut.print but my statement returns nothing i.e. 0

Also is ther a way to get the date when a table was last analyzed if at all ?

what should i do. Please suggest.
Question by:Ade101
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 25 total points
ID: 17887290
if the MY_TABLE and MY_OWNER are variables, you don't need the quotes, as the variables are clearly of varchar data type, so no confusion possibly:

SELECT COUNT(*) into tblYesNo  FROM sys.all_objects WHERE object_type = 'TABLE' AND object_name =  MY_TABLE AND owner = MY_OWNER;

LVL 27

Expert Comment

ID: 17887318
Since MY_TABLE and MY_OWNER are varchar2 type variables there is no need to pad single quotes on both sides. You can directly use like:

SELECT COUNT(*) into tblYesNo  FROM sys.all_objects WHERE object_type = 'TABLE' AND object_name =  MY_TABLE AND owner = MY_OWNER ;

To find the last analyzed date use:
select LAST_ANALYZED from user_tables where table_name = 'TAB1';

Expert Comment

ID: 17890212
incidentally, the way to pass quotes into a query is by doubling them up:

SELECT 'o''clock' FROM dual

While not related to your query, it will save you writing CHR(39) all the time
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 32

Assisted Solution

awking00 earned 25 total points
ID: 17890726
Since you are selecting into a variable named tblYesNo, this is apparently part of a procedure. You can circumvent the quotes issue with dynamic sql.
sqlstmt  varchar2(255);
sqlstmt := 'SELECT COUNT(*) FROM sys.all_objects WHERE object_type = ''TABLE'' AND object_name =  '
||MY_TABLE ||' AND owner = '|| MY_OWNER;
execute immediate sqlstmt into tblYesNo;

Also, note that select last_analyzed from {dba|all|user}_tables will return null if table has not been analyzed.

Author Comment

ID: 17893108
Great !! Many Thanks...

Author Comment

ID: 17893117
Thats what i thought in first place why it was giving error when i am using VARCHAR variable. It worked this time .. There was another problem which was causing this :)

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question