473,553 Members | 2,899 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 1869

"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
8339
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
7274
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...
1
8549
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...
5
3766
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...
5
3810
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...
1
4343
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...
11
2783
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...
5
2286
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...
3
1376
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'...
0
7568
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...
0
7492
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...
0
7852
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6109
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5409
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...
0
3537
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1990
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
1
1106
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
808
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...

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.