473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Need help with Stored Procedure

sah
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
The query I currently run for each table and each single data is
below:

set nocount on
PRINT 'Table: info_table_01
DateTime: BETWEEN 01 sep 2004 00:00 and 01 sep 2004 23:59 '
go
select
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 info_table_01
WHERE DateTime BETWEEN '01 sep 2004 00:00' and '01 sep 2004 23:59'
go
RESULTS:

Table: info_table_01
DateTime: BETWEEN 01 sep 2004 00:00 and 01 sep 2004 23:59
TotalRec MinSerNum MMaxSerNum ShouldBeTotalRec MisRec
-------- --------- ---------- ---------------- -------
69023 00054 69090 69027 4
Any help would be very appriciate.

Thanks
Jul 20 '05 #1
3 1351
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
Jul 20 '05 #2
Thanks. I am using only one SQL Server but 50 tables. I need to run the
query every day and for all 50 tables.

Is there a way I can combine the list of all tables from such a query:

SELECT DISTINCT so.name
FROM sysobjects AS so
INNER JOIN syscolumns AS sc
ON so.id=sc.id
WHERE so.name like 't_%' AND sc.name = 'DateTime' AND sc.name
='SerNum'.

Also is there a way to specify how many days I want the records of i.e.
3 days, 7 days or 30 days etc.,

BTW the above query has a little issue too, I have mutiple WHERE filter,
I can not have more then one "AND", currently I am using only one "AND"
and one "OR".

Thanks for your help
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
You also posted this same question to microsoft.public.sqlserver.server.
Please don't post the same question independently to multiple groups as this
causes duplication of effort.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"s sah" <do***********@hotmail.com> wrote in message
news:41**********************@news.newsgroups.ws.. .
Thanks. I am using only one SQL Server but 50 tables. I need to run the
query every day and for all 50 tables.

Is there a way I can combine the list of all tables from such a query:

SELECT DISTINCT so.name
FROM sysobjects AS so
INNER JOIN syscolumns AS sc
ON so.id=sc.id
WHERE so.name like 't_%' AND sc.name = 'DateTime' AND sc.name
='SerNum'.

Also is there a way to specify how many days I want the records of i.e.
3 days, 7 days or 30 days etc.,

BTW the above query has a little issue too, I have mutiple WHERE filter,
I can not have more then one "AND", currently I am using only one "AND"
and one "OR".

Thanks for your help
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shawn Fletcher | last post by:
Hi, I'm trying to work around a bug that our helpdesk software has. When a new issue is created, it cannot automatically default 2 fields to the value of No like we need it to. I have a...
5
by: Ralph | last post by:
Hi all, I'm a newbie to MS-SQL UDFs and seem to have a real big problem. I need to implement a logic to receive an adress build out of various user definable fields from various user defined...
0
by: Doug R | last post by:
Hello, I have a system that I am writing to automaticly import Credit Transaction data into a SQL Server 2000 Database. I am using a VB.Net application to detect when the file arives and prep...
4
by: Rhino | last post by:
Is it possible for a Java Stored Procedure in DB2 V7.2 (Windows) to pass a Throwable back to the calling program as an OUT parameter? If yes, what datatype should I use when registering the...
1
by: Raquel | last post by:
Have a question on the Stored procedure method code generated by DB2 development center for Java stored procedures. Suppose I have a requirement to return the resultset consisting of FIRSTNME,...
3
by: Ellie O'Donnell | last post by:
Hi, I wrote a DB2 stored procedure in COBOL, and now I need to prepare a CLIST in order to call it. This CLIST would be for people who want to use the stored procedure outside of COBOL or Pl/I....
1
by: Jim H | last post by:
I am on a project where I am supposed to send an XML document to a SQL Server stored procedure. The XML Doc is a list strings. If in my c# function I get a list values as (string psValueList),...
3
by: Jack Black | last post by:
Help!! I'm trying to call a custom stored procedure from a VB.Net code-behind page in an ASP.Net application, and I keep getting an error with no real helpful info... Basically, I'm accepting a...
5
by: byahne | last post by:
We just went live today with a production SQL Server 2005 database running with our custom Java application. We are utilizing the jTDS open source driver. We migrated our existing application...
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.