Select query to get table names
Hi,
Can i retrieve a table name from a select query if i know value of one of the columns in the table?
Glen
Can't you take a look at the source code and see what table those values are coming from? That's likely the easiest solution.
Barring that, you could use dynamic SQL, but it's going to be awfully inefficient. Something like
FOR x IN (SELECT * FROM user_tab_cols WHERE data_type = 'VARCHAR2')
LOOP
EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM ' || x.table_name ||
' WHERE ' || x.column_name || ' = <<literal value>>'
INTO l_cnt;
IF( l_cnt > 1 )
THEN
DBMS_OUTPUT.PUT_LINE( 'Found the value' );
END IF;
END LOOP;This assumes that you know at least what data type the value is stored in. It's also going to generate a tremendous number of full table scans, so it'll be slow as molasses on a system of any size.
Justin
Similar Messages
-
Creating SELECT query between 3 tables
Hi there,
Im trying to create a SELECT query between 3 tables and it is driving me round the bend.
I have 3 tables connected together which are:
MODUL
modulid
modulname
modulevel
STUDENT
studentid
surname
inits
s e x
phone
email
logon
STUDREGOCCUR
modulid
acyear
semester
occletter
studentid
result
I want to select the fields surname, inits, studentid from the STUDENT table, modulname from the MODUL table and result from my STUDREGOCCUR table that is NOT NULL.
I have tried SELECT STUDENT.STUDENTID, STUDENT.INITS, STUDENT.SURNAME, MODUL.MODULNAME, STUDREGOCCUR.RESULT WHERE STUDREGOCCUR.RESULT IS NOT NULL;
I have also tried many other ways and done research hwich hasnt really helped me unfortunately.
Im quite new to SELECT queries and im not sure where im going wrong, i would greatly appreciate if someone could help me solve my problem.
Thanks for the help :)
Edited by: user633643 on 06-Dec-2008 08:09If you want data from multiple tables you would need to perform a join. The general form would be:
select t1.cola, t2.col2, t3.col.x
from table_a a, table_b b, table_c c
where a.key = b.key
and b.key = c.key
or perhaps c relates back directly to a so it is a.key = c.key where key can be any column whose value is equilivent in what it represents to the value it is being compared to, that is, student_number = student_number or the columns regardless of name are both building numbers, room numbers, etc ....
This is not your exact solution but it should help.
HTH -- Mark D Powell -- -
Select query on a table with 13 million of rows
Hi guys,
I have been trying to perform a select query on a table which has 13 millions of entries however it took around 58 min to complete.
The table has 8 columns with 4 Primary keys looks like below:
(PK) SegmentID > INT
(PK) IPAddress > VARCHAR (45)
MAC Address > VARCHAR (45)
(PK) Application Name > VARCHAR (45)
Total Bytes > INT
Dates > VARCHAR (45)
Times > VARCHAR (45)
(PK) DateTime > DATETIME
The sql query format is :
select ipaddress, macaddress, sum(totalbytes), applicationname , dates,
times from appstat where segmentid = 1 and datetime between '2011-01-03
15:00:00.0' and '2011-01-04 15:00:00.0' group by ipaddress,
applicationname order by applicationname, sum(totalbytes) desc
Is there a way I can improve this query to be faster (through my.conf or any other method)?
Any feedback is welcomed.
Thank you.
MusTolls wrote:
What db is this?
You never said.
Anyway, it looks like it's using the Primary Key to find the correct rows.
Is that the correct number of rows returned?
5 million?
Sorted?I am using MySQL. By the way, the query time has been much more faster (22 sec) after I changed the configuration file (based on my-huge.cnf).
The number of rows returned is 7999 Rows
This is some portion of the my.cnf
# The MySQL server
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
skip-locking
key_buffer = 800M
max_allowed_packet = 1M
table_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
log = /var/log/mysql.log
log-slow-queries = /var/log/mysqld.slow.log
long_query_time=10
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 6
Is there anything else I need to tune so it can be faster ?
Thanks a bunch.
Edited by: user578505 on Jan 17, 2011 6:47 PM -
How to compare 2 different database to get table name which are not present in second database
How to compare 2 different database to get table name which are not present in second database
Sorry cannot test it right now
use db1
go
select * from sys.tables t where not exists
(select * from db2.sys.tables s where t.object_id=s.object_id)
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Using sql query as a table name
hello,
I have a table(say table1) which is storing the names of some other tables. I want to access the name of a table from table1 using sql query and then use the result of this query as the table name to access the data from
retrieved table name. How can I do this ?
ex:
select * from (select tablename from table1 where tableid='1');
I want to do something likw this. How can I do this?I want to access the name of a table from table1 using sql query and then use the result of this query as the table name to access the data from retrieved table name. How can I do this ?e.g. like this:
SQL> with table1 as (
select 'emp' tablename from dual
select extractvalue(x.column_value, 'ROW/ENAME') ename
from table1, xmltable('ROWSET/ROW' passing dbms_xmlgen.getxmltype('select * from ' || tablename)) x
ENAME
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
14 rows selected. -
Select Query, Showing What Tables a Specific User Key Can Be Found?
I need to have query or Filter to show me all data elements where user_key='001' is found in database.
Basically what tables would I find user_key='001'
Select * ______________ where user_key='001';
Need help on what to put in the above... because I want to search all database and Not specific table.
Thanks.
JDog29Hi,
Here's a SQL*Plus solution.
You run this script:
-- Find_Number.sql -- Find all NUMBER columns where &1 occurs
-- FIND_NUMBER finds which number columns in the database
-- contain a given value, such as 12345
-- find_number writes and executes a script called find_number_1.sql,
-- which contains rows like
-- @@find_number_sub SCOTT EMP EMPNO 12345
-- When run, this line will produce the output
-- SCOTT EMP EMPNO
-- if there is a row in scott.emp where empno=12345.
SET FEEDBACK OFF
SET LINESIZE 200
SET PAGESIZE 0
SET VERIFY OFF
DEFINE target_num = &1
DEFINE dir = o:\oradba\dbobj
SPOOL &dir\find_number_1.sql
PROMPT SPOOL &dir\find_number_&target_num..lst
PROMPT
SELECT '@@find_number_sub'
, owner
, table_name
, column_name
, &target_num
FROM all_tab_columns
WHERE data_type IN ('NUMBER')
AND owner IN ('SCOTT') -- If needed
-- AND ROWNUM <= 50 -- For testing
ORDER BY
owner
, table_name
, column_name
PROMPT
PROMPT SPOOL OFF
SPOOL OFF
@&dir\find_number_1On the same directory, you should have the following script:
-- Find_Number_sub.sql - See if any row of
-- table &1.&2 column &3 is equal to &4
-- If so, &1, &2 and &3 will be displayed
-- If not, nothing will be displayed
SELECT '&1' -- Owner
, '&2' -- Table name
, '&3' -- Column name
FROM dual
WHERE EXISTS
SELECT 0
FROM &1..&2
WHERE &3 = &4
; To search for columns containing the number 12345:
@find_number 12345A file called find_number_12345.lst, containing the results, will be produced. -
Hello All,
I am writing select query on TCURR table , but unable to get the values from table though i cansee the entries in table for the same condition as i provided int he program .
Is there an peculiar reason for the sme .
MY query is as follows.
SELECT * FROM tcurr INTO TABLE g_t_tcurr
WHERE kurst = 'M' AND fcurr = g_f_waers
AND tcurr = 'AUD'
AND gdatu LE l_f_datum (MKPF-BUDAT).
Any help is appreciated .
ThanksHi,
Please check the datatype and try again...
data:
DATC(8) TYPE C,
GDATU_OLD(8) TYPE C.
*exc_type = 'M'.
SELECT * FROM tcurf into table g_t_tcurr
CLIENT SPECIFIED
WHERE kurst = exc_type
AND fcurr = for_curr
AND tcurr = loc_curr
AND gdatu GE datc
AND mandt = mandt
ORDER BY PRIMARY KEY.
FM: CONVERT_TO_LOCAL_CURRENCY_N can be used to convert the amt from local currency to Foreign currency.
Regds
Parvathi -
Is it worth using select query on infotype tables
Hi Experts,
I might be posting in the wrong column, but i just need to know is it worth using a select query on Infotype tables (PAxxxx)?? or should we prefer using the function modules for data fetching?
If select is not suggested, what is the reason for that?
Rgds
PrateekHi ,
Its not said that u cant write select on PAXXXX tables . Yes of couse LDB are there to fetch the data but it depends on
the requirement when to write a select and when to consider using in LDB .
Generally when you are looking at say 8 to 10 tables of infotypes with free selection , then LDB is suggested to fetch the data .
if you are looking to fetch the data for say some tables for a restricted selection (where clause) then select is used .
If i want to write a program using select only then fetching data from infotypes tables for large no of records will lead to
more time consumption which becomes easier in LDB as they are fetched in hierarchy level based on keys .
Normally it will be a combination of LDB and select querys in the development scenario
Br,
Vijay. -
CV04N takes long time to process select query on DRAT table
Hello Team,
While using CV04N to display DIR's, it takes long time to process select query on DRAT table. This query includes all the key fields. Any idea as to how to analyse this?
Thanks and best regards,
Bobby
Moderator message: please read the sticky threads of this forum, there is a lot of information on what you can do.
Edited by: Thomas Zloch on Feb 24, 2012Be aware that XP takes approx 1gb of your RAM leaving you with 1gb for whatever else is running. MS Outlook is also a memory hog.
To check Virtual Memory Settings:
Control Panel -> System
System Properties -> Advanced Tab -> Performance Settings
Performance Options -> Adavanced Tab - Virtual Memory section
Virtual Memory -
what are
* Initial Size
* Maximum Size
In a presentation at one of the Hyperion conferences years ago, Mark Ostroff suggested that the initial be set to the same as Max. (Max is typically 2x physical RAM)
These changes may provide some improvement. -
Hello Friends..
I need your Help My Friends...
we are working currently on Oracle Applications : 12.1.1
i would like to know the best way to get Table name from Web Based Form..
Regards
Yas.Hi Yas;
Please follow below 2 thread i belive you will find some hint in those thread
table name for a jdeveloper page
Re: EBS, 11.5.10, table name
Hope it helps
Regard
Helios -
How to insert the select query result into table?
How to insert the select query result into table?
SELECT top 20 creation_time
,last_execution_time
,total_physical_reads
,total_logical_reads
,total_logical_writes
, execution_count
, total_worker_time
, total_elapsed_time
, total_elapsed_time / execution_count avg_elapsed_time
,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,
((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE qs.statement_end_offset END
- qs.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY total_elapsed_time / execution_count DESC;
Thanks,
Tirumala1. SELECT INTO
Below method will create table when data is inserted from one table to another table. Its useful when you need exactly same datatype as source table.
Use AdventureWorks2008R2;
Go
---Insert data using SELECT INTO
SELECT AddressLine1, City
INTO BothellAddresses
FROM Person.Address
where City = 'Bothell';
GO
---VERIFY DATA
Select AddressLine1, City
FROM BothellAddresses
---DROP TABLE
DROP TABLE BothellAddresses
GO
2. INSERT INTO SELECT
Below method will need table to be created prior to inserting data. Its really useful when table is already created and you want insert data from
another table.
Use AdventureWorks2008R2;
Go
---Create Table
CREATE TABLE BothellAddresses (AddressLine1 NVARCHAR(60), City NVARCHAR(30))
---Insert into above table using SELECT
INSERT INTO BothellAddresses(AddressLine1, City)
SELECT AddressLine1, City
FROM Person.Address
where City = 'Bothell';
---VERIFY DATA
Select AddressLine1, City
FROM BothellAddresses
---DROP TABLE
DROP TABLE BothellAddresses
GO
Regards,
Vishal Patel
Blog: http://vspatel.co.uk
Site: http://lehrity.com -
Getting table name from a query
Hi, I'm trying to get the table name from my sql query
My code is as follows;
ResultSetMetaData metadata = rs.getMetaData();
for (int i = 1; i < metadata.getColumnCount(); i++)
System.out.println(metadata.getTableName(i));
I just get a blank output. Yet my sql query executes and I can pull down the records, and grab the column names from the result sets.
Any ideas on how I can get the table name for the columns. The reason being is on one of my querys I'm using join statements and i need to retrieve the table name for the column i'm displaying, as some tables have similar column names.
Thanks in advance.Hi,
What db are you using?
I also get a blank output with ResultSetMetaData.getTableName because Oracle doesn't implement this method.
Why do you not know the table name anyway?
regards
Nick -
Query to get the name of the tables used in a View?
Hi,
Could you please help me in getting the name of the tables used in a perticular view?
Please help me out...
thnx in advance.
Thnx,
Bits
Edited by: Bits on Sep 14, 2009 2:15 PMThere is no data dictionary table that stores this directly.
- You could get the DDL used to create the view and parse that SQL. Depending on how complex the view might be, this could be relatively easy or highly complex.
- You could get a recursive list of the objects that the view depends on (from ALL_/USER_/DBA_DEPENDENCIES) and trace those dependencies back to all the tables that the view depends on. If your view calls functions, however, this may or may not be what you're looking for. If you are selecting from just one table in the FROM clause but you are calling a function that references a dozen tables, do you want one row? Or thirteen? What about tables referenced in a correlated subquery in the WHERE clause?
Justin -
Selecting a query as a table name
Greedings,
an example to explain what im trying to do
lets says
query1:=table1.name||'_code';
so outcome will be for example 'citizen_code'
what im trying to do is select the specific query to get the required data
select * from query1@seconddatabase where .....
is it possible to do something like that?
thanks for your time,
demThe common ways to do this are use dynamic SQL or synonym frigging (which I would massively discourage).
A nice work around is to do somwething like this...
CREATE OR REPLACE VIEW emp_vw
AS
SELECT 'DB1' db,
empno
FROM emp@db1
UNION ALL
SELECT 'DB2' db,
empno
FROM emp@db2;
SELECT *
FROM emp_vw
WHERE db = 'DB2';Keep it in the SQL!
Cheers
Ben -
Query to get file name of field
Hi folks,
I have one field in SQL table with whole file names:
e.g
C:\Directory1\File 1.txt
C:\Drirectory2\SubDirectory1\Image 33.jpg
I need one query to show only the file names:
e.g
File 1.txt
Image 33.jpg
What I need to do to get only the file names as result of query?create table test (filenames varchar(100))
insert into test values ('C:\Directory1\File 1.txt'),('C:\Drirectory2\SubDirectory1\Image 33.jpg'),('aaa'),(null)
select Reverse(stuff(reverse(filenames)
, Case when Charindex('\',reverse(filenames))>0 then Charindex('\',reverse(filenames)) else len(filenames) end
, Case when Charindex('\',reverse(filenames))>0 then len(filenames) else 0 End
,'')) fileCol from test
--Or
SELECT Distinct Coalesce(S.a.value('(/H/r)[4]', 'VARCHAR(100)'), S.a.value('(/H/r)[3]', 'VARCHAR(100)'), S.a.value('(/H/r)[2]', 'VARCHAR(100)'), S.a.value('(/H/r)[1]', 'VARCHAR(100)') ) AS fileCol
FROM
SELECT *,CAST (N'<H><r>' + REPLACE(filenames, '\', '</r><r>') + '</r></H>' AS XML) AS [vals]
FROM test) d
CROSS APPLY d.[vals].nodes('/H/r') S(a)
drop table test
Maybe you are looking for
-
File type in Spotlight and Finder search
I have several lectures that are all Keynote presentations (both 08 and 09) and wanted to make a smart folder to have them all in one place. However, doing that, I realized that my Keynote files are not listed as "Presentations" but rather as "Docume
-
Report on Foreign currency valuation
Sub:How i can get report from FGCL_FC_VAL Dear gurus, Configuration of foreign currency valuation is done.Only thing is that how i can run said report.I mean what data i should put in said report,so that when i run FGCL_FC_VAL,the system will do auto
-
IPhone 5s decrease memory after updating to iOS 8.3
I notice a decrease on my iPhone 5s 16GB memory from: 16GB to 12GB after downloading the latest iOS 8.3 update. I though I have so much stuff on the phone so I decided to delete all content and reinstall the software to factory settings. No luck, my
-
How to open my iPod touch if I forgot the password
I Forgot the iPod on the iPod touch how can I reset it? Help!
-
Importing pictures into iPhoto and Mac
Hello, I like to store my pictures on an external hard drive that I share with my Mac and other PCs and media sharing (PS3, Wii) consoles. I use the iPhoto to organize my pictures on the Mac by event/faces/places etc. But I do not want to use the iPh