sah (do***********@hotmail.com) writes:
Can someone please help me write a stored procedure? I run a query
(see below) for 50 tables and for every day of the month. I would like
a SP which can provide data with results as follows:
Srv_Name, DB_Name, Table_Name, Date_Of_Records, Total_Records,
Min_SerNumber, Max_SerNumber, Should_Be_Total_Records, Missing_Records
Why are there 50 tables to start with? I don't know, but it sounds like a
bad table design to me.
Anyway, there are a couple of options, which one that works the best
depends your evironment. But my primary suggestion would be to create
a inlined table function that spans all these 50 databases. The body of
the function would look like:
select Srv_Name = 'SERVER1', DB_Name = 'db1',
Table_Name = 'info_table1', Date_of_Records = @date
count(*) AS 'Total Records' ,
min (SerNumber) AS 'MIN SerNumber'
max (SerNumber) AS 'MAX SerNumber',
max (SerNumber)- min (SerNumber)+1 AS 'Should be total Records',
count(*) - (max (SerNumber)- min (SerNumber)+1) AS 'Missing
Records'
from SERVER1.db1.dbo.info_table_01
WHERE DateTime >= @date and DateTime < dateadd(DAY, 1, @date)
UNION ALL
select Srv_Name = 'SERVER1', DB_Name = 'db2',
Table_Name = 'info_table2', Date_of_Records = @date
count(*) AS 'Total Records' ,
min (SerNumber) AS 'MIN SerNumber'
max (SerNumber) AS 'MAX SerNumber',
max (SerNumber)- min (SerNumber)+1 AS 'Should be total Records',
count(*) - (max (SerNumber)- min (SerNumber)+1) AS 'Missing
Records'
from SERVER1.db2.dbo.info_table_02
WHERE DateTime >= @date and DateTime < dateadd(DAY, 1, @date)
UNION ALL
...
I made this a table-valued function to accomodate for the date parameter,
but essentially this is the same as a view.
If the set of tables is fairly static, you could develop and maintain
this beast manually. If there are plenty of changes, you may prefer to
write a program that generates the function from an input list. Such
code could be written in T-SQL, but could just as well be written in
C, Visual Basic, Perl or whatever.
Note also, by the way, that I changed the condition for the DateTime
column. I figured that you want to include events from the last minute
of the day as well.
--
Erland Sommarskog, SQL Server MVP,
es****@sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp