Showing posts with label advance. Show all posts
Showing posts with label advance. Show all posts

Monday, March 26, 2012

Number of SQL Server logins

Is there a limit on how many SQL logins can be created on an instance? If
so, where is this documented? Thanks in advance.
OscarNumber of users per database = 16,379
Number of roles per database = 16,367
Number of concurrent connections = 32,767
However, if you are approaching any of these limits, your design will
probably raise quite a few eyebrows...
--
http://www.aspfaq.com/
(Reverse address to reply.)
"Oscar" <Oscar@.discussions.microsoft.com> wrote in message
news:4159DD0F-0B77-4783-8AEA-A779E3138700@.microsoft.com...
> Is there a limit on how many SQL logins can be created on an instance? If
> so, where is this documented? Thanks in advance.
> Oscar|||Hi
Can't find documentation, but master.dbo.syslogins does not use a counter.
Good so far. Limitation here is number of unique combinations of charaters
that fit into "name" which is nvarchar(128)
In userdatabase.dbo.sysusers, uid is a smallint, so 32'767 would be the
limit per database.
So, it looks like only a per DB limit. Think it is time for me to do a bit
of an experiment.
Regards
Mike
"Oscar" wrote:
> Is there a limit on how many SQL logins can be created on an instance? If
> so, where is this documented? Thanks in advance.
> Oscar|||I compiled this list from a variety of sources, but IIRC, they were all
official:
http://www.aspfaq.com/2345
So I think there might be an enforced limit on users per database...
--
http://www.aspfaq.com/
(Reverse address to reply.)
"Mike Epprecht (SQL MVP)" <mike@.epprecht.net> wrote in message
news:257A5B67-9708-4FA0-B58C-897000078781@.microsoft.com...
> Hi
> Can't find documentation, but master.dbo.syslogins does not use a counter.
> Good so far. Limitation here is number of unique combinations of charaters
> that fit into "name" which is nvarchar(128)
> In userdatabase.dbo.sysusers, uid is a smallint, so 32'767 would be the
> limit per database.
> So, it looks like only a per DB limit. Think it is time for me to do a bit
> of an experiment.
> Regards
> Mike
> "Oscar" wrote:
> > Is there a limit on how many SQL logins can be created on an instance?
If
> > so, where is this documented? Thanks in advance.
> >
> > Oscar|||> In userdatabase.dbo.sysusers, uid is a smallint, so 32'767 would be the
> limit per database.
However, roles are also stored in sysusers, cutting down number of users to half. Below is a line
from sp_grantdbaccess:
where uid >= 5 and uid < (16384 - 1) -- stay in users range
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Mike Epprecht (SQL MVP)" <mike@.epprecht.net> wrote in message
news:257A5B67-9708-4FA0-B58C-897000078781@.microsoft.com...
> Hi
> Can't find documentation, but master.dbo.syslogins does not use a counter.
> Good so far. Limitation here is number of unique combinations of charaters
> that fit into "name" which is nvarchar(128)
> In userdatabase.dbo.sysusers, uid is a smallint, so 32'767 would be the
> limit per database.
> So, it looks like only a per DB limit. Think it is time for me to do a bit
> of an experiment.
> Regards
> Mike
> "Oscar" wrote:
>> Is there a limit on how many SQL logins can be created on an instance? If
>> so, where is this documented? Thanks in advance.
>> Oscar|||Thank You
"Aaron [SQL Server MVP]" wrote:
> Number of users per database = 16,379
> Number of roles per database = 16,367
> Number of concurrent connections = 32,767
> However, if you are approaching any of these limits, your design will
> probably raise quite a few eyebrows...
> --
> http://www.aspfaq.com/
> (Reverse address to reply.)
>
>
> "Oscar" <Oscar@.discussions.microsoft.com> wrote in message
> news:4159DD0F-0B77-4783-8AEA-A779E3138700@.microsoft.com...
> > Is there a limit on how many SQL logins can be created on an instance? If
> > so, where is this documented? Thanks in advance.
> >
> > Oscar
>
>|||Thank You
"Mike Epprecht (SQL MVP)" wrote:
> Hi
> Can't find documentation, but master.dbo.syslogins does not use a counter.
> Good so far. Limitation here is number of unique combinations of charaters
> that fit into "name" which is nvarchar(128)
> In userdatabase.dbo.sysusers, uid is a smallint, so 32'767 would be the
> limit per database.
> So, it looks like only a per DB limit. Think it is time for me to do a bit
> of an experiment.
> Regards
> Mike
> "Oscar" wrote:
> > Is there a limit on how many SQL logins can be created on an instance? If
> > so, where is this documented? Thanks in advance.
> >
> > Oscar|||Thank You
"Tibor Karaszi" wrote:
> > In userdatabase.dbo.sysusers, uid is a smallint, so 32'767 would be the
> > limit per database.
> However, roles are also stored in sysusers, cutting down number of users to half. Below is a line
> from sp_grantdbaccess:
> where uid >= 5 and uid < (16384 - 1) -- stay in users range
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> http://www.sqlug.se/
>
> "Mike Epprecht (SQL MVP)" <mike@.epprecht.net> wrote in message
> news:257A5B67-9708-4FA0-B58C-897000078781@.microsoft.com...
> > Hi
> >
> > Can't find documentation, but master.dbo.syslogins does not use a counter.
> > Good so far. Limitation here is number of unique combinations of charaters
> > that fit into "name" which is nvarchar(128)
> >
> > In userdatabase.dbo.sysusers, uid is a smallint, so 32'767 would be the
> > limit per database.
> >
> > So, it looks like only a per DB limit. Think it is time for me to do a bit
> > of an experiment.
> >
> > Regards
> > Mike
> >
> > "Oscar" wrote:
> >
> >> Is there a limit on how many SQL logins can be created on an instance? If
> >> so, where is this documented? Thanks in advance.
> >>
> >> Oscar
>
>|||Hi
Yes you are right....my head has been a bit too much in Sybase today.
Busy writing some scripts to test what really happens when the 2 tables hit
their limits.
Cheers
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:OaEph9D7EHA.2572@.tk2msftngp13.phx.gbl...
> > In userdatabase.dbo.sysusers, uid is a smallint, so 32'767 would be the
> > limit per database.
> However, roles are also stored in sysusers, cutting down number of users
to half. Below is a line
> from sp_grantdbaccess:
> where uid >= 5 and uid < (16384 - 1) -- stay in users range
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> http://www.sqlug.se/
>
> "Mike Epprecht (SQL MVP)" <mike@.epprecht.net> wrote in message
> news:257A5B67-9708-4FA0-B58C-897000078781@.microsoft.com...
> > Hi
> >
> > Can't find documentation, but master.dbo.syslogins does not use a
counter.
> > Good so far. Limitation here is number of unique combinations of
charaters
> > that fit into "name" which is nvarchar(128)
> >
> > In userdatabase.dbo.sysusers, uid is a smallint, so 32'767 would be the
> > limit per database.
> >
> > So, it looks like only a per DB limit. Think it is time for me to do a
bit
> > of an experiment.
> >
> > Regards
> > Mike
> >
> > "Oscar" wrote:
> >
> >> Is there a limit on how many SQL logins can be created on an instance?
If
> >> so, where is this documented? Thanks in advance.
> >>
> >> Oscar
>

Tuesday, March 20, 2012

Number of columns for all tables in a database

Hi - I apologize in advance for the newbie question...
I have been asked to gather some db statistics. We have 11 dbs and
some have 70 or more tables.
I need to gather is number of fields for each table in the database.
I've used this so far:
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'pub_info'
and doing this for each table in each database. However, this is going
to take me three lifetimes. ;o)
Is there a query that can output a grid listing the number of fields
per each table in a selected database?
If the query could simultaneously list the number of rows as well that
would be a bonus, but not necessary.
I REALLY appreciate any help sent my way.
Cheers!
Joel
Hi Joel
No need to apologize, it's a perfectly valid question. The only thing
'newbie' is that you didn't tell us what version you were using.
I will assume SQL Server 2000.
There's no easy way to do this for all databases in a single query, but this
will give you all tables in one database:
SELECT TABLE_NAME, COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME
To get all databases, you would have to use an undocumented command that
basically 'loops' through all the databases. It will not give you a single
resultset however.
exec sp_MSforeachdb
'USE ?; SELECT TABLE_NAME, COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME'
If you wanted all the output in a single table, you could change the command
to insert all the rows into a temp table, and then as a final step you could
select from the temp table. You might want to try doing that on your own as
a homework exercise. :-)
Also, listing the number of rows is a different problem. There is no where
that is stored in a guaranteed way, so you would have to go through and
count in each table. That would not be a single query. There is a system
table that contains an estimate of the number of rows. It is called
sysindexes, and you could join that to INFORMATION_SCHEMA.COLUMNS. But if I
were doing it, as long as I was having to go to the real system tables
anyway, I would skip the INFORMATION_SCHEME and use all system tables:
sysobjects, syscolumns and sysindexes. (But there would still be the problem
of getting all info from all databases together.)
Hopefully, this has given you a start...
HTH
Kalen Delaney, SQL Server MVP
http://sqlblog.com
"JoelBarish" <JoelForBoards@.gmail.com> wrote in message
news:1159539224.878544.293450@.i42g2000cwa.googlegr oups.com...
> Hi - I apologize in advance for the newbie question...
> I have been asked to gather some db statistics. We have 11 dbs and
> some have 70 or more tables.
> I need to gather is number of fields for each table in the database.
> I've used this so far:
> SELECT COUNT(*)
> FROM INFORMATION_SCHEMA.COLUMNS
> WHERE TABLE_NAME = 'pub_info'
> and doing this for each table in each database. However, this is going
> to take me three lifetimes. ;o)
> Is there a query that can output a grid listing the number of fields
> per each table in a selected database?
> If the query could simultaneously list the number of rows as well that
> would be a bonus, but not necessary.
> I REALLY appreciate any help sent my way.
> Cheers!
> Joel
>
|||Just do a GROUP BY and you are there:
SELECT TABLE_NAME, COUNT(*) AS noOfColumns
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME
For number of rows, we need to know what version of SQL Server...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"JoelBarish" <JoelForBoards@.gmail.com> wrote in message
news:1159539224.878544.293450@.i42g2000cwa.googlegr oups.com...
> Hi - I apologize in advance for the newbie question...
> I have been asked to gather some db statistics. We have 11 dbs and
> some have 70 or more tables.
> I need to gather is number of fields for each table in the database.
> I've used this so far:
> SELECT COUNT(*)
> FROM INFORMATION_SCHEMA.COLUMNS
> WHERE TABLE_NAME = 'pub_info'
> and doing this for each table in each database. However, this is going
> to take me three lifetimes. ;o)
> Is there a query that can output a grid listing the number of fields
> per each table in a selected database?
> If the query could simultaneously list the number of rows as well that
> would be a bonus, but not necessary.
> I REALLY appreciate any help sent my way.
> Cheers!
> Joel
>
|||Thank you BOTH so much for your help.
I will take on the homework assignment suggested by Kalen as it is good
exercise for me as a novice.
We are using SQL Server 2000. -- You see! This is how much of a
newbie I am! I didn't even know it made a difference!! :o) If anyone
knows a quickie way to get the rows until I can get to my homework that
would be great. Just trying to get this information out the door to
mgmt for now...
Thanks!
Tibor Karaszi wrote:[vbcol=seagreen]
> Just do a GROUP BY and you are there:
> SELECT TABLE_NAME, COUNT(*) AS noOfColumns
> FROM INFORMATION_SCHEMA.COLUMNS
> GROUP BY TABLE_NAME
> For number of rows, we need to know what version of SQL Server...
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "JoelBarish" <JoelForBoards@.gmail.com> wrote in message
> news:1159539224.878544.293450@.i42g2000cwa.googlegr oups.com...
|||Does the row count need to be exact? If not, use the sysindexes system table, something like:
SELECT OBJECT_NAME(i.id) AS table_name, i.rowcnt, COUNT(*) AS NoOfColumns
FROM sysindexes AS i
JOIN syscolumns AS c
ON i.id = c.id
WHERE i.indid IN(0,1) AND OBJECTPROPERTY(i.id, 'IsMSShipped') = 0
GROUP BY OBJECT_NAME(i.id), i.rowcnt
And before you run above, run (below should make rowcount exact):
DBCC UPDATEUSAGE('dbname') WITH COUNT_ROWS
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"JoelBarish" <JoelForBoards@.gmail.com> wrote in message
news:1159544274.480475.86270@.b28g2000cwb.googlegro ups.com...
> Thank you BOTH so much for your help.
> I will take on the homework assignment suggested by Kalen as it is good
> exercise for me as a novice.
> We are using SQL Server 2000. -- You see! This is how much of a
> newbie I am! I didn't even know it made a difference!! :o) If anyone
> knows a quickie way to get the rows until I can get to my homework that
> would be great. Just trying to get this information out the door to
> mgmt for now...
> Thanks!
>
> Tibor Karaszi wrote:
>
|||Joel,
I think I am a little more bored than the rest of them.
--This procedure will loop through the database list and load a table
--in tempdb with the column count for each table.
USE tempdb
IF EXISTS ( SELECT name from sysobjects where name = 'table_specs' )
BEGIN
DROP TABLE table_specs
END
CREATE TABLE table_specs
( db varchar(200) NOT NULL
, tbl_namevarchar(200) NOT NULL
, col_cntint NULL )
DECLARE @.db_namevarchar(200)
DECLARE @.count as int
DECLARE @.cmdvarchar(2000)
SET @.cmd = ''
SET @.db_name = ''
DECLARE dbs CURSOR FOR
SELECT name FROM master..sysdatabases where name is not null
OPEN dbs
FETCH NEXT FROM dbs into @.db_name
WHILE @.@.FETCH_STATUS = 0
BEGIN
PRINT @.db_name
SET@.cmd = 'USE [' + @.db_name + ']'
PRINT@.cmd
EXECUTE( @.cmd)
SET @.cmd = 'INSERT INTO tempdb..table_specs
SELECT''' + @.db_name + ''', a.name, count(*)
FROM[' + @.db_name + ']..sysobjects a, [' + @.db_name + ']..syscolumns
b
WHEREa.id= b.id
ANDa.type= ''U''
GROUP BY a.name'
EXECUTE (@.cmd)
PRINT @.cmd
FETCH NEXT FROM dbs into @.db_name
END
CLOSE dbs
DEALLOCATE dbs
-- SELECT db, sum(col_cnt) FROM tempdb..table_specs GROUP BY db
-- SELECT * FROM tempdb..table_specs

Number of columns for all tables in a database

Hi - I apologize in advance for the newbie question...
I have been asked to gather some db statistics. We have 11 dbs and
some have 70 or more tables.
I need to gather is number of fields for each table in the database.
I've used this so far:
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'pub_info'
and doing this for each table in each database. However, this is going
to take me three lifetimes. ;o)
Is there a query that can output a grid listing the number of fields
per each table in a selected database?
If the query could simultaneously list the number of rows as well that
would be a bonus, but not necessary.
I REALLY appreciate any help sent my way.
Cheers!
JoelHi Joel
No need to apologize, it's a perfectly valid question. The only thing
'newbie' is that you didn't tell us what version you were using.
I will assume SQL Server 2000.
There's no easy way to do this for all databases in a single query, but this
will give you all tables in one database:
SELECT TABLE_NAME, COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME
To get all databases, you would have to use an undocumented command that
basically 'loops' through all the databases. It will not give you a single
resultset however.
exec sp_MSforeachdb
'USE ?; SELECT TABLE_NAME, COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME'
If you wanted all the output in a single table, you could change the command
to insert all the rows into a temp table, and then as a final step you could
select from the temp table. You might want to try doing that on your own as
a homework exercise. :-)
Also, listing the number of rows is a different problem. There is no where
that is stored in a guaranteed way, so you would have to go through and
count in each table. That would not be a single query. There is a system
table that contains an estimate of the number of rows. It is called
sysindexes, and you could join that to INFORMATION_SCHEMA.COLUMNS. But if I
were doing it, as long as I was having to go to the real system tables
anyway, I would skip the INFORMATION_SCHEME and use all system tables:
sysobjects, syscolumns and sysindexes. (But there would still be the problem
of getting all info from all databases together.)
Hopefully, this has given you a start...
--
HTH
Kalen Delaney, SQL Server MVP
http://sqlblog.com
"JoelBarish" <JoelForBoards@.gmail.com> wrote in message
news:1159539224.878544.293450@.i42g2000cwa.googlegroups.com...
> Hi - I apologize in advance for the newbie question...
> I have been asked to gather some db statistics. We have 11 dbs and
> some have 70 or more tables.
> I need to gather is number of fields for each table in the database.
> I've used this so far:
> SELECT COUNT(*)
> FROM INFORMATION_SCHEMA.COLUMNS
> WHERE TABLE_NAME = 'pub_info'
> and doing this for each table in each database. However, this is going
> to take me three lifetimes. ;o)
> Is there a query that can output a grid listing the number of fields
> per each table in a selected database?
> If the query could simultaneously list the number of rows as well that
> would be a bonus, but not necessary.
> I REALLY appreciate any help sent my way.
> Cheers!
> Joel
>|||Just do a GROUP BY and you are there:
SELECT TABLE_NAME, COUNT(*) AS noOfColumns
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME
For number of rows, we need to know what version of SQL Server...
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"JoelBarish" <JoelForBoards@.gmail.com> wrote in message
news:1159539224.878544.293450@.i42g2000cwa.googlegroups.com...
> Hi - I apologize in advance for the newbie question...
> I have been asked to gather some db statistics. We have 11 dbs and
> some have 70 or more tables.
> I need to gather is number of fields for each table in the database.
> I've used this so far:
> SELECT COUNT(*)
> FROM INFORMATION_SCHEMA.COLUMNS
> WHERE TABLE_NAME = 'pub_info'
> and doing this for each table in each database. However, this is going
> to take me three lifetimes. ;o)
> Is there a query that can output a grid listing the number of fields
> per each table in a selected database?
> If the query could simultaneously list the number of rows as well that
> would be a bonus, but not necessary.
> I REALLY appreciate any help sent my way.
> Cheers!
> Joel
>|||Thank you BOTH so much for your help.
I will take on the homework assignment suggested by Kalen as it is good
exercise for me as a novice.
We are using SQL Server 2000. -- You see! This is how much of a
newbie I am! I didn't even know it made a difference!! :o) If anyone
knows a quickie way to get the rows until I can get to my homework that
would be great. Just trying to get this information out the door to
mgmt for now...
Thanks!
Tibor Karaszi wrote:
> Just do a GROUP BY and you are there:
> SELECT TABLE_NAME, COUNT(*) AS noOfColumns
> FROM INFORMATION_SCHEMA.COLUMNS
> GROUP BY TABLE_NAME
> For number of rows, we need to know what version of SQL Server...
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "JoelBarish" <JoelForBoards@.gmail.com> wrote in message
> news:1159539224.878544.293450@.i42g2000cwa.googlegroups.com...
> > Hi - I apologize in advance for the newbie question...
> >
> > I have been asked to gather some db statistics. We have 11 dbs and
> > some have 70 or more tables.
> >
> > I need to gather is number of fields for each table in the database.
> > I've used this so far:
> > SELECT COUNT(*)
> > FROM INFORMATION_SCHEMA.COLUMNS
> > WHERE TABLE_NAME = 'pub_info'
> > and doing this for each table in each database. However, this is going
> > to take me three lifetimes. ;o)
> >
> > Is there a query that can output a grid listing the number of fields
> > per each table in a selected database?
> >
> > If the query could simultaneously list the number of rows as well that
> > would be a bonus, but not necessary.
> >
> > I REALLY appreciate any help sent my way.
> >
> > Cheers!
> > Joel
> >|||Does the row count need to be exact? If not, use the sysindexes system table, something like:
SELECT OBJECT_NAME(i.id) AS table_name, i.rowcnt, COUNT(*) AS NoOfColumns
FROM sysindexes AS i
JOIN syscolumns AS c
ON i.id = c.id
WHERE i.indid IN(0,1) AND OBJECTPROPERTY(i.id, 'IsMSShipped') = 0
GROUP BY OBJECT_NAME(i.id), i.rowcnt
And before you run above, run (below should make rowcount exact):
DBCC UPDATEUSAGE('dbname') WITH COUNT_ROWS
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"JoelBarish" <JoelForBoards@.gmail.com> wrote in message
news:1159544274.480475.86270@.b28g2000cwb.googlegroups.com...
> Thank you BOTH so much for your help.
> I will take on the homework assignment suggested by Kalen as it is good
> exercise for me as a novice.
> We are using SQL Server 2000. -- You see! This is how much of a
> newbie I am! I didn't even know it made a difference!! :o) If anyone
> knows a quickie way to get the rows until I can get to my homework that
> would be great. Just trying to get this information out the door to
> mgmt for now...
> Thanks!
>
> Tibor Karaszi wrote:
>> Just do a GROUP BY and you are there:
>> SELECT TABLE_NAME, COUNT(*) AS noOfColumns
>> FROM INFORMATION_SCHEMA.COLUMNS
>> GROUP BY TABLE_NAME
>> For number of rows, we need to know what version of SQL Server...
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://www.solidqualitylearning.com/
>>
>> "JoelBarish" <JoelForBoards@.gmail.com> wrote in message
>> news:1159539224.878544.293450@.i42g2000cwa.googlegroups.com...
>> > Hi - I apologize in advance for the newbie question...
>> >
>> > I have been asked to gather some db statistics. We have 11 dbs and
>> > some have 70 or more tables.
>> >
>> > I need to gather is number of fields for each table in the database.
>> > I've used this so far:
>> > SELECT COUNT(*)
>> > FROM INFORMATION_SCHEMA.COLUMNS
>> > WHERE TABLE_NAME = 'pub_info'
>> > and doing this for each table in each database. However, this is going
>> > to take me three lifetimes. ;o)
>> >
>> > Is there a query that can output a grid listing the number of fields
>> > per each table in a selected database?
>> >
>> > If the query could simultaneously list the number of rows as well that
>> > would be a bonus, but not necessary.
>> >
>> > I REALLY appreciate any help sent my way.
>> >
>> > Cheers!
>> > Joel
>> >
>|||Joel,
I think I am a little more bored than the rest of them.
--This procedure will loop through the database list and load a table
-- in tempdb with the column count for each table.
USE tempdb
IF EXISTS ( SELECT name from sysobjects where name = 'table_specs' )
BEGIN
DROP TABLE table_specs
END
CREATE TABLE table_specs
( db varchar(200) NOT NULL
, tbl_name varchar(200) NOT NULL
, col_cnt int NULL )
DECLARE @.db_name varchar(200)
DECLARE @.count as int
DECLARE @.cmd varchar(2000)
SET @.cmd = ''
SET @.db_name = ''
DECLARE dbs CURSOR FOR
SELECT name FROM master..sysdatabases where name is not null
OPEN dbs
FETCH NEXT FROM dbs into @.db_name
WHILE @.@.FETCH_STATUS = 0
BEGIN
PRINT @.db_name
SET @.cmd = 'USE [' + @.db_name + ']'
PRINT @.cmd
EXECUTE ( @.cmd)
SET @.cmd = 'INSERT INTO tempdb..table_specs
SELECT ''' + @.db_name + ''', a.name, count(*)
FROM [' + @.db_name + ']..sysobjects a, [' + @.db_name + ']..syscolumns
b
WHERE a.id = b.id
AND a.type = ''U''
GROUP BY a.name'
EXECUTE (@.cmd)
PRINT @.cmd
FETCH NEXT FROM dbs into @.db_name
END
CLOSE dbs
DEALLOCATE dbs
-- SELECT db, sum(col_cnt) FROM tempdb..table_specs GROUP BY db
-- SELECT * FROM tempdb..table_specs

Number of columns for all tables in a database

Hi - I apologize in advance for the newbie question...
I have been asked to gather some db statistics. We have 11 dbs and
some have 70 or more tables.
I need to gather is number of fields for each table in the database.
I've used this so far:
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'pub_info'
and doing this for each table in each database. However, this is going
to take me three lifetimes. ;o)
Is there a query that can output a grid listing the number of fields
per each table in a selected database?
If the query could simultaneously list the number of rows as well that
would be a bonus, but not necessary.
I REALLY appreciate any help sent my way.
Cheers!
JoelHi Joel
No need to apologize, it's a perfectly valid question. The only thing
'newbie' is that you didn't tell us what version you were using.
I will assume SQL Server 2000.
There's no easy way to do this for all databases in a single query, but this
will give you all tables in one database:
SELECT TABLE_NAME, COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME
To get all databases, you would have to use an undocumented command that
basically 'loops' through all the databases. It will not give you a single
resultset however.
exec sp_MSforeachdb
'USE ?; SELECT TABLE_NAME, COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME'
If you wanted all the output in a single table, you could change the command
to insert all the rows into a temp table, and then as a final step you could
select from the temp table. You might want to try doing that on your own as
a homework exercise. :-)
Also, listing the number of rows is a different problem. There is no where
that is stored in a guaranteed way, so you would have to go through and
count in each table. That would not be a single query. There is a system
table that contains an estimate of the number of rows. It is called
sysindexes, and you could join that to INFORMATION_SCHEMA.COLUMNS. But if I
were doing it, as long as I was having to go to the real system tables
anyway, I would skip the INFORMATION_SCHEME and use all system tables:
sysobjects, syscolumns and sysindexes. (But there would still be the problem
of getting all info from all databases together.)
Hopefully, this has given you a start...
HTH
Kalen Delaney, SQL Server MVP
http://sqlblog.com
"JoelBarish" <JoelForBoards@.gmail.com> wrote in message
news:1159539224.878544.293450@.i42g2000cwa.googlegroups.com...
> Hi - I apologize in advance for the newbie question...
> I have been asked to gather some db statistics. We have 11 dbs and
> some have 70 or more tables.
> I need to gather is number of fields for each table in the database.
> I've used this so far:
> SELECT COUNT(*)
> FROM INFORMATION_SCHEMA.COLUMNS
> WHERE TABLE_NAME = 'pub_info'
> and doing this for each table in each database. However, this is going
> to take me three lifetimes. ;o)
> Is there a query that can output a grid listing the number of fields
> per each table in a selected database?
> If the query could simultaneously list the number of rows as well that
> would be a bonus, but not necessary.
> I REALLY appreciate any help sent my way.
> Cheers!
> Joel
>|||Just do a GROUP BY and you are there:
SELECT TABLE_NAME, COUNT(*) AS noOfColumns
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME
For number of rows, we need to know what version of SQL Server...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"JoelBarish" <JoelForBoards@.gmail.com> wrote in message
news:1159539224.878544.293450@.i42g2000cwa.googlegroups.com...
> Hi - I apologize in advance for the newbie question...
> I have been asked to gather some db statistics. We have 11 dbs and
> some have 70 or more tables.
> I need to gather is number of fields for each table in the database.
> I've used this so far:
> SELECT COUNT(*)
> FROM INFORMATION_SCHEMA.COLUMNS
> WHERE TABLE_NAME = 'pub_info'
> and doing this for each table in each database. However, this is going
> to take me three lifetimes. ;o)
> Is there a query that can output a grid listing the number of fields
> per each table in a selected database?
> If the query could simultaneously list the number of rows as well that
> would be a bonus, but not necessary.
> I REALLY appreciate any help sent my way.
> Cheers!
> Joel
>|||Thank you BOTH so much for your help.
I will take on the homework assignment suggested by Kalen as it is good
exercise for me as a novice.
We are using SQL Server 2000. -- You see! This is how much of a
newbie I am! I didn't even know it made a difference!! :o) If anyone
knows a quickie way to get the rows until I can get to my homework that
would be great. Just trying to get this information out the door to
mgmt for now...
Thanks!
Tibor Karaszi wrote:[vbcol=seagreen]
> Just do a GROUP BY and you are there:
> SELECT TABLE_NAME, COUNT(*) AS noOfColumns
> FROM INFORMATION_SCHEMA.COLUMNS
> GROUP BY TABLE_NAME
> For number of rows, we need to know what version of SQL Server...
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "JoelBarish" <JoelForBoards@.gmail.com> wrote in message
> news:1159539224.878544.293450@.i42g2000cwa.googlegroups.com...|||Does the row count need to be exact? If not, use the sysindexes system table
, something like:
SELECT OBJECT_NAME(i.id) AS table_name, i.rowcnt, COUNT(*) AS NoOfColumns
FROM sysindexes AS i
JOIN syscolumns AS c
ON i.id = c.id
WHERE i.indid IN(0,1) AND OBJECTPROPERTY(i.id, 'IsMSShipped') = 0
GROUP BY OBJECT_NAME(i.id), i.rowcnt
And before you run above, run (below should make rowcount exact):
DBCC UPDATEUSAGE('dbname') WITH COUNT_ROWS
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"JoelBarish" <JoelForBoards@.gmail.com> wrote in message
news:1159544274.480475.86270@.b28g2000cwb.googlegroups.com...
> Thank you BOTH so much for your help.
> I will take on the homework assignment suggested by Kalen as it is good
> exercise for me as a novice.
> We are using SQL Server 2000. -- You see! This is how much of a
> newbie I am! I didn't even know it made a difference!! :o) If anyone
> knows a quickie way to get the rows until I can get to my homework that
> would be great. Just trying to get this information out the door to
> mgmt for now...
> Thanks!
>
> Tibor Karaszi wrote:
>|||Joel,
I think I am a little more bored than the rest of them.
--This procedure will loop through the database list and load a table
-- in tempdb with the column count for each table.
USE tempdb
IF EXISTS ( SELECT name from sysobjects where name = 'table_specs' )
BEGIN
DROP TABLE table_specs
END
CREATE TABLE table_specs
( db varchar(200) NOT NULL
, tbl_name varchar(200) NOT NULL
, col_cnt int NULL )
DECLARE @.db_name varchar(200)
DECLARE @.count as int
DECLARE @.cmd varchar(2000)
SET @.cmd = ''
SET @.db_name = ''
DECLARE dbs CURSOR FOR
SELECT name FROM master..sysdatabases where name is not null
OPEN dbs
FETCH NEXT FROM dbs into @.db_name
WHILE @.@.FETCH_STATUS = 0
BEGIN
PRINT @.db_name
SET @.cmd = 'USE [' + @.db_name + ']'
PRINT @.cmd
EXECUTE ( @.cmd)
SET @.cmd = 'INSERT INTO tempdb..table_specs
SELECT ''' + @.db_name + ''', a.name, count(*)
FROM [' + @.db_name + ']..sysobjects a, [' + @.db_name + ']..syscolumn
s
b
WHERE a.id = b.id
AND a.type = ''U''
GROUP BY a.name'
EXECUTE (@.cmd)
PRINT @.cmd
FETCH NEXT FROM dbs into @.db_name
END
CLOSE dbs
DEALLOCATE dbs
-- SELECT db, sum(col_cnt) FROM tempdb..table_specs GROUP BY db
-- SELECT * FROM tempdb..table_specs

Monday, March 12, 2012

Nulls debate

Hi!
What's your opinion about whether null columns should ever allow nulls (or not)?
Thanks in advance for your comments.
We don't have to hash this out again, do we?
http://groups.google.com/groups?hl=e...A*sqls erver*
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Francisco" <anonymous@.discussions.microsoft.com> wrote in message
news:A2A108C8-BD9F-4B70-9921-A306FB506B5F@.microsoft.com...
> Hi!
> What's your opinion about whether null columns should ever allow nulls (or
not)?
> Thanks in advance for your comments.
>
|||If your business rules determine data for a column may or may not be present
then nulls should be allowed in your table design. Now on the flip side if
you business rules say there should always be data for a column, then you
should NEVER design your table to allow nulls since this violates your
business rule.
----
Need SQL Server Examples check out my website at
http://www.geocities.com/sqlserverexamples
"Francisco" <anonymous@.discussions.microsoft.com> wrote in message
news:A2A108C8-BD9F-4B70-9921-A306FB506B5F@.microsoft.com...
> Hi!
> What's your opinion about whether null columns should ever allow nulls (or
not)?
> Thanks in advance for your comments.
>
|||Sorry Aaron, I didn′t mean to offend you.
Sorry my ignorance. Perhaps I make a mistake to express my doubt.
|||Why do you think you offended me?
It's just a very common debate, and the google threads should provide you
with all the information that could possibly be offered again...
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/

Nulls debate

Hi!
What's your opinion about whether null columns should ever allow nulls (or n
ot)?
Thanks in advance for your comments.We don't have to hash this out again, do we?
ion+group%3A*sqlserver*" target="_blank">http://groups.google.com/groups?hl=...p%3A*sqlserver*
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Francisco" <anonymous@.discussions.microsoft.com> wrote in message
news:A2A108C8-BD9F-4B70-9921-A306FB506B5F@.microsoft.com...
> Hi!
> What's your opinion about whether null columns should ever allow nulls (or
not)?
> Thanks in advance for your comments.
>|||If your business rules determine data for a column may or may not be present
then nulls should be allowed in your table design. Now on the flip side if
you business rules say there should always be data for a column, then you
should NEVER design your table to allow nulls since this violates your
business rule.
----
----
--
Need SQL Server Examples check out my website at
http://www.geocities.com/sqlserverexamples
"Francisco" <anonymous@.discussions.microsoft.com> wrote in message
news:A2A108C8-BD9F-4B70-9921-A306FB506B5F@.microsoft.com...
> Hi!
> What's your opinion about whether null columns should ever allow nulls (or
not)?
> Thanks in advance for your comments.
>|||Sorry Aaron, I didn′t mean to offend you.
Sorry my ignorance. Perhaps I make a mistake to express my doubt.|||Why do you think you offended me?
It's just a very common debate, and the google threads should provide you
with all the information that could possibly be offered again...
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/