Get row count of all tables in database: SQL Server [duplicate] - sql

This question already has answers here:
Query to list number of records in each table in a database
(23 answers)
Closed 8 years ago.
I'm trying to familiarize myself with a large database and search for relevant information among the many tables. I often find myself calling up a table, to see if there is relevant data inside, only to find that the table has no records.
How to quickly call up a list of all tables and the number of records contained therein? I'm using sql server 2008.
Thanks!
Related Question: How do I QUICKLY check many sql database tables and views to see if they are not empty or contain records

Right click on database -> Reports -> Standard Reports -> Disk usage by Top Tables

If you want to use a query, you can use this (note: it's using an undocumented stored procedure sp_msforeachtable):
create table #tempcount (tablename nvarchar(128), record_count bigint)
EXEC sp_msforeachtable 'insert #tempcount select ''?'', count(*) from ? with (nolock)'
select * from #tempcount
drop table #tempcount

Related

How do I create a temp table in Oracle SQL just like in Microsoft SQL Server? [duplicate]

This question already has answers here:
How do you create a temporary table in an Oracle database?
(4 answers)
Closed 2 months ago.
I saw a lot of examples online, but I'm still unsure on how to create a temporary table. The following example is how I create a temporary table in SQL Server:
if object_id('tempdb.dbo.#processid') is not null truncate table #processid
if object_id('tempdb.dbo.#processid') is not null drop table #processid
select distinct
lot_id,
ssr.run_oid,
process_id
into #processid
from sigma.sigma_run ssr
inner join sigma.sigma_lot ssl on ssl.run_oid = ssr.run_oid
How can I create a temporary table just like this in Oracle SQL?
In Oracle, we usually do NOT create temporary tables.
Basically, it depends on what you want to do.
If you must, then you could e.g.
create global temporary table temp_table as
select distinct
lot_id,
ssr.run_oid,
process_id
from sigma.sigma_run ssr
inner join sigma.sigma_lot ssl on ssl.run_oid = ssr.run_oid;
But, once again, you should explain what's behind the scene so that we could suggest what to do next.
Note that data in a temporary table is visible only to you; other users won't see anything. If you want to share data collected with that query, then create a "normal" table (i.e. remove global temporary from that statement).
Also, you could create a view (which is just a stored query) and then fetch data from it.

Filtering a SELECT statement based on row data [duplicate]

This question already has answers here:
Query to list number of records in each table in a database
(23 answers)
Closed 7 years ago.
I'm trying to add a WHERE clause to a SELECT statement that checks to see if there is data in the table or if it was a 0-row table.
Basically, I'm trying to get this to work but obviously tables.NAME isn't a valid object.
SELECT NAME
FROM sys.tables
WHERE (SELECT Count(*)
FROM tables.NAME) <> 0
I don't want to have to create a temp table, declare a cursor and checking a value row by row, but I'm having a hard time thinking a bout how to do this otherwise.
Using a cursor is a suitable solution to this problem. You can't dynamically interpret a column from one table as a table name and simultaneously select from that table to see if it's empty like you're attempting to do. In fact you can't even select from a dynamic table without using dynamic SQL. See the documentation for FROM. You can either iterate over each table and check each, or you can query system tables that hold storage statistics to see if tables are empty.

PostgreSQL compare data across different servers [duplicate]

This question already has answers here:
Possible to perform cross-database queries with PostgreSQL?
(9 answers)
PostgreSQL cross server query? [closed]
(2 answers)
Closed 9 years ago.
I've one problem regarding manage information from two different Postgres servers.
In the one server I've one table a containing id bigserial and phone varchar(200)
On the other server I've table b that contains id bigserial and tell varchar(200)
Is there a way that I can compare witch rows from table a are present in the rows of table b, comparing by phone = tell?
You can use dblink to fetch data from remote server. Once you have a cursor open you can treat data from dblink_fetch as data returned from local table and just outer join it with the local table. If there are many rows in the remote table you might want to use pl/pgSQL to fetch it in parts.

RODBC - multiple tables in one sqlQuery() call [duplicate]

This question already has an answer here:
How to read multiple result sets returned from a SQL Server stored procedure in R
(1 answer)
Closed 3 years ago.
I'm using RODBC to pull data into R from an existing SQL database. I am trying to call multiple queries/tables at once.
For example:
test2=sqlQuery(channel,'select top 10 * from bug; select top 10 * from site')
This only returns the first table. I am interested in doing this because I want to call a more complex stored procedure that other collaborators are using and continuing development on...this procedure returns 3 tables (which are set as temporary tables in the stored procedure (#) to meet our other data needs/uses).
So, when I call the stored procedure, similar to the two query scenario above, it only returns the first table as the result.
Any suggested workarounds?
I don't think you'll be able to get that to work in RODBC; as far as I know, it expects a single resultset from the database.
However, you could modify your SQL to get both values in a single query:
SELECT
(SELECT COUNT(*)
FROM bug) AS bugcount,
(SELECT COUNT(*)
FROM site) AS sitecount;

Export a table with SQL Server 2005

I'm not able to find in SQL Server 2005 the utility that phpmyadmin has for exporting tables.
My case is:
I have an small table (20 rows) and I need to delete some columns I won't need.
So I need a way to dump all info the table contains in an insert query. Then, editing this query I just need to delete the columns I don't need and later I can delete the table and create it again filling it with the edited insert query.
How can I do that with SQL Server 2005?
Rather than exporting, dropping, recreating and repopulating the table, why not simply drop each of the unwanted columns:
ALTER TABLE TableName DROP COLUMN ColumnName;
Further details here.