Table name decided at runtime - How to do a select?
Hi,
Searching these forums I found how to do a select * from where the table name is decided at runtime. You use paranthesis on the variable containing the table name.
However, I still get syntax error I can't solve and need some help.
This syntax doesnt work, could you help me find out what is the problem?
SELECT tab1~field1 tab2~field2
FROM ( (dynamic_tab_name1) AS tab1 INNER JOIN
(dynamic_tab_name2) AS tab2 ON tab1~someIndex = tab2~someIndex )
dynamic_tab_name1 and dynamic_tab_name2 are defined as two strings where I concatenate the table prefix name with the system client number.
The syntax works if i replace the dynamic_tab_name1 and dynamic_tab_name2 with hardcoded table names.
thanks & regards
Edited by: Baran Sölen on Feb 5, 2009 1:44 PM
Well you can generate your program dynamically in the following way.
But I never like to use the method.
append 'PROGRAM SUBPOOL no standard page heading.' to code.
append 'form dyn1.' to code.
append 'data: begin of i_tab occurs 0,' to code.
append ' kolnr like t682i-kolnr,' to code.
append ' gstxt like tmc1t-gstxt,' to code.
append ' kschl like t685-kschl,' to code.
append ' vkorg like komg-vkorg,' to code.
append ' vtweg like komg-vtweg,' to code.
append ' matkl like komg-matkl,' to code.
append ' matnr like komg-matnr,' to code.
append ' inco1 like komg-inco1,' to code.
append ' inco2 like komg-inco2,' to code.
append ' charg like komg-charg,' to code.
append ' vkbur like komg-vkbur,' to code.
append ' vkgrp like komg-vkgrp,' to code.
append ' zsales like komg-zsales,' to code.
append ' end of i_tab.' to code.
append 'data: begin of i_t682 occurs 0,' to code.
append ' kolnr like t682i-kolnr,' to code.
append ' kotabnr like t682i-kotabnr,' to code.
append ' end of i_t682.' to code.
data: v_table_name(16),
v_condition_type_code(72),
v_kolnr like t682i-kolnr.
concatenate 'kschl = ''' p_kschl '''.' into v_condition_type_code.
loop at i_t682.
concatenate 'A' i_t682-kotabnr into v_table_name.
append 'select * from' to code.
append v_table_name to code.
append 'appending corresponding fields of table i_tab' to code.
append 'where kappl = ''V'' and' to code.
append v_condition_type_code to code.
append 'if sy-subrc = 0.' to code.
append 'loop at i_tab where kolnr = '' ''.' to code.
concatenate 'i_tab-kolnr = ' i_t682-kolnr '.' into code separated by space.
append code. clear code.
append 'i_tab-gstxt = ''Description will be put later''.' to code.
append 'modify i_tab.' to code.
append 'endloop.' to code.
append 'endif.' to code.
endloop.
append 'export i_tab to memory id ''i_tab''.' to code.
append 'endform.' to code.
generate subroutine pool code
name prog
message msg
line lin
word wrd
offset off.
*break-point.
if sy-subrc = 0.
perform dyn1 in program (prog).
endif.
import i_tab from memory id 'i_tab'.
Similar Messages
-
Retrieve data from oracle table, table name passed in runtime into JSP
Hello All,
I am new to JSP, i have a requirement,
I need to retrieve data from oracle table, here table is passed at random. how to get the data displayed in JSP page.
can any one help me in that
thanks1) Learn SQL.
2) Learn Java.
3) Learn JDBC.
4) Learn DAO.
5) Learn HTTP.
6) Learn HTML.
7) Learn JSP/Servlet.
8) Learn JSTL.
9) Apply learned things and develop.
Whenever you stucks, please come back and post the specific coding/technical problem here. -
How to use table name dynamically in report procedure
Hello every body
I want to use table name dynamically means at runtime i want to pass table name.
I can do this by lexical parameter in main query.
But my problem is that i want to crate new format trigger or new function that use 1 cursor in which that lexical parameter : table name is used.
so how to do that?
i can not use that lexical parameter it is giving error.
please help me how to do that.Call a database function which you pass the lexical parameter. Then in the database function you use dynamic sql or the execute immediate option to build the SQL string.
Return the information from this function to build your business logic on in your format trigger.
Marcos -
How get all table name from database
hi master
sir
how get all table name from databaseThe big question is 'why'.
Selecting from view 'dba_tables' will indeed give the list of all tables in the database, but that includes the dictionary tables and the internal tables, and many others that are probably not of interet to a person who needs to ask this question. Besides, the dba_tables view requires access to a DBA account.
There are several other views: "user_tables" will list all the tables in this user's schema; and "all_tables" will list all the tables this user can access in some way.
The above do not, of course, include any information about synonyms, sequences, views, indexes and so on.
The correct answer and the meaningful answer may be two different things. -
How to set dynamic table name in sql query?
I want set dynamic table name by parameter in sql query,just like:
select * from :tbname
but run report is error,BI P report table name is invalidation.
What can i do? Thanks!Hi,
that's only possible inside a data template with a lexical parameter.
Regards
Rainer -
Creating a form with variable table name(s)
Hi,
I am building a form that will allow user(s) switch to other user
(s) dynamically. I have been able able to do this by creating a
non-database table and use record group to read from the base
table of each user.(All the tables in question have the same
structure).
Record group is a read only, and I want to be able to do data
maninpulation with this form.
The questions are
(1) It possible to do data manipulation with record group and if
it is, how?
(2) Is it possible to build a form with a variable table name?
if possible, how?
Somebody pls help.
Thanks,
Olutunde.
nullOlutunde Babarinsa (guest) wrote:
: Hi,
: I am building a form that will allow user(s) switch to other
user
: (s) dynamically. I have been able able to do this by creating a
: non-database table and use record group to read from the base
: table of each user.(All the tables in question have the same
: structure).
: Record group is a read only, and I want to be able to do data
: maninpulation with this form.
: The questions are
: (1) It possible to do data manipulation with record group and
if
: it is, how?
: (2) Is it possible to build a form with a variable table name?
: if possible, how?
: Somebody pls help.
: Thanks,
: Olutunde.
Hi,
you can create and manipulate record groups at runtime(see Forms
Reference 'Create_Group' and 'Add_Group_Row') don't use
Create_Group_from_Query. For your purpose it's better to build a
cursor loop on your Query and add your datas after
manipulating with 'Add_Group_Row' to your Record Group.
It's possible to SET_BLOCK_PROPERTY(QUERY_DATA_SOURCE_NAME) but
it's not possible to change the item property 'Column Name'.
Therefor I would suggest to build a non database block and
populate these block by a programm unit, which works with a
pl/sql cursor loop and the 'create record' to populate these
block.
null -
Help passing table name as parameter to a procedure
Hello,
i'm trying to write a procedure that takes a table name as input and uses a cursor to select a column,count(1) from the passed table to the cursor. The procedure i've come up with is as follows,
CREATE OR REPLACE
PROCEDURE excur(
p_tbl user_tables.table_name%type )
AS
type rc is ref cursor;
c rc;
res BOOLEAN;
BEGIN
open c for 'SELECT columnA,COUNT(1) FROM'|| p_tbl||';';
close c;
END excur;When i try to execute it, an error pops up informing that a table cannot be used in this context. As in i cannot pass a table name as an argument to a procedure. Kindly guide as to how to solve this situation.vishm8 wrote:
Hello,
i'm trying to write a procedure that takes a table name as input and uses a cursor to select a column,count(1) from the passed table to the cursor. The procedure i've come up with is as follows,
CREATE OR REPLACE
PROCEDURE excur(
p_tbl user_tables.table_name%type )
AS
type rc is ref cursor;
c rc;
res BOOLEAN;
BEGIN
open c for 'SELECT columnA,COUNT(1) FROM'|| p_tbl||';';
close c;
END excur;When i try to execute it, an error pops up informing that a table cannot be used in this context. As in i cannot pass a table name as an argument to a procedure. Kindly guide as to how to solve this situation.Generally speaking, Dynamic code is a bad idea for a staggering number of reasons.
That aside, what do you want to return? You're selecting a column and applying an aggregate function (count) but you're not grouping, that doesn't usually work out too well.
TUBBY_TUBBZ?select owner, count(*) from all_objects;
select owner, count(*) from all_objects
ERROR at line 1:
ORA-00937: not a single-group group functionWhy do you perceive the need to be able to take in ANY table name, can't you design an application where table names are known at compile time and not run time? -
Pass table name as parameter in prepared Statement
Can I pass table name as parameter in prepared Statement
for example
select * from ? where name =?
when i use setString method for passing parameters this method append single colon before and after of this parameter but table name should be send with out colon as SQL Spec.
I have another way to make sql query in programing but i have a case where i have limitation of that thing so please tell me is it possible with prepared Statment SetXXx methods or not ?
Thanks
Haroon Idrees.haroonob wrote:
I know ? is use for data only my question is this way to pass table name as parameterI assume you mean "how can I do it?" As I have already answered "is this the way?" with no.
Well, I would say (ugly as it is) String concatenation, or stored procedures. -
How to freeze the selection column in the table control of the module pool.
hi ,
in my module pool there is a row selection field <b>w/selcolumn</b> of the table control called as mark.
how to freeze the selection column where there is no record in the table control row.
or in other words where wa is initial.
thanks
ektaHi all,
in the PBO of the screen the following code is written.
say the selection column is MARK and is declared in the data as well.
thanks
ekta
*************************C O D E **************************************************
MODULE disp_tabctrl1 OUTPUT.
IF flag_c = 1.
READ TABLE it_create_data INTO wa_material_data
INDEX tab_ctrl1-current_line.
ELSE.
READ TABLE it_material_data INTO wa_material_data
INDEX tab_ctrl1-current_line.
IF sy-subrc = 0.
IF ok_code_0101 = '&SEL1'.
mark = 'X'.
ELSEIF ok_code_0101 = '&DSEL'.
mark = ' '.
ENDIF.
ELSE.
LOOP AT SCREEN.
IF screen-name = 'MARK'.
screen-input = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.
index_t = tab_ctrl1-top_line.
index_d = tab_ctrl1-top_line + n.
ENDMODULE. " DISP_TABCTRL1 OUTPUT -
Table(): invalid table name
The following line is a part of a PLSQL procedure which takes object_reference as an argument.
delete from table(deref(object_reference).nested_table) where column_value = 'something';
Error: invalid table name. Why? How do I accomplish this correctly?Oracle 10.2 . Any table. It doesn't matter. Or does it?
I'm trying to query against a nested table (also called collection) of an object to which I have a reference. This object may be in any (object) table. A more elaborate example:
create type item_ty (
code number
create type items_ty as table of item_ty;
create type box (
items items_ty,
member procedure put_item(code number),
member function take_item(code number)
create type body box is
begin
member procedure put_item(code number) is
begin
insert into table(deref(self).items) item_ty(code); -- ERROR: invalid table name
end;
member procedure take_item(code number) is
begin
delete from table(deref(self).items) where column_value = code; -- ERROR: invalid table name
end;
end; -
Select data from all the table names in the view
Hi,
"I have some tables with names T_SRI_MMYYYY in my database.
I created a view ,Say "Summary_View" for all the table names
with "T_SRI_%".
Now i want to select data from all the tables in the view
Summary_View.
How can i do that ? Please throw some light on the same?
Thanks and Regards
Srinivas CheboluSrinivas,
There are a couple of things that I am unsure of here.
Firstly, does your view definition say something like ...
Select ...
From "T_SRI_%"
If so, it is not valid. Oracle won't allow this.
The second thing is that your naming convention for the
tables suggests to me that each table is the same except
that they store data for different time periods. This would be
a very bad design methodology. You should have a single
table with an extra column to state what period is referred to,
although you can partition it into segments for each period if
appropriate.
Apologies if i am misinterpreting your question, but perhaps
you could post your view definition and table definitions
here. -
In forms i want to give variable as table name.
e.g When_button_press
Insert into :txt1 values (select * from abc1)
return error on :txt1.
how can i do this.There are examples in the forum that show how. Here are some of them:
CAN I PASS A TABLE NAME AS A VARIABLE IN THE FROM CLAUSE?
DML issue in Oracle forms
Re: How to include variable on block's query
How to use table name as variable -
Dynamic SQL : passing table name as parameter
Hi
I have a SQL query (a store procedure ) that i want to convert to PLSQL
This is a part of my SQL query that i am trying to to find a solution for it, because i cant convert it to oracle :
DECLARE lookupTableRow CURSOR FOR
SELECT TableName FROM SYS_LookUpTable
OPEN lookupTableRow
FETCH NEXT FROM lookupTableRow INTO @tableName
WHILE @@FETCH_STATUS=0
BEGIN
SET @sql='SELECT * FROM '+@tableName
EXECUTE sp_executesql @sql
IF @counter=0
BEGIN
INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table', @tableName)
END
ELSE
BEGIN
INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table'+CONVERT(NVARCHAR(10),@counter), @tableName)
END
SET @counter=@counter+1
FETCH NEXT FROM lookupTableRow INTO @tableName
END
CLOSE lookupTableRow
DEALLOCATE lookupTableRow
As i understand i can't use ORACLE dynamic sql (execute immediate) when the table name is a parameter
Furthermore when i execute this dynamic query in my SQL store procedure each SELECT statement return me as a result the relevant table rows , those result are different in each loop .
So i cant do this too with ORACLE dynamic sql .
Please advice for any solution
* how can i use dynamic sql with table name as parameter ?
* how can i use a "dynamic" cursor, in order to be able to display the dynamic results ?
Thanks for the adviceHi,
b003cf5e-e55d-4ff1-bdd2-f088a662d9f7 wrote:
Hi
I have a SQL query (a store procedure ) that i want to convert to PLSQL
This is a part of my SQL query that i am trying to to find a solution for it, because i cant convert it to oracle :
DECLARE lookupTableRow CURSOR FOR
SELECT TableName FROM SYS_LookUpTable
OPEN lookupTableRow
FETCH NEXT FROM lookupTableRow INTO @tableName
WHILE @@FETCH_STATUS=0
BEGIN
SET @sql='SELECT * FROM '+@tableName
EXECUTE sp_executesql @sql
IF @counter=0
BEGIN
INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table', @tableName)
END
ELSE
BEGIN
INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table'+CONVERT(NVARCHAR(10),@counter), @tableName)
END
SET @counter=@counter+1
FETCH NEXT FROM lookupTableRow INTO @tableName
END
CLOSE lookupTableRow
DEALLOCATE lookupTableRow
As i understand i can't use ORACLE dynamic sql (execute immediate) when the table name is a parameter
Furthermore when i execute this dynamic query in my SQL store procedure each SELECT statement return me as a result the relevant table rows , those result are different in each loop .
So i cant do this too with ORACLE dynamic sql .
Please advice for any solution
* how can i use dynamic sql with table name as parameter ?
* how can i use a "dynamic" cursor, in order to be able to display the dynamic results ?
Thanks for the advice
I have a SQL query (a store procedure ) that i want to convert to PLSQL
I doesn't help when you use one term to mean another thing.
SQL is a language used in both Oracle and other products, such as Microsoft's SQL Server. I don't know much about SQL Server, but Oracle (at least) doesn't support stored procedures in SQL itself; they have to be coded in some other language, such as PL/SQL.
As i understand i can't use ORACLE dynamic sql (execute immediate) when the table name is a parameter
If the table name is a parameter (or only known at run-time for any reason), that's exactly the kind of situation where you MUST use dynamic SQL.
The number of columns that a query produces (and their datatypes) is fixed when you compile a query, whether that query is dynamic or not. If you have multiple queries, that produce result sets with different numbers of columns, then you can't combine them into a single query. The best you can do with one query is to add NULL columns to some of the queries so they all produce the same number of columns.
If you're just displaying the results, there might not be any reason to combine separate result sets. Just display one result set after another.
Whenever you have a question, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ: https://forums.oracle.com/message/9362002 -
Dynamic Select query is failing with error "Invalid Table Name"
OPEN rc FOR 'SELECT count(*) from :s' USING tab_name;
fetch rc into rec_count;
CLOSE rc;
my requirement is to build dynamic select query to retrieve the total count of rows in each table ( variable tab_name contains the table_name )
But I am getting stuck by this errror, not sure if there is any alternative !
ORA-00903: invalid table name
ORA-06512: at line 43OPEN rc FOR 'SELECT count(*) from '||tab_name;
fetch rc into rec_count;
CLOSE rc;
-- This will work
1. Create a sql statement.
2. Open ref cursor for that statement. -
SQL or PL/SQL : dynamically insert table name in a SQL Statement
Hi,
We have a strange requirement - we need to dynamically use the table names in a SQL Query. E.g:
select * from :table_name
The table_name will be chosen from a list. I have tried this in SQL as well as PL SQL - but, I have been unsuccessul so far.
Can you guys please help me solve this puzzle ?
I hope I have explained my quesion clearly - if not, please do let me know if some more details are necessary.
Regards,
RamkyThe following is the anonymous block that im using in a report in HTMLDB. My problem is Line Number 9. The bind variable contains the chosen table name at
the run time.
Variable "qry_stmt" contains the query to be returned, so that result set for that query will be displayed in the report.
If I hard code the table name(rather that passing it through bind variable) in the
qry_stmt string, Im getting the result sets for that query. But if I pass through
bind variable at run time, its still generating the string correctly( im printing
using a print statement at line number 14). But its returing the following report
error
report error:
ORA-01403: no data found
Please advice/help me in this.....
declare
qry_stmt varchar2(1000);
p_table varchar2(30) := 'EMP';
P_ENAME varchar2(1000);
begin
IF :p2_TABLE_NAMES IS NOT NULL THEN
qry_stmt := 'select * from '||TRIM(:P2_TABLE_NAMES); -- Line Num 9
execute immediate qry_stmt; --into P_ENAME;
ELSE
qry_stmt := 'SELECT 1 FROM dual ';
END IF;
htp.p(qry_stmt);--Line Num 14
return qry_stmt;
EXCEPTION WHEN NO_DATA_FOUND THEN
NULL;
end;
Thanks and Regards,
Ramky
Maybe you are looking for
-
Yosemite, Filevault, I accidently turned it on?? Help..
I didnt know what filevault is and I just turned it on without knowing what it is doing. It says that it is encrypting something and its going to take 1 day???? its using a lot of power and my hdd is making anoyying crackling noises. Its not loud.. I
-
You Tube will not play in Safari 3.0.4
I am not able to watch You Tube videos in their entirety 95% of the time. Occasionally, I have been able to view a complete video, but most of the time I can only watch 5-10 seconds before the video stops. I have tried emptying the cache, but this ma
-
I cannot drag files on my computer
So recently, I tried to drag a file from my dektop to my trash but it would not work. I cannot drag folders, or any other type of file either, but I can draw in Photoshop, make selections by dragging, etc...
-
SQL Datamodeling: importing sql
Hello, I'm importing a ddl file to sql datamodeler. Columns, pks and uks are imported ok but the foreign keys are not imported allthough they are in the script. The script was created from sql developer. fragment: CREATE TABLE "DARWIN"."DUS_ALLOCATIE
-
New connections stop working after 31 hours of computer uptime.
I have been using a pppoe connection since I installed archlinux in 2006. This works very well. a network cable is connected to my computer and I use it to dial a pppoe connection. I never had any problems with it. But a while ago, I thought I would