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

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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to trim oracle sql sentence in unix 17 58
form builder not starting 3 55
Database Design Dilemma 6 58
Loading flat file data in tables 2 41
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

861 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