Quantcast
Channel: VAS Feeds
Viewing all articles
Browse latest Browse all 10

How to run SQL commands with an input variable

$
0
0

sql-logo

This post will guide you how you can play with SQL using the interactive variable values inside the statement. To begin with, suppose we even don’t know which table we want to query from. To check all the tables inside the logged in database, you can query the TABLE_NAME from user_tables. To get all the columns and the type of values they can store, you can use “desc table_name”.

SQL> desc user_tables
Name                                      Null?    Type
—————————————– ——– —————————-
TABLE_NAME                                NOT NULL VARCHAR2(30)
TABLESPACE_NAME                                    VARCHAR2(30)
CLUSTER_NAME                                       VARCHAR2(30)
IOT_NAME                                           VARCHAR2(30)
PCT_FREE                                           NUMBER
PCT_USED                                           NUMBER
INI_TRANS                                          NUMBER
MAX_TRANS                                          NUMBER
INITIAL_EXTENT                                     NUMBER
NEXT_EXTENT                                        NUMBER
MIN_EXTENTS                                        NUMBER
MAX_EXTENTS                                        NUMBER
PCT_INCREASE                                       NUMBER
FREELISTS                                          NUMBER
FREELIST_GROUPS                                    NUMBER

[cut]

As you can see, we can get all the columns types. Now, lets check how many tables do we have.

SQL> select TABLE_NAME from user_tables;

TABLE_NAME
——————————
ACCESS$
APPLY$_CONF_HDLR_COLUMNS
APPLY$_DEST_OBJ
APPLY$_DEST_OBJ_CMAP
APPLY$_DEST_OBJ_OPS
APPLY$_ERROR
APPLY$_ERROR_HANDLER
APPLY$_SOURCE_OBJ
APPLY$_SOURCE_SCHEMA
APPROLE$
AQ$_MESSAGE_TYPES

TABLE_NAME
——————————
AQ$_PENDING_MESSAGES
AQ$_PROPAGATION_STATUS
AQ$_PUBLISHER
AQ$_QUEUE_STATISTICS
AQ$_QUEUE_TABLE_AFFINITIES
AQ$_REPLAY_INFO
AQ$_SCHEDULES
AQ_EVENT_TABLE
AQ_SRVNTFN_TABLE

[cut]

You can check the tables and query your required table. Suppose I wanted to check the table SMON_SCN_TIME. This table is filled by SMON every 5 minutes with a time stamp and current SCN. I previously discussed the processes involved in Oracle including SMON. Now, I want to query this table but want to check it with different limits of the TIME_DP column. So, I will run the command with && option followed by a variable for which I will input the value after running the command as shown below:

SQL> l
  1  select * from SMON_SCN_TIME
  2  where TIME_DP>&&x
  3*
SQL> /
Enter value for x:

As you see, it is asking me for a value of x now, I can set any value I want and get the results. If I execute the command the second time, it won’t ask for the value of x, rather take the previously entered value. To reset the value of the variable, use undefine and run the command again with a different value of x.

SQL> undefine x
SQL>
SQL>
SQL> /
Enter value for x:


Viewing all articles
Browse latest Browse all 10

Trending Articles