473,499 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert cursor to set-based sql statement - HELP

1 New Member
Hi everyone,

I am having problem/troubles with creating an sql inline statement for our application. How do I create a statement/query that selects list of servers with defined date ranges, but once a server of the same name has date ranges within the date range of another, I will have to get the date range that contains them all. For easy understanding here's a snapshot of data (the Before and After screenshots).

BEFORE:

Id ServerName DateFrom DateTo
1 APAXSG1 01/01/2007 01/31/2007
2 APAXSG1 01/15/2007 02/15/2007
3 APAXSG1 02/01/2007 05/31/2007
4 APAXSG1 06/01/2007 07/01/2007
5 APAXSG1 07/02/2007 07/31/2007
6 APAXSG2 04/01/2007 05/01/2007
7 APAXSG2 04/15/2007 06/30/2007
8 APAXSG2 08/25/2007 09/25/2007
9 APAXSG3 09/01/2007 10/01/2007
10 APAXSG3 11/01/2007 12/25/2007


AFTER:

Id ServerName DateFrom DateTo
1 APAXSG1 01/01/2007 05/31/2007
2 APAXSG1 06/01/2007 07/01/2007
3 APAXSG1 07/02/2007 07/31/2007
4 APAXSG2 04/01/2007 06/30/2007
5 APAXSG2 08/25/2007 09/25/2007
6 APAXSG3 09/01/2007 10/01/2007
7 APAXSG3 11/01/2007 12/25/2007



I was helped by a friend and suggested the following statement to me:
DECLARE @Source TABLE (Id INT, ServerNm VARCHAR(100), DateFrom DATETIME, DateTo DATETIME)
INSERT INTO @Source SELECT * FROM dbo.NormalizationTable

DECLARE @Target TABLE (ServerNm VARCHAR(100), DateFrom DATETIME, DateTo DATETIME)
DECLARE @ServerNm VARCHAR(100), @DateFrom DATETIME, @DateTo DATETIME
DECLARE normalize CURSOR FOR
SELECT ServerNm, DateFrom, DateTo
FROM @Source
ORDER BY DateFrom, DateTo
OPEN normalize
FETCH NEXT FROM normalize INTO @ServerNm, @DateFrom, @DateTo
WHILE @@FETCH_STATUS = 0
BEGIN
IF (SELECT COUNT(*)
FROM @Target
WHERE ServerNm = @ServerNm
AND (DateFrom BETWEEN @DateFrom AND @DateTo OR DateTo BETWEEN @DateFrom AND @DateTo)) = 0
INSERT INTO @Target VALUES (@ServerNm, @DateFrom, @DateTo)
ELSE
UPDATE @Target
SET DateFrom = CASE WHEN DateFrom < @DateFrom
THEN DateFrom
ELSE @DateFrom END,
DateTo = CASE WHEN DateTo > @DateTo
THEN DateTo
ELSE @DateTo END
WHERE ServerNm = @ServerNm
AND (DateFrom BETWEEN @DateFrom AND @DateTo OR DateTo BETWEEN @DateFrom AND @DateTo)
FETCH NEXT
FROM normalize
INTO @ServerNm, @DateFrom, @DateTo
END
CLOSE normalize
DEALLOCATE normalize

SELECT * FROM @Target
ORDER BY ServerNm, DateFrom, DateTo



- - But upon researching, cursors can be performance issues esp for large data. How could I convert tha above statement to a non-cursor one?

Kindly help me.. thanks a lot!!
Nov 21 '07 #1
0 1708

Sign in to post your reply or Sign up for a free account.

Similar topics

5
18493
by: Cally | last post by:
Hello, I would like to convert a field from ntext field found in one database table to float field found in another database table. The reason why I want to do this is a long one. I have...
9
6596
by: Rodusa | last post by:
I am trying to assign @sql variable to @total, where @sql is a sql statement stored on the database, however what I am getting is its string value and not its calcuation. Could anybody help? ...
4
9247
by: Rodusa | last post by:
I am having problem to apply updates into this function below. I tried using cursor for updates, etc. but no success. Sql server keeps telling me that I cannot execute insert or update from inside...
1
3348
by: | last post by:
Hi, I am loading some crystal reports in a method similar to this (see below). And set the cursor to waiting while the reoprt is loaded, run and shown. BUT while the report is running the cursor...
1
2235
by: ilPostino | last post by:
I'm using this function to load a custom cursor from a .cur file; public static extern IntPtr LoadCursorFromFile(String str); It works great but if I popup a ContextMenu or Control like a...
2
1765
by: dave | last post by:
In my form Ive got a SaveData() routine that saves changes to a DB. When I encounter an exception in the save operations, I am having trouble chaning the cursor back to the default cursor, it just...
5
1953
by: Lespaul36 | last post by:
I have a mdi app. I need to change the cursor when I click on certain button on a form that I made into a toolbar. I tried to use cursor.current. But the cursor won't show. if I change the form...
10
17291
by: Just Me | last post by:
Does Me.Cursor.Current=Cursors.WaitCursor set the current property of Me.Cursor to Cursors.WaitCursor And Me.Cursor.Current=Cursors.Default set the Me.Current property to something (default)...
4
1974
by: Al Reid | last post by:
I have a simple function that I use in many of my applications. It allows one to update the "Status" panel of a status bar and optionally set the MousePointer. This is useful if the status is...
0
2697
by: tickle | last post by:
Need to convert this PL/SQL script to Dynamic SQL Method 2 * copybook - celg02u3.sql SIR 24265 * * updates dt_deny for all rows in * * ...
0
7128
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
7006
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
7215
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...
1
6892
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
7385
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
4597
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...
0
3096
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...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.