Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Counting records and summing columns of all tables

Author  Topic 

piflechien73
Starting Member

2 Posts

Posted - 2011-11-20 : 04:54:44
Hi,

I try to build one SP that generates a table listing all tables & columns and counting rows of all tables and summing all columns.
That should give a big flat table with columns “ Table_Name”,”Column_Name”, “Rows_Total”, “Column_Sum”.


So far I caught a SP on the web listing tables and columns which works well:

USE [BPI]
GO
/****** Object: StoredProcedure [dbo].[spOther_ListTablesAndColumns] Script Date: 11/18/2011 11:50:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[spOther_ListTablesAndColumns]

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT C.Table_Catalog DB,C.Table_Schema, C.Table_Name, Column_Name, Data_Type

FROM Information_Schema.COLUMNS C JOIN Information_Schema.TABLES T

ON C.table_name = T.table_name

WHERE Table_Type = 'BASE TABLE'


END




I also developed 2 separate SP:

One counts rows of a table
USE [BPI]
GO
/****** Object: StoredProcedure [dbo].[spOther_CountRecordsTables] Script Date: 11/18/2011 11:51:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[spOther_CountRecordsTables]

AS
DECLARE @CountTable nvarchar(200)
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT @CountTable=COUNT(*) FROM [tblDistribution_base]
Print'There are '+@CountTable+' records'
END


The other code sums values of a field

USE [BPI]
GO
/****** Object: StoredProcedure [dbo].[spOther_SumValues] Script Date: 11/19/2011 19:11:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[spOther_SumValues]

AS
DECLARE @SumTable nvarchar(200)

BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT @SumTable=sum(value) FROM [tblDistribution_base]


Print'Total is '+@SumTable
END


I try to merge those three SP that can fetch all tables and fields and calculate all automatically without having to choose one specific table and/or column.

The result should be this one, I developed a similar solution in VBA MS Access (Click image link below):
[url]http://cjoint.com/11nv/AKukUwfHrgS.htm[/url]

As I am quite beginner in SQL Server and SP, I do not know how to do that.

Can somebody help me?


Thanks in advance.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-20 : 06:16:33
sorry didnt get whats tblDistribution_base. is that table containing all table information?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

piflechien73
Starting Member

2 Posts

Posted - 2011-11-20 : 07:45:04
quote:
Originally posted by visakh16

sorry didnt get whats tblDistribution_base. is that table containing all table information?




Hello,

Sorry if it was not clear. This table is just one of the user tables I created in the dB. I counted/summed for one table in the example, but of course I would like to automatize the process for all tables of the dB without naming them.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-20 : 10:59:30
quote:
Originally posted by piflechien73

quote:
Originally posted by visakh16

sorry didnt get whats tblDistribution_base. is that table containing all table information?




Hello,

Sorry if it was not clear. This table is just one of the user tables I created in the dB. I counted/summed for one table in the example, but of course I would like to automatize the process for all tables of the dB without naming them.



for that you need a cursor or looping logic to loop through tables.
If this is a one off activity then you can even make use of undocumented stored procedure sp_Msforeachtable for that in which case you dont need to loop.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -