473,765 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with query or possible Stored Procedure

sah
I need some help with the following query:

DECLARE @SRV VARCHAR(20), @date smalldatetime
SET @SRV = (select @@servername)
SET @date = '20040901'
select Srv_Name = @SRV, DB_Name = 'DB_NAME', Table_Name =
'Info_Table', Date_of_Records = @date,
count(*) AS 'Actual Total' ,
max (SER_NO)- min (SER_NO)+1 AS 'Desired total ',
count(*) - (max (SER_NO)- min (SER_NO)+1) AS 'Missing Records',
min (SER_NO) AS 'MIN SER_NO',
max (SER_NO) AS 'MAX SER_NO'
from Info_Table
where DateTime >= @date and DateTime < dateadd(DAY, 1, @date)

I would like to get records of next 30 days from the @date. I can copy
paste this query 30 times with different date and get the desired
results but would prefer to use one query only.

If possible would like a add a variable to get the table name by using
the following query into the above query:

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

So basic idea is to run one simple (or complexed) query to get 30 days
data of many tables select by the above query.
Can someone help please?
Jul 20 '05 #1
4 1879

"sah" <us****@gmail.c om> wrote in message
news:36******** *************** ***@posting.goo gle.com...

I would like to get records of next 30 days from the @date. I can copy
paste this query 30 times with different date and get the desired
results but would prefer to use one query only.


You need to group by the on the days to make this work. One way to do that
is using CONVERT and getting rid of the time portion that way. Another is
with DATEADD and DATEDIFF.

I think this should do it for you (using the DATEADD/DATEDIFF method):
DECLARE @SRV VARCHAR(20), @date smalldatetime
SET @SRV = (select @@servername)
SET @date = '20040901'
select Srv_Name = @SRV, DB_Name = 'DB_NAME', Table_Name =
'Info_Table', Date_of_Records = DATEADD(dd, DATEDIFF(dd, 0, [DateTime]), 0),
count(*) AS 'Actual Total' ,
max (SER_NO)- min (SER_NO)+1 AS 'Desired total ',
count(*) - (max (SER_NO)- min (SER_NO)+1) AS 'Missing Records',
min (SER_NO) AS 'MIN SER_NO',
max (SER_NO) AS 'MAX SER_NO'
from Info_Table
where DateTime >= @date and DateTime < dateadd(DAY, 30, @date)
GROUP BY DATEADD(dd, DATEDIFF(dd, 0, [DateTime]), 0)
Jul 20 '05 #2
sah
us****@gmail.co m (sah) wrote in message news:<36******* *************** ****@posting.go ogle.com>...
I need some help with the following query:

DECLARE @SRV VARCHAR(20), @date smalldatetime
SET @SRV = (select @@servername)
SET @date = '20040901'
select Srv_Name = @SRV, DB_Name = 'DB_NAME', Table_Name =
'Info_Table', Date_of_Records = @date,
count(*) AS 'Actual Total' ,
max (SER_NO)- min (SER_NO)+1 AS 'Desired total ',
count(*) - (max (SER_NO)- min (SER_NO)+1) AS 'Missing Records',
min (SER_NO) AS 'MIN SER_NO',
max (SER_NO) AS 'MAX SER_NO'
from Info_Table
where DateTime >= @date and DateTime < dateadd(DAY, 1, @date)

I would like to get records of next 30 days from the @date. I can copy
paste this query 30 times with different date and get the desired
results but would prefer to use one query only.

If possible would like a add a variable to get the table name by using
the following query into the above query:

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

So basic idea is to run one simple (or complexed) query to get 30 days
data of many tables select by the above query.
Can someone help please?


That worked, thanks.

Now to the second part of it. Please review the following query:

SELECT DISTINCT so.name
FROM sysobjects AS so
INNER JOIN syscolumns AS sc
ON so.id=sc.id
WHERE so.name like 'm_%' AND sc.name = 'DateTime' OR sc.name ='SER_NO'
I wrote this query to get table names using certain filters. How can I
use this in the previous query instad of the table name.

Also a minor thing. The query you helped me with gives records for one
months only, can you please guide me in getting records from greater
then the given date.

Thanks.
Jul 20 '05 #3

"sah" <us****@gmail.c om> wrote in message
news:36******** *************** ***@posting.goo gle.com...

Now to the second part of it. Please review the following query:

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

I wrote this query to get table names using certain filters. How can I
use this in the previous query instad of the table name.
Probably not a good idea; why don't you know the name of the table you
want to query? That sounds like a design flaw to me. If you must, you
could use dynamic SQL, but that query could return multiple rows... what
would you do in that case?
Also a minor thing. The query you helped me with gives records for one
months only, can you please guide me in getting records from greater
then the given date.


Sure, just add more than 30 days:

where DateTime >= @date and DateTime < dateadd(DAY, 30, @date)
--30 days
where DateTime >= @date and DateTime < dateadd(DAY, 60, @date)
--60 days
where DateTime >= @date and DateTime < dateadd(DAY, 900, @date)
--900 days...

Jul 20 '05 #4
sah
us****@gmail.co m (sah) wrote in message news:<36******* *************** ****@posting.go ogle.com>...
I need some help with the following query:

DECLARE @SRV VARCHAR(20), @date smalldatetime
SET @SRV = (select @@servername)
SET @date = '20040901'
select Srv_Name = @SRV, DB_Name = 'DB_NAME', Table_Name =
'Info_Table', Date_of_Records = @date,
count(*) AS 'Actual Total' ,
max (SER_NO)- min (SER_NO)+1 AS 'Desired total ',
count(*) - (max (SER_NO)- min (SER_NO)+1) AS 'Missing Records',
min (SER_NO) AS 'MIN SER_NO',
max (SER_NO) AS 'MAX SER_NO'
from Info_Table
where DateTime >= @date and DateTime < dateadd(DAY, 1, @date)

I would like to get records of next 30 days from the @date. I can copy
paste this query 30 times with different date and get the desired
results but would prefer to use one query only.

If possible would like a add a variable to get the table name by using
the following query into the above query:

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

So basic idea is to run one simple (or complexed) query to get 30 days
data of many tables select by the above query.
Can someone help please?


Thank you so much for all your help Adam. :-)
Jul 20 '05 #5

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

Similar topics

1
8347
by: Harald | last post by:
Hi, is it possible to create an "INSERT INTO ..... "Select from stored procedure" Query? I want to create an temporary table. In this table I want to enter the data, which I can get from an stored procedure. But in the FROM-clause a stored procedure is not allowed?
5
7293
by: Warren Wright | last post by:
Hi group, I have a select statement that if run against a 1 million record database directly in query analyzer takes less than 1 second. However, if I execute the select statement in a stored procedure instead, calling the stored proc from query analyzer, then it takes 12-17 seconds. Here is what I execute in Query Analyzer when bypassing the stored procedure:
1
8563
by: A.M. de Jong | last post by:
When I perform a query on a linked Oracle server in the Query analyser I have no prboblem' to perform this query. However, when I create this query in a stored procedure I get a compilation error when saving this procedure. (Not when compiling; it has no errors) Server: Msg 7405, Level 16, State 1, Line 1 Heterogeneous queries require the ANSI_NULLS and ANSI_WARNINGS options to be set for the connection. This ensures consistent query...
5
3776
by: Jarrod Morrison | last post by:
Hi All Im unsure of how to use vb to read the results of my stored procedure. Ive included the stored procedure at the end of this message for reference. Basically the stored procedure will first of all construct a select string based on the your computer name and perform this select on a table named Locations, next it will perform a similar search on a table called CustLocations. Normally it will return two recordsets in query analyzer...
5
3824
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 tables. The function is already implemented in the Client software and as UDF-compliant in MySQL and Oracle. Now there's just MS-SQL left... The problem now is for sure, I'm in need of a scalar return value (a varchar) composed out of a dynamic...
1
4363
by: Mike Chamberlain | last post by:
Hi all. I'm trying to extend the Microsoft Enterprise Library Data Access Application Block (http://msdn.microsoft.com/library/en-us/dnpag2/html/daab.asp?frame=true) to work with a Borland Interbase database. To do this, I copied and renamed the source files for working with a SQL Server database (SqlCommandWrapper.cs/SqlDatabase.cs to InterbaseCommandWrapper.cs/InterbaseDatabase.cs). I then changed all
11
2805
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field (strPubCity) for Publisher City. These two fields have a many-to-one relationship with tables, (tlkpPubName and tlkpPubCity) respectively. The lookup tables only have one field (strPubName and strPubCity), which is their primary key. I also have...
5
2299
by: moondaddy | last post by:
I have a website where cataloge pages are populated by calling a stored procedure on sql server. I use the sql data adapter's fill method to call this stored procedure and fill the dataset. about 6 to 15 times a day the server hangs and times out when this fill method is called. The SP is simple and uses very little resources (as tested using client statistics in query analyzer). Here's data from my error log which includes the...
3
1383
by: Pieter | last post by:
Hi, I have a View which contains (of course) several columns. Now I want to make a Stored Procedure that does a Select based on the parameters. For exemple: One time I want to select all the reocrds with ColumnA = 'Value A', an other time all the reords with ColumnB = 'An other value', a third time all records with "ColumnA = 'Value C' AND ColumnB = 'this value'" etc etc.
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.