473,624 Members | 2,543 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.Normalizati onTable

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 1720

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

Similar topics

5
18519
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 tried the following and playing around with the following: declare @valuePointer varbinary(16)
9
6605
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? DECLARE my_cursor CURSOR FOR SELECT sqlstatement from Sn_SalesReport declare @sql varchar(255), @total varchar(20) OPEN my_cursor FETCH NEXT FROM my_cursor INTO @sql
4
9258
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 a function and it gives me an option that I could write an extended stored procedure, but I don't have a clue of how to do it. To quickly fix the problem the only solution left in my case is to convert this recursive function into one recursive...
1
3368
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 changes back to default. I have tested setting it back to waiting after the call to run the report, - and it does get set. Has anyone else seen this wierd crystal bug I am also putting a message in the status bar - and this hold true the
1
2259
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 ListView, the cursor resets to the system arrow. Even if I set the cursor property of each control to the one loaded it just ignores it. The ContextMenu doesn't support a cursor property either.
2
1773
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 stays as an hourglass. It seems as though the cursor can only be set in the context of the form. I would like to set the cursor within my standard error handling module. See code below... Any ideas? Thanks, Dave
5
1963
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 cursor it will work over the form. I need it to stay with the same cursor over all objects. Any ideas of what I am doing wrong here? TIA
10
17337
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) stored in Me.Cursor. Or is Cursors.Default some process wide cursor shape? What is a correct statement?
4
1978
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 being changed to "Processing" and you want to show the Wait cursor. Public Sub UpdateStatusBar(ByVal Status As String, Optional ByVal Pointer As Long = -1) sbrMain.Panels("Status") = Status If Pointer <> -1 Then Me.MousePointer = Pointer
0
2708
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 * * the removal_eligibility_link table for all persons * * in all stages associated with the victim who has * * has had a specific legal status change * EXEC SQL EXECUTE
0
8179
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,...
1
8348
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,...
0
8493
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7176
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
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
5570
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
4084
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4187
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2613
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

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.