Count the total number of databases - sql-server

I am working in SQL server having a large number of databases. I want to count the number of databases. Is there any query to count?

SELECT * FROM sys.databases
OR
SELECT COUNT(*) FROM sys.databases

You can try this
SELECT Count(*) as DatabaseCount FROM master..sysdatabases
or
SELECT count(*) as DatabaseCount FROM master.sys.databases

try select COUNT(*) from sysdatabases or select COUNT(*) from sys.databases
edited from source: http://www.sqlservercentral.com/Forums/Topic401516-463-1.aspx#bm816566

SELECT COUNT(*) FROM sys.databases where database_id not in ( 1,2,3,4 ).. excluding system database

SELECT count(1) FROM sys.databases
this is what you can have for counting the number of database check this link for more info

If you only want to know the count, check this-
select COUNT(*) from sys.databases
check select * from sysdatabases for 2000 and 2005 server

Related

How to list all the user tables in Sybase along with their row count?

I would like to return all the tables and its count next to it. what is the quickest way to go about it?
I know in Oracle, you can do something like below, but not sure about Sybase:
declare n number;
begin
for rec in (select object_name from user_objects where object_type='TABLE')
loop
execute immediate 'select count(*) from '||rec.object_name into n;
dbms_output.put_line (rec.object_name||':'||n);
end loop;
end;
Here is the Sybase sql that does the above:
select ob.name,st.rowcnt
from sysobjects ob, systabstats st
where ob.type="U"
and st.id=ob.id
order by ob.name
It depends on what Sybase product you mean. In my SQL Anywhere (9 and 11), your solution doesn't work, but this works:
select table_name, count
from systable
where primary_root<>0 and creator=1
order by 1
As there can be multiple entries in systabstats table, the query should be:
select ob.name, sum(st.rowcnt)
from sysobjects ob, systabstats st
where ob.type="U"
and st.id=ob.id
group by ob.name
order by ob.name
If the current user is the creator:
SELECT table_name, count
FROM sys.systable
WHERE creator = user_id()
NOTE: I test this in Sybase ASA 9.
use the below query
select name,row_count(db_id(),id) as "Rows"
from sysobjects where type='U' order by 2 desc
This works for me using "SQL Central" with SQL Anywhere 17:
SELECT table_name, st.count
FROM systable st
WHERE table_type = 'BASE'
select ob.name,st.rowcnt from sysobjects ob, systabstats st where b.type='U'
and st.id=ob.id and indid=0 order by ob.name

Getting a count of each distinct row in SQL Server

How do I get this statement (which returns duplicates):
Select Name from Table
To return this:
Select Distinct Name, Count(Number of non-distinct Name rows)
?
Thanks in advance.
SELECT Name, COUNT(Name) AS DistinctCount
FROM tableName
GROUP BY Name
Try:
select
Name,
count(Name)
from tableName
GROUP BY name

how to count number of tables/views/index in my database

how to count number of tables/views/index in my database
I am using sybase 11
select count(*) from sysobjects where type = 'U'
should get you the number of user tables. You can also use type = 'V' to count views.
select count(*) from sysindexes
will give you an index count. You may need to further filter both though, depending on which types of indexes you want.
sysobjects reference here.
sysindexes reference here.
For oracle
Count Tables:
SELECT COUNT(*) FROM user_tables;
Count Sequences
SELECT COUNT(*) FROM user_sequences;
Count Views
SELECT COUNT(*) FROM user_views;
Count Indexes
SELECT COUNT(*) FROM user_indexes;
Hi Hope this below sql works
SELECT COUNT(*) FROM USER_TABLES;
will return you number of tables in respective database.

How to display user databases only in sqlserver?

how to display user databases in sqlserver
i queried as select sys.databases it displays all including msdb temp and all.
i need only user cretaed databases.
is there any query like : select * from sys.databases where type='u' ??
Help me.
Thanks in Advance
Is there a reason it needs to be more advanced than just this?
SELECT * FROM sysdatabases WHERE name NOT IN('master', 'tempdb', 'model', 'msdb')
You should specify them with their owner name. In order to do that, you should join sys.databases with sys.server_principals.
Recent versions can have ReportServer databases as well with contains $ sign in name.
select
d.name
,d.database_id
from
sys.databases d
join
sys.server_principals p
on p.sid = d.owner_sid
where
p.name <> 'sa' and d.name not like '%$%';
Or per this thread:
select * from sys.sysdatabases where dbid>4 and [name] not like '$'

How do I check if a column exists in SQL Server?

How do I check if a column exists in SQL Server 2000?
IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='tablename' AND COLUMN_NAME='columname' )
If col_length('table_name','column_name') is null
select 0 as Present
ELSE
select 1 as Present
Present will be 0, is there is no column_name present in table_name , otherwise 1
#CMS: I don't think that 'INFORMATION_SCHEMA.COLUMNS' have information about every table in DB. Because this didn't worked for me. But my answer did worked.
In query analyzer, select the Database that contains the table in which you need to check if the field exists or not and run the query below.
SELECT count(*) AS [Column Exists]
FROM SYSOBJECTS
INNER JOIN SYSCOLUMNS ON SYSOBJECTS.ID = SYSCOLUMNS.ID
WHERE
SYSOBJECTS.NAME = 'myTable'
AND SYSCOLUMNS.NAME = 'Myfield'
This should do nicely:
if COLUMNPROPERTY(object_id('table_name'), 'column_name', 'ColumnId') is null
print 'doesn\'t exist'
else
print 'exists'
I don't know if this script will work in sqlserver 2000, but in 2008 works:
SELECT COLUMNS.*
FROM INFORMATION_SCHEMA.COLUMNS COLUMNS, INFORMATION_SCHEMA.TABLES TABLES
WHERE COLUMNS.TABLE_NAME=TABLES.TABLE_NAME AND UPPER(COLUMNS.COLUMN_NAME)=UPPER('column_name')

Resources