How does SELECT 'TABLE_NAME' AS TEMP work? - sql

I found this (working) statement in some code I maintain:
SELECT 'TABLE_NAME' AS TEMP
FROM TABLE_NAME
WHERE ROWNUM = 1
and while I am familiar with the AS use for table columns, e.g.
USE mydatabase;
SELECT day_of_order AS "Date",
customer As "Client",
product,
quantity
FROM orders;
I couldn't find documentation for using the entire table's name as a column.
How does this work? and what would be the intended use of such statement?

It just looks like they're selecting a literal string of the table name, and put it in a column called TEMP. It will only return one row because of the WHERE ROWNUM = 1. They could have just as easily done:
SELECT 'TABLE_NAME' AS TEMP FROM DUAL
As for what you'd use it for... that's a good question. :)

Related

Query to find the column names of a table using BIgquery

I am working on project_name='Alpha' and dataset_name = 'Beta' and this dataset contains numerous tables. I want to get table details i.e, columns of a particular table i.e, table_name = 'Gamma'
To retrieve meta data information. you can use INFORMATION_SCHEMA.
Try with below query..
SELECT column_name FROM `project_name.dataset_name.INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = <'Table Name'>;
For your solution, You can use INFORMATION_SCHEMA.COLUMNS
First, try with below:
SELECT * FROM Alpha.Beta.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Gamma' ;
Then you can select the details you want, only for the column name you can use below;
SELECT column_name FROM Alpha.Beta.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Gamma' ORDER BY ordinal_position asc ;
Please do let me know if it doesn't work out but flag it as an answer if it works.
EDIT: if it didn't work, can you please try putting Alpha.Beta.INFORMATION_SCHEMA.COLUMNS between escape character

SQL Match table and multiple columns

I wanted to know a Query where in which i want to locate a specific table that contains two or more columns:
I tried:
SELECT *
FROM DB
WHERE TableName = 'TableName'
AND ColumName in('column1' , 'column2')
But this query will look if any of those columns are there, but i want it to return only if all of them are a match.
I hope this questions makes sense.
This should work for you in MySQL:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('column1','column2')
AND TABLE_SCHEMA='your_database'
GROUP BY table_name
HAVING COUNT(COLUMN_NAME) =2;
The operator IN is a concatenation of OR. However, there's no way to creare a short concatenation of AND as well.
See this question.

one query for many similar tables

I have an Oracle database with many tables that have identical structure (columns are all the same). The table names are similar also. The names of the tables are like table_1, table_2, table_3...
I know this isn't the most efficient design, but I don't have the option of changing this at this time.
In this case, is it possible to make a single sql query, to extract all rows with the same condition across multiple tables (hundreds of tables) without explicitly using the exact table name?
I realize I could use something like
select * from table_1 UNION select * from table_2 UNION select * from table_3...select * from table_1000
But is there a more elegant sql statement that can be run that extracts from all matching table names into one result without having to name each table explicitly.
Something like
select * from table_%
Is something like that possible? If not, what is the most efficient way to write this query?
You can use dbms_xmlgen to query tables using a pattern, which generates an XML document as a CLOB:
select dbms_xmlgen.getxml('select * from ' || table_name
|| ' where some_col like ''%Test%''') as xml_clob
from user_tables
where table_name like 'TABLE_%';
You said you wanted a condition, so I've included a dummy one, where some_col like '%Test%'.
You can then use XMLTable to extract the values back as relational data, converting the CLOB to XMLType on the way:
select x.*
from (
select xmltype(dbms_xmlgen.getxml('select * from ' || table_name
|| ' where some_col like ''%Test%''')) as xml
from user_tables
where table_name like 'TABLE_%'
) t
cross join xmltable('/ROWSET/ROW'
passing t.xml
columns id number path 'ID',
some_col varchar2(10) path 'SOME_COL'
) x;
SQL Fiddle demo which retrieves one matching row from each of two similar tables. Of course, this assumes your table names follow a useful pattern like table_%, but you suggest they do.
This is the only way I know to do something like this without resorting to PL/SQL (and having searched back a bit, was probably inspired by this answer to count multiple tables). Whether it's efficient (enough) is something you'd need to test with your data.
This is kind of messy and best performed in a middle-tier, but I suppose you could basically loop over the tables and use EXECUTE IMMEDIATE to do it.
Something like:
for t in (select table_name from all_tables where table_name like 'table_%') loop
execute immediate 'select blah from ' || t.table_name;
end loop;
You can write "select * from table_1 and table_2 and tabl_3;"

How can I Retrieve a List of All Table Names from a SQL Server CE database?

Is there some SQL that will either return a list of table names or (to cut to the chase) that would return a boolean as to whether a tablename with a certain pattern exists?
Specifically, I need to know if there is a table in the database named INV[Bla] such as INVclay, INVcherri, INVkelvin, INVmorgan, INVgrandFunk, INVgobbledygook, INV2468WhoDoWeAppreciate, etc. (the INV part is what I'm looking for; the remainder of the table name could be almost anything).
IOW, can "wildcards" be used in a SQL statement, such as:
SELECT * tables
FROM database
WHERE tableName = 'INV*'
or how would this be accomplished?
This should get you there:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where table_name LIKE '%INV%'
EDIT:
fixed table_name
To check for exists:
--
-- note that the sql compiler knows that it just needs to check for existence, so this is a case where "select *" is just fine
if exists
(select *
from [sys].[tables]
where upper([name]) like N'INV%')
select N'do something appropriate because there is a table based on this pattern';
You can try the following:
SELECT name FROM sys.tables where name LIKE 'INV%';

How to select a column without its name in sql server?

how to
select name,family from student where name="X"
without its column name.
for example :
select "column1","column2" from student where "column1"="x"
or for example
select "1","2" from student where "1"="x"
"1" is column1
"2" is column2
i dont want to say columns name. i want to say just its number or other....
idont tired from select *. but it just for that i dont know the columns name but i know where they are. my columns name are change every i want to read the file but its data are same, and the data are in the same columns in each file.
Although you can not use field positions specifiers in the SELECT statement, the SQL standard includes the INFORMATION_SCHEMA where the dictionary of your tables is defined. This includes the COLUMNS view where all the fields of all tables are defined. And in this view, there is a field called ORDINAL_POSITION which you can use to assist in this problem.
If you query
SELECT ORDINAL_POSITION, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TABLE'
ORDER BY ORDINAL_POSITION
then you can obtain the column names for the ordinal positions you want. From there you can prepare a SQL statement.
You could use temp table as:
DECLARE #TB TABLE(Column1 NVARCHAR(50),...)
INSERT #TB
SELECT * FROM student
Then use it:
SELECT Column1 FROM #TB WHERE Column1='aa'
If it's a string you can do this :
Select Column1 + '' From Table
If it's a number you can do this :
Select Column1 + 0 From Table
If it's a datetime you can do this :
Select dateadd(d, 0, Column1) From Table
And similarly for other data types..
No, you can not use the ordinal (numeric) position in the SELECT clause. Only in Order by you can.
however you can make your own column alias...
Select Column1 as [1] From Table
You can use alias:
SELECT name AS [1], family AS [2] FROM student WHERE name="X"
It's just not possible. Unfortunately, they didn't think about table-valued functions, for which information_schema is not available, so good luck with that.