Can any one tell me that how to get names of all tables of a database using asp.net
A newer method on SQL Server is to use the INFORMATION_SCHEMA Views to get the information:
SELECT table_name FROM INFORMATION_SCHEMA.Tables WHERE table_type='BASE TABLE'
This particular view also includes Views in its list of tables, which is why you need the where clause.
You didn't mention which database engine you are using. On SQL Server, you can query the sysobjects table and filter for objects with type U:
SELECT name FROM sysobjects WHERE type = 'U'
In case you are interested in the MySQL way to achieve this, you can use
DESCRIBE tableName;
Related
I am sure I can solve this programmatically, but I am curious if I can do this in one query.
Context:
I will be querying multiple databases, some will have a table; 'table', others will instead have the table; 'table_v2'. I want to run the same SELECT statement on the table that exists. I know I can check if a table exists, but I want to know if I can do it all in one statement.
psuedo code summary of what I want to do in one statement:
if 'SELECT name FROM sqlite_master WHERE type='table' AND name=''table'; is not empty:
SELECT * FROM table;
else
SELECT * FROM table_v2;
I am beholden to constraints out of my control.
Thoughts:
Could I have the table name be a regex?
Can I run both SELECTS, ignore the failed result, and just return the success?
Generally, you can't do either. The query planner in SQLite needs to know the name of the table beforehand, and it must be valid so it can determine which paths to take.
You can use the loadable extension eval to build up the SQL query based off of the schema. Though, this exposes a variant of the same issue, since the query planner needs the table name, you need to build up the entire SQL statement, then run it, so you'll need two eval calls.
SELECT EVAL(
'SELECT * FROM ''' ||
EVAL('SELECT name FROM sqlite_master WHERE type=''table'' AND name IN (''table'', ''table_v2'');') ||
''';'
);
To use the eval function, you'll need to either build and load the extension as a library, or build it into your own custom build of SQLite itself.
Of course, I can't answer if you should do this.
I know that to find all the fields of a table I should use something like
sqlplus > desc testtable;
This lists all the fields of a table (here testtable)
But now, I have a list of tables in my database.
What is the way through which I can get the fields of all tables
in a given database using sqlplus?
I tried
sqlplus > desc <Databasename>; which didnot work.
Someone told me to use
sqlplus > select * from INFORMATION_SCHEMA.TABLES ; //gives error.
SQLPLUS > SELECT * FROM INFORMATION_SCHEMA.COLUMNS; //gives some error.
(At the bottom line, I am trying to get the database schema.)
I don't believe that the information_schema tables you can find in other dbs (SQL Server, PostGres, MySQL, etc) is in Oracle. I use to use the ALL_TAB_COLUMNS table to get at that type of information....maybe another alternative.
Link: ALL_TAB_COLUMNS
How can i see only VIEWS in SQLite.
.tables command list both views as well as tables. I want to see only VIEWS and not tables.
Thanks.
You can do:
SELECT sql FROM sqlite_master WHERE type = 'view';
In order to fine-tune it:
.headers on
select * from sqlite_master;
you will know what columns are available there.
I have a trivial issue that I can't resolve. Currently our app uses Linq to retrieve data and get a basic integer value of the row count. I can't form a query that gives back a count without a 'select i'. I don't need the select, just the count(*) response. How do I do this? Below is a sample:
return (from io in db._Owners
where io.Id == Id && io.userId == userId
join i in db._Instances on io.Id equals i.Id **select i**).Count()
;
The select i is fine - it's not actually going to be fetching any data back to the client, because the Count() call will be translated into a Count(something) call at the SQL side.
When in doubt, look at the SQL that's being generated for your query, e.g. with the DataContext.Log property.
Using the LINQ query syntax requires a select statement. There's no way around that.
That being said, the statement will get transformed into a COUNT()-based query; the select i is there only to satisfy the expression system that underlies the LINQ query providers (otherwise the type of the expression would be unknown).
Including the select will not affect the performance here because the final query will get translated into SQL. At this point it will be optimized and will be like select (*) from ......
You can get a list of databases using
PRAGMA database_list
or a list of tables in the "main" database using
select name from sqlite_master where type='table'
but as I just wrote, it only returns the tables from the "main" DB only, and I don't see a way to know which tables are in the other DBs.
So how does one list the tables in the other DBs (which were attached later on)?
Thanks, --DD
PS: I can think of a work around of creating a separate sqlite* for each DB listed via the pragma database_list, and them doing the "select name from sqlite_master where type='table'" N times on those (since each one is the "main" one now), but this sounds like something that should be possible without resorting to the work-around, no???
Ah ah, found the answer by looking at the answer for How do I open an in-memory database file into sqlite3
Since there's a sqlite_master per DB, all I need to do is prefix sqlite_master with "DB_name." where DB_name corresponds to the name column returned by PRAGMA database_list.