Is it possible to return column names?
Hi!
Is it possible to return column names of a select statement?
Let's say I have table with the following columns:
Col1
COl2
Col3
And I have a select statement like this:
Select * from XX
Is it then possible either through SQL or PL/SQL to return the column names?
Br
Casper Thrane
If you are looking at column names for a particular table,
then you can query aganist the sys.User_Tab_Columns.
here is the piece of code.
SELECT Column_Name
FROM Sys.User_Tab_Columns
WHERE Table_Name = 'TABLENAME';
Remember TABLENAME is the name of the table in CAPITAL letters.
You can use this in SQL or PL/SQL code.
You can also use the ALL_Tab_Columns but then you have add
another AND condition, because, the there may be a same table name for different users. so one has to use the owner Column in your query.
SELECT Column_Name
FROM All_Tab_Columns
WHERE Table_NAME = 'TABLENAME'
AND OWNER = 'TABLE_DESIRED_OWNER';
Similar Messages
-
Hi,
how can I make Oracle ODBC Driver (8.01.72.00) returning column name in small letters ?
Thanks for any help.Pardon my stupidity, but I'm still a little confused. Perhaps it's too early in the morning...
If you create a table foo in Oracle, i.e.
create table foo (
col1 varchar2,
col2 integer )
col1 & col2 are stored as uppercase in the database, although queries against the table are case-insensitive. For instance
select col1 from foo;
select COL1 from foo;
select cOl1 from foo;
all return the same thing
If you instead create a table
create table foo (
"col1" varchar2,
"col2" integer )
col1 & col2 are stored as case-sensitive column names, in this case lower case. Because the column names are case-sensitive,
the SQL statement
select "col1" from foo;
will return the correct data, while
select col1 from foo;
will cause an error.
Is this helpful to you? I guess I'm not sure where it is that you're generating or gathering column names, so I'm not sure how much control you have.
If you're gathering column names by making calls to catalog functions like SQLTables, I assume you can simply use the appropriate LOWER() function call to create lowercase column names.
If you can explain in a little more detail, preferrably with reference to the particular ODBC calls you're making, I might be able to help a little more.
Justin -
How to return column name in some table?
Hi All,
I know :system.cursor_value which returns the value of the current text item in the form.
But Is there a way to return the column name and its value in some table?
Note: I'm using Oracle DB 10g
Thank youDid you read the original post? [...] You don't understand what I want and you don't say antthing useful for my goal!
This is the SQL and PL/SQL forum. You need the Mind-Readers' forum down the hall.
First you asked "Is there a way to return the column name and its value in some table?", and were told you can get the former from the data dictionary (you can get the latter from the table itself).
Then you say you "want to create a trigger on a table", which tells us nothing about your problem.
Finally you say "but suppose the table contains 50 columns then I have to write 50 columns names three times", which is essentially true - but the process can be automated. What you (probably) need to do is write queries based on the data dictionary tables that generates the PL/SQL that you ultimately put in your triggger - you then use that output to build the trigger(s).
I don't think there's any way, at run time, to generically fish out all the columns of a table with their :new and :old values - you have to write (or auto-generate) specific code for each table. -
Query to return column name of first NULL column?
I have a table with 40 columns but only the first n have non-null values. Is there a way to pull the name of the first non-null column? I have tried using CASE but I run into nesting too deeply problems (apparently you can only nest to 10 levels).
Thank you.
Kevin
Kevin BurtonOr, you could try something like this:
-- This is just for testing
DROP TABLE #test
CREATE TABLE #Test
(ID INTEGER
,n1 INTEGER
,n2 INTEGER
,n3 INTEGER
,n4 INTEGER
,n5 INTEGER
,n6 INTEGER
,n7 INTEGER
,n8 INTEGER
,n9 INTEGER
INSERT INTO #Test
VALUES (1,256,365,4000,0,NULL,NULL,NULL,NULL,NULL)
SELECT *
FROM #Test
-- THIS IS THE IMPORTANT PIECE
SELECT ISNULL(LEN(LEFT(n1,1)),0)
+ ISNULL(LEN(LEFT(n2,1)),0)
+ ISNULL(LEN(LEFT(n3,1)),0)
+ ISNULL(LEN(LEFT(n4,1)),0)
+ ISNULL(LEN(LEFT(n5,1)),0)
+ ISNULL(LEN(LEFT(n6,1)),0)
+ ISNULL(LEN(LEFT(n7,1)),0)
+ ISNULL(LEN(LEFT(n8,1)),0)
+ ISNULL(LEN(LEFT(n9,1)),0)
FROM #Test
The LEFT(x,1) will always either give you a LEN of 1, or a NULL. Add 'em up. Try it.
Then use the Ordinal_Position from the Information Schema to get the name of the column (if that is what you need)
Duncan Davenport -
Return the column names for which the row values are not null.
Hi i m a new guy to db admin, and i need a sql script which should return column names of the particular table. and the returned column should have value (fyi - if the column has null value column name should not come in the sql o/p).
Exmple:
table name - A
s.no name mark status fee
1 aa 45 p null
2 bb 30 null paid
3 cc 35 p paid
fyi -1) if i give the table name(A) and s.no (2) the o/p should be -- name,mark.
2) if i give the tablename(A) and s.no (1) the o/p should be --- name,mark,status.
Thanks
Krishna.
Edited by: user13294228 on Jun 14, 2010 10:54 PMBTW,
The previous solution is for all values of the column, if you want a specific row, you can add it in where clause.
I mean in your example, it you look like:
SET serveroutput on;
DECLARE
l_cnt NUMBER;
l_str VARCHAR2 (255) := '';
l_table_name VARCHAR2 (255) := 'YOUR_TABLE_NAME';
l_col_cond VARCHAR2 (255) := 'S_NO';
l_val NUMBER := 1;
CURSOR c_col
IS
SELECT column_name
FROM user_tab_columns
WHERE table_name = l_table_name;
BEGIN
FOR i IN c_col
LOOP
EXECUTE IMMEDIATE 'SELECT COUNT ('
|| i.column_name
|| ') FROM '
|| l_table_name
|| ' WHERE '
|| l_col_cond
|| ' = '
|| l_val
INTO l_cnt;
l_str := l_str || CASE
WHEN l_cnt = 0
THEN ''
ELSE i.column_name
END || ',';
END LOOP;
l_str := SUBSTR (l_str, 1, LENGTH (l_str) - 1);
DBMS_OUTPUT.put_line (l_str);
END;Saad,
Edited by: S.Nayef on Jun 15, 2010 11:54 AM -
Decode possible without column name?
Hi,
I am trying to reformat text using decode as follows:
SELECT DECODE('a1a1','a','X','Z') FROM DUAL;
in order to convert 'a1a1' inro 'XZXZ'
However, this didn't work, so I tried:
SELECT DECODE('a1a1' as VARIABLE,'a','X','Z') FROM DUAL;
Any ideas what is the best way to do it?
I can do it using TRANSLATE, but that only applies one substitution, whereas I need two.Decode is possible without a column name but it is looking for exact matches, ie if ('a1a1') = 'a' then 'X' else 'Z'In your example you can use translate:
select translate('a1a1','a1','XZ') from dual;
TRAN
XZXZor a nested translate
select translate(translate('a1a1','a','X'),'1','Z') from dual;
TRAN
XZXZMessage was edited by:
SamB -
Pass column-name as a parameter to reports
Hello,
the code below calls a report. But now I want to sort the rows in the report. For example I have a text-item in my form-modul. If I type a column-name and press the button then the rows should be sorted in the report. Is it possible tp pass column-names as parameter to reports?
DECLARE
repid REPORT_OBJECT;
v_rep VARCHAR2(100);
rep_status VARCHAR2(20);
BEGIN
repid := find_report_object('STATIONSTOPOLOGIE');
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_EXECUTION_MODE,RUNTIME);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_COMM_MODE,SYNCHRONOUS);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_DESTYPE,CACHE);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_DESFORMAT,'html');
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_SERVER,'rep_oracle-dev');
-- SET_REPORT_OBJECT_PROPERTY(repid,REPORT_OTHER,'paramform=no pdeptno='||:dept.deptno);
v_rep := RUN_REPORT_OBJECT(repid);
rep_status := REPORT_OBJECT_STATUS(v_rep);
WHILE rep_status in ('RUNNING','OPENING_REPORT','ENQUEUED')
LOOP
rep_status := report_object_status(v_rep);
END LOOP;
IF rep_status = 'FINISHED' THEN
WEB.SHOW_DOCUMENT('http://oracle-dev:8888/reports/rwservlet/getjobid'||
substr(v_rep,instr(v_rep,'_',-1)+1)||'?'||'server=rep_oracle-dev','_blank');
ELSE
message('Error when running report');
END IF;
END;Hi,
the work has been done in reports. You can use a lexical parameter in reports to add a condition for sorting to the query like:
select .. from .. where ... &p_order.
Then add another parameter to the report (for example p_param). Fill p_param via your interface in forms (SET_REPORT_OBJECT_PROPERTY(repid,REPORT_OTHER, ....) with the column name. Then build a report trigger like:
if :p_param is null
then
:p_order:= null;
else
:p_order:= 'order by '||:p_param;
end if;
But have a look, that p_param can only get correct values.
Rainer -
How to pass column name in slect statement in query
hi,
i want to make a report where in query select statement using variable as a column name. but its not working plz guide me how can i do this.
i have created a function which return column name through variable & that variable i want to to use in select statement
select :m1 from table1;
regardsHi,
Create a user parameter (say P_field), and assign a valid field name as initial value (say NAME), And In the Query, write
SELECT CODE, &P_field FN_FIELD FROM <table_name> WHERE <condition>And in the BEFORE PARAMETER FORM Trigger under the Report Triggers, write,
function BeforePForm return boolean is
begin
:P_field := <your_function_call>;
return (TRUE);
end;And use that FN_FIELD field in the report.
Hope this will clear your issue.
Regards,
Manu. -
Hi all,
I am getting table name those tables had primary key from this Query:
SELECT TABLE_NAME FROM ALL_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'P'
ORDER BY TABLE_NAME
I need column name also is it possible to get column name also.
ThanksPlease, use the same answer that was already given in three other threads of yours. It starts to get annoying to hear the same question again and again.
-
ResultSetMetaData.getColumnLabel(int coluN) gives column name with quote
I am using ojdbc14.jar for "Oracle Database 10g Enterprise Edition Release 10.2.0.3.0". My query is "Select 'column name' from dual". When i get column name using ResultSetMetaData.getColumnLabel(int coluN) it return column name including single quotes ie: for the above query result is 'columname'. Ideally it should not return ' (single quote).
Why do this happening?You will have that even when you run with sqlplus.
=
Ashok -
Passing Column name(String) in setting CallableStatement
Is it possible to pass Column Name while setting the CallableStatement while using drivers for JDK 1.4, if yes how do I do it.
Thanks!You don't necessarily need to use dynamic SQL;
create or replace procedure p
p_a number default null,
p_b number default null,
p_c number default null
) is
begin
insert into t
values
(p_a, p_b, p_c);
end;
Procedure createdUsing named notation;
exec p(p_a =>1, p_c => 3);
PL/SQL procedure successfully completed
exec p(p_b =>20, p_c => 30);
PL/SQL procedure successfully completedPassing NULLs;
exec p(4, null, 6);
PL/SQL procedure successfully completed
exec p(40, 50, null);
PL/SQL procedure successfully completed
select * from t;
A B C
1 3
20 30
4 6
40 50 -
Search for column name in DB tables
Hi Guys
Is it possible to search column name when we do not know the corresponding table in the databse
Cheers
shabarYou can use the data dictionary to search though a list of all column names. The <strike>table</strike> data dictionary view is ALL_TAB_COLUMNS.
Lets assume you want to search for ID columns:
example
select owner, table_name, column_name
from all_tab_columns
where column_name = 'ID';Edited by: Sven W. on Jun 21, 2011 1:47 PM -
Using Define Statement as Column Name Alias
Hi,
Is it possible to assign a defined variable as a column name in a simple select statement?
For examples -
DEFINE test_case = to_date('01-Jan-2011','DD-Mon-YYYY');
SELECT banana &&test_case
FROM dual;
Which would result in -
01-Jan-2011
banana
Is this at all possible? The column names will change when the date changes so I do not want to just label the columns normally as this would need to be changed every time the script is run for a different month.Cameron wrote:
DEFINE test = to_char(SYSDATE,'Mon')
SELECT hello &&test
FROM dual
So should that result in -
June
hello
Becuase all I get is a fail and -
15:34:37 ORA-00923: FROM keyword not found where expectedClearly you missed sybrands response.
DEFINE is a SQL*Plus command (SQL*Plus is a GUI tool)
to_char and SYSDATE are SQL functions (SQL is a language)
You cannot use SQL directly in SQL*Plus commands like DEFINE. -
Using Column Name returned by function in SELECT statement
Hi
Output from my function (RETURN data type is VARCHAR2) is column name. I want to use it directly in my SELECT statement. Below is simplified example of this:
--- Function
CREATE OR REPLACE FUNCTION simple RETURN varchar2 IS
BEGIN
RETURN ‘my_column’;
END simple;
--- Select
SELECT simple FROM my_table;
This does not work. It seems that output from function is passed in quotation i.e.
SELECT ‘my_column’ FROM my_table;
So the output from SELECT is a list of rows populated with values my_table:
COLUMN simple
ROW1 my_column
ROW2 my_column
ROW3 my_column
Can please someone help me with this?I'm not sure I got you right.
In standard SQL everything must be known at compile time. If not dynamic SQL is required, but is a costly operation (usually requires parsing before each execution) so it should better not be used when standard SQL can do it.
I provided a design time example where a function returns the column name from the given the table name and column id for a varchar2 column data type to make things simple. Then a query string is constructed and executed dynymically to return all column values of the chosen table_name.column_name.
SELECT simple FROM my_tableAt compile time the simple function return value is unknown (any varchar2 value would do) you already find out you get the (same) return value (i.e column name) for each table row => dynamic SQL needed to get the column values
The purpose of function would be to rename all columns for provided table.The table name would be provided, right? If yes => dynamic SQL
What is the function supposed to return the column name (where would the new name come from?), the column alias (which table column would be renamed to the new name?)
The user could use the new_column name as the column alias name submitting the query.
Is it possible to do this?Maybe () using a pipelined function (different data types - number,date, ... not cosidered yet) but your simple query;
<tt>SELECT simple FROM my_table</tt>
might look like:
<tt>select my_column_value new_column_name from table(get_column_value(table_name,column_name))</tt>
Sorry, no Database at hand to provide a specific example.
Regards
Etbin -
Column name of custom columns is possible of translating automatic
In SharePoint 2013 is possible of translating automatic the column name of custom columns with machine translation services or it is necessary create resource files or source site and target site?
Hi,
When you create a column, the name you enter is just a constant string. SharePoint does not do translation based on known words. Internationalization is based on resource files.
Here are the steps I would take for creating international-able columns through the browser:
Create the column with the name you want as the internal name (ie, no spaces or symbols to avoid
_x0020_ and other encoding replacements).
Edit the column by changing the column name to a resource string that you know if defined in each language you want to support.
Here are similar posts, you can use as a reference:
https://social.msdn.microsoft.com/Forums/sqlserver/pt-BR/06742b53-ccc9-4d2c-ac61-fbba37e60be1/how-to-translate-custom-column-names-in-the-target-variation-site-in-sharepoint-2013?forum=sharepointdevelopment
http://sharepoint.stackexchange.com/questions/30226/how-to-display-column-names-in-several-languages-in-sharepoint
Best Regards,
Lisa Chen
TechNet Community Support
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact
[email protected]
Maybe you are looking for
-
How to deactivate a license from a dead hard drive
how to deactivate a license from a dead hard drive
-
Hi, I have the dvd for adobe creative suite 5.5 web premium, I see a set-up.exe for windows, but how do I install this software on my macbook please? Thank you E
-
Just upgraded to OSX 10.9 mavericks on macbook pro mid 2011 version. Having a big issue here that whenever my mac comes back from sleep, i.e after I open the lid, the screen flickers and there is an error of Finder not opening properly. Any fix to th
-
I have followed the Upload and Download Files in an Application How To. I can upload files into the application but I cannot download. When I click on the download link, the full URL appears in the address bar at the top of the screen, but the file d
-
Calling SQL Server function in ODI
Hi All, In the Target data store in the interface I have a column called COUNT_BY_CERTDATE column and in the mapping I have teh following syntax, which does not work. SUM(CASE WHEN ((dbo.getcertdate(person_id,getdate()) >= current_timestamp) THEN 1 E