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

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%';

Related

List all table names without knowing the database name within my machine

I am using SQl Server 2014, and Normally we can list out tables if we knew the DB name as :
USE YOURDBNAME
GO
SELECT *
FROM sys.Tables
GO
But I want to know all the tables irrespective of db's that are present on my machine
Or can I go for looping by listing out all DB name.(EXEC sp_databases)
Is there any better way to find out this?
There are several ways of getting all tables in your database
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
OR
SELECT * FROM Sys.Tables
OR
SELECT * FROM INFORMATION_SCHEMA.TABLES
OR
SELECT sobjects.name FROM sysobjects sobjects WHERE sobjects.xtype = 'U'
Docs for all the xtypes
select * from tab
or
select * from * cat
The first one will show the associated CLUSTERID with tables for the current user.
I think you need
SELECT name FROM master.sys.databases
and then you will need to create a cursor query (for your USE XXXX) to iterate through the following query.
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

Why can't I query individual columns in a SQL view?

I've used a script to create a view that has the table name for every table in a database, along with the number of rows of data in the respective table. Everything works fine with SELECT *. However, I'd like to query only certain rows, but I can't seem to do that.
The view was created with the following script (credit to DatabaseZone.com for the script):
CREATE VIEW RowCount_AllTables
AS
SELECT DISTINCT
sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'RowCount'
FROM
sys.tables
INNER JOIN
sys.dm_db_partition_stats ON sys.tables.object_id = sys.dm_db_partition_stats.object_id
INNER JOIN
sys.schemas ON sys.tables.schema_id = sys.schemas.schema_id
WHERE
(sys.tables.is_ms_shipped = 0)
When I run Select * against the resulting view, I get results such as:
name tableName RowCount
dbo Table1 2
dbo Table2 1230
dbo Table3 0
If I query just the tableName column, that works fine and returns the table names. However, if I try to query the RowCount column as well, I get the error message Incorrect syntax near the keyword 'RowCount. This happens regardless of whether I qualify the database -- it seems to not recognize RowCount as a valid column that I can call in a query. So this script fails:
SELECT RowCount
FROM RowCount_AllTables;
But this one works:
SELECT tableName
FROM RowCount_AllTables;
What's going on? I'd like to be able to alias the column names in the view in my query but I can't do that so long as I can't reference the RowCount column.
(FYI running this in SQL Server 2014)
Rowcount is a reserved word, you can select reserved words using [] as:
[Rowcount]
Thanks to #sgeddes for pointing the way. Dropped the view, changed the script for creating it to use another name for the row count column, and it now works as expected. The issue was a conflict with Rowcount being a reserved word.
Changed the create table script on this line:
SELECT distinct sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'RowCount'
to:
SELECT distinct sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'Row_Count'
...at which point I can now reference the Row_Count column as desired.

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 to find DB2 tables that do not exist

I have a list of tables, and I want to check which ones do not currently exist in the target database, I can't figure out a query to return only the tables from the list that do not exist? I am running DB2 9.7.
If the list of tables you want to check against is in a form that you can query it would be something like this. The query below will return all tables NOT in the select table query (that you'll have to provide):
select * from sysibm.systables
where owner = 'SCHEMA'
and type = 'T'
and name not in ( /* select query to return your list of tables */ );
Update post comment:
If the tables are listed in a flat file (.txt, .csv) and the number is manageable. You should be able to list them out in a coma seperated form like this (at least you can with other sql languages I'm more familiar with).
select * from sysibm.systables
where owner = 'SCHEMA'
and type = 'T'
and name not in ( 'table1', 'table2', 'table3', 'table4', 'tableA', 'tableB' );
Otherwise you might have to build a quick temp table to import all the table names into and go with the first example still.
Update post post comment:
And finally, after your most recent comment, I realize I was mis-understanding your question and had it backwards. To flip it and find the tables from the list that aren't in the SCHEMA you'd do something like this. (after importing the list into a temporary table).
select mytablename from templistoftables
where mytablename not in
(select name from sysibm.systables
where owner = 'SCHEMA'
and type = 'T');

mysql select all table names except

Alright, I know you can do the following to get all column names.
SHOW COLUMNS FROM person;
However, it does not allow where statements.
Is there any way to get the full list and just say not this one?
So I am basically looking for something like this:
select (show columns from person where Field <> 'id') from person
USe SHOW [FULL] COLUMNS {FROM | IN} tbl_name [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]
Example:
SHOW CHARACTER SET WHERE `Default collation` LIKE '%japanese%';
More about show here
Try something like SHOW COLUMNS FROM person WHERE column != 'value';
Or you can use proper a SQL query against information_schema
select * from information_schema.columns
where table_schema = 'test' # your db name
and table_name = 'person '
and column_name != 'id'
For MySQL 4, you can use LIKE directly instead of WHERE http://dev.mysql.com/doc/refman/4.1/en/show-columns.html
SHOW COLUMNS FROM 'Person' LIKE '%x%'
However unfortunately, that does not support a NOT clause.
If I may suggest
If you need this information from
MySQL console/phpAdmin/equivalent,
just eyeball it..
If you need this
from a front-end program like php, do
it from that environment.
MySQL documentation says that WHERE clause is allowed. Read the docs.
You cannot perform a true SHOW COLUMNS WHERE in MySQL 4. The docs show only an optional LIKE parameter for that version: http://dev.mysql.com/doc/refman/4.1/en/show-columns.html
However, you can use the metadatabase information_schema.
USE information_schema;
SELECT * FROM `COLUMNS` WHERE `TABLE_NAME` = 'person' AND `COLUMN_NAME` != 'value';