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 ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: <Author,,Name>-- Create date: <Create Date,,>-- Description: <Description,,>-- =============================================ALTER PROCEDURE [dbo].[spOther_ListTablesAndColumns] ASBEGIN -- 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 tableUSE [BPI]GO/****** Object: StoredProcedure [dbo].[spOther_CountRecordsTables] Script Date: 11/18/2011 11:51:35 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: <Author,,Name>-- Create date: <Create Date,,>-- Description: <Description,,>-- =============================================ALTER PROCEDURE [dbo].[spOther_CountRecordsTables] ASDECLARE @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 fieldUSE [BPI]GO/****** Object: StoredProcedure [dbo].[spOther_SumValues] Script Date: 11/19/2011 19:11:44 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: <Author,,Name>-- Create date: <Create Date,,>-- Description: <Description,,>-- =============================================ALTER PROCEDURE [dbo].[spOther_SumValues] ASDECLARE @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.