get result from select statement put into header - oracle

I have problem:
- I have select statement
Select name from org where id = 123
I can get value from statement that puting into header
ex: result is "aread" then header column is "aread"
Thanks!

Possible duplicate:
Oracle - dynamic column name in select statement

Related

BIRT allow user to dynamically select report's columns

I want to add an option for the user when creating the report to select the columns that the report will show. See the attached image below on how it is suppose to look.
Is there a way to do that?
I don't know about the parameter dialog, but assuming that your column names are in an array.
You can have an SQL query with all possible column names
(probably you should use special comments to mark the beginning and end of the select list).
E.g.
select
'X' as dummy
-- BEGIN COLS
, column1
, column2
...
-- END COLS
from ...
where ...
order by ...
Then, in the beforeOpen event of the query, you can access and modify the query with this.queryText (IIRC) and remove all those lines ("," + columnname) in the marked part for which columnname is not contained in the array.

How do I get N rows in Oracle SQL SELECT statement given number by variable?

In Oracle SQL, I have a variable that is the number of rows to fetch, I can only use SQL SELECT statement, so no PL/SQL block.
If the variable has a value set I must fetch the number of rows in the variable, if not then fetch as many rows as possible (infinite).
I tried:
select * from system_options
THEN FETCH FIRST
CASE :lim
THEN :lim
ELSE 9999
END
ROWS ONLY
This gives me a ORA-00933: SQL command not properly ended.
Another option would be by not having the variable set then not having the THEN FETCH statement.
You have several syntax errors:
You do not need the THEN before the FETCH FIRST; and
The CASE expression is missing a WHEN clause.
I don't have SQL/Plus to hand to test using a bind variable but something like this:
select * from system_options
FETCH FIRST CASE WHEN :lim IS NOT NULL THEN :lim ELSE 9999 END ROWS ONLY
Or, you can use COALESCE:
select * from system_options
FETCH FIRST COALESCE( :lim, 9999 ) ROWS ONLY
db<>fiddle here

How can i limit the output of select statement for a varchar column in Oracle?

i have a table in which i store the information (product id and description ) of all my products, description column is of type VarChar2(200). i want to format the output of this column in select statement to only result me specific part of output string. E.G Here is my simple select statement:
Select PRODUCTId, PRODUCT_DESC From ProductTable Order By PRODUCTId Desc;
this statement result me the output as:
ProductId Product_Desc
1 Oxford English-Oxford-Oxford Press-Textbook
now i want only the specific part of the output result from product_description column. i have already checked Trim() function but that did not helped me. can someone help me?
A substring function may help.
SELECT SUBSTR('ABCDEFG',3,4) "Substring"
FROM DUAL;
You can use SUBSTR() function. You can provide start and end position for the product_desc column.
The query should be like:
Select product_id,substr(product_desc,2,4) from producttable;
Here you'll get 4 chars from the second one.

pl-sql include column names in query

A weird request maybe but. My boss wants me to create an admin version of a page we have that displays data from an oracle query in a table.
The admin page, instead of displaying the data (query returns 1 row), needs to return the table name and column name
Ex: Instead of:
Name Initial
==================
Bob A
I want:
Name Initial
============================
Users.FirstName Users.MiddleInitial
I realize I can do this in code but would rather just modify the query to return the data I want so I can leave the report generation code mostly alone.
I don't want to do it in a stored procedure.
So when I spit out the data in the report using something like:
blah blah = MyDataRow("FirstName")
I can leave that as is but instead of it displaying "BOB" it would display "Users.FirstName"
And I want to do the query using select * if possible instead of listing all the columns
So for each of the columns I am querying in the * , I want to get (instead of the column value) the tablename.ColumnName or tablename|columnName
hope you are following- I am confusing myself...
pseudo:
select tablename + '.' + Columnname as WhateverTheColumnNameIs
from Table1
left join Table2 on whatever...
Join Table_Names on blah blah
Whew- after writing all this I think I will just do it on the code side.
But if you are up for it maybe a fun challenge
Oracle does not provide an authentic way(there is no pseudocolumn) to get the column name of a table as a result of a query against that table. But you might consider these two approaches:
Extract column name from an xmltype, formed by passing cursor expression(your query) in the xmltable() function:
-- your table
with t1(first_name, middle_name) as(
select 1,2 from dual
), -- your query
t2 as(
select * -- col1 as "t1.col1"
--, col2 as "t1.col2"
--, col3 as "t1.col3"
from hr.t1
)
select *
from ( select q.object_value.getrootelement() as col_name
, rownum as rn
from xmltable('//*'
passing xmltype(cursor(select * from t2 where rownum = 1))
) q
where q.object_value.getrootelement() not in ('ROWSET', 'ROW')
)
pivot(
max(col_name) for rn in (1 as "name", 2 as "initial")
)
Result:
name initial
--------------- ---------------
FIRST_NAME MIDDLE_NAME
Note: In order for column names to be prefixed with table name, you need to list them
explicitly in the select list of a query and supply an alias, manually.
PL/SQL approach. Starting from Oracle 11g you could use dbms_sql() package and describe_columns() procedure specifically to get the name of columns in the cursor(your select).
This might be what you are looking for, try selecting from system views USER_TAB_COLS or ALL_TAB_COLS.

Update One table Column with Values from Another table Having Similar

Hi Guys I have Two tables (MIGADM.CORPMISCELLANEOUSINFO and CRMUSER.PREFERENCES) and Each Has a field called PREFERENCE_ID and ORGKEY. I want to Update the Preference ID for MIGADM.CORPMISCELLANEOUSINFO with Preference_ID from CRMUSER.PREFERENCES for Each Corresponding ORGKEY. SO I wrote this Query;
update migadm.CORPMISCELLANEOUSINFO s set s.PREFERENCE_ID = (
select e.PREFERENCE_ID from crmuser.preferences e where s.ORGKEY = e.ORGKEY)
But I get:
ORA-01427: single-row subquery returns more than one row
What Should I do?
It means the columns you have selected are not unique enough to identify one row in your source table. Your first step would be to identify those columns.
To see the set of rows that have this problem, run this query.
select e.origkey,
count(*)
from crmuser.preferences e
group by e.origkey
having count(*) > 1
eg : for origkey of 2, let's say there are two rows in the preferences table.
orig_key PREFERENCE_ID
2 202
2 201
Oracle is not sure which of these should be used to update the preference_id column in CORPMISCELLANEOUSINFO
identify the row where the subquery returns more than one row (You could use REJECT ERROR clause to do it for instance) or use the condition 'where rownum = 1'.

Resources