How to use a table of column names in a select statement - information-schema

I have a table that I will add columns to in the future and don't want to update all my queries when they're added. I'm grabbing a set of column names using information_schema.COLUMNS and I want to use those names as the columns in a SELECT statement, but that's where I'm stuck:
select * into #Temp
from information_schema.COLUMNS
where column_name like '%search_string%' and table_name like 'searched_table'
select ...
So what's the syntax to use that table of column names after the SELECT, instead of listing each name individually?

Related

Using multiple select statement inside insert statement in Hive

I'm new in Hive. I have three tables like this:
table1:
id;value
1;val1
2;val2
3;val3
table2
num;desc;refVal
1;desc;0
2;descd;0
3;desc;0
I want to create a new table3 that contains:
num;desc;refVal
1;desc;3
2;descd;3
3;desc;3
Where num and desc are columns from table2 and refVal is the max value of column id in table1
Can someone guide me to solve this?
First, you have to create an table to hold this.
CREATE TABLE my_new_table;
After that, you have to insert into this table, as showed here
INSERT INTO TABLE my_new_table
[PARTITION (partcol1=val1, partcol2=val2 ...)]
select_statement1;
In the select_statement1 you can use the same select you would normally use to join and select the columns you need.
For more informations, you can check here

In sql without insert values in table, how i can see column of table

SQL> create table justlike(customerid varchar(19),first_name varchar(40),last_name varchar(100),Address varchar(50),city varchar(30),pincode varchar(10),state varchar(20));
Just use:
desc <table_name>;
This will print the description of your table columns
in your case:
desc justlike;
You can always check the table definition, in case you are using Oracle, by running below query -
SELECT * FROM USER_TAB_COLS
WHERE TABLE_NAME = 'JUSTLIKE';
OR you can write a select on table itself -
SELECT * FROM JUSTLIKE;

How to only select existing values from oracle?

I have a table with a massive number of columns. So many, that when I do SELECT * I can't even see any values because all the columns fill up the screen. I'd like to do something like this:
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND <THIS COLUMN> IS NOT NULL
Is this possible? Note: VALUE is not a column.
There are so many questions on SO that ask this same question, but they have some bizarre twist, and the actual question is not answered.
I've tried:
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND VALUE NOT NULL
*
Invalid relational operator
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND VALUE <> ''
*
'VALUE': invalid identifier
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND COLUMN NOT NULL
*
Missing Expression
Bonus Questions:
Is there any way to force Oracle to only show one output screen at a time?
Is there a variable to use in the WHERE clause that relates to the current column? Such as: WHERE this.column = '1', where it would check each column to match that expression?
Is there any way to get back your last command in Oracle? (I have to remote into a Linux box running Oracle - it's all command line - can't even copy/paste, so I have to type every command by hand, with a wonky connection, so it's taking an extremely long time to debug this stuff)
If you are trying to find all the non null column values for a particular record you could try an unpivot provided all the columns you are unpivoting have the same data type:
SELECT *
FROM (select * from my_table where name like '%unique value%')
UNPIVOT [include nulls] (col_value FOR col_name IN (col1, col2, ..., coln))
with the above code null values will be excluded unless you include the optional include nulls statement, also you will need to explicitly list each column you want unpivoted.
If they don't all have the same data type, you can use a variation that doesn't necessarily prune away all the null values:
select *
from (select * from my_table where name like '%unique value%')
unpivot ((str_val, num_val, date_val)
for col_name in ((cola, col1, date1)
,(colb, col2, date2)
,(colc, col3, date1)));
You can have a fairly large set of column groups, though here I'm showing just three, one for each major data type, with the IN list you need to have a column listed for each column in your column group, though you can reuse columns as shown by the date_val column where I've used date1 twice. As an alternative to reusing an existing column, you could use a dummy column with a null value:
select *
from (select t1.*, null dummy from my_table t1 where name like '%unique value%')
unpivot ((str_val, num_val, date_val)
for col_name in ((dummy, col1, date1)
,(colb, dummy, date2)
,(colc, col3, dummy)));
Have tried this?
SELECT * FROM my_table WHERE NAME LIKE '%unique name%' AND value IS NOT NULL;
Oracle / PLSQL: IS NOT NULL Condition
For row number:
SELECT field1, field2, ROW_NUMBER() OVER (PARTITION BY unique_field) R WHERE R=1;
Usually in Linux consoles you can use arrow up&down to repeat the last sentence.

How to select multiple column names (not values) in a table when column value is specified-Oracle

I have a table which has around 30 columns.Out of 30 columns i need to retrieve around 25 column names where value is set to some specified value(say 1).
I am not able to find a way to do this.Will multiple if statement work as below
select if columnname1='1' then 'columnname1' else null
if columnname2='1' then 'columnname2' else null from table.
In case the value is not set to 1, i don't want to retrieve the column names.
The below query can give me the column names but i can't specify the value with below query
select DISTINCT COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='table_name'.
Does this work for you?
SELECT COLUMN_NAME
from ALL_TAB_COLUMNS
where TABLE_NAME='TABLE_NAME'
AND INSTR(column_name,'1') >0

how to find source search for a table?

i have a existing table called property i alter a column called overbook_percent (i change a number 2 to 3) but i want to do a source search to make sure that there is not a variable declaration that sets this to just 2 digits. how would i go about this?not sure you called this a source search.
how can i see where this table column is use?
i tried this but get no output
select text
from dba_source
where upper(text) like 'OVERBOOK_MAX'
;
I'm working on toad for oracle
may be you are looking for this:-
select owner, table_name from all_tab_columns where column_name = 'OVERBOOK_MAX';
This will search all tables with a column name OVERBOOK_MAX
For checking in stored procedures you may try this:-
SELECT DISTINCT type, name
FROM dba_source
WHERE owner = 'OWNER'
AND UPPER(text) LIKE '%OVERBOOK_MAX%';
EDIT
to search a string try this:-
SELECT owner, name, type, line, text
FROM dba_source
WHERE instr(UPPER(text), UPPER(:search_string)) > 0;

Resources