all groups > sql server programming > december 2004 >
You're in the

sql server programming

group:

Cursor with Input parameters


Cursor with Input parameters Ejaz ul Haq
12/2/2004 10:49:04 PM
sql server programming:
Is it possible to create a cursor with some input parameter on which the
cursor query can be based, if so then what is the syntax of using that?
--
RE: Cursor with Input parameters Robert Davis
12/2/2004 11:25:03 PM
Either create a stored procedure or a user-defined function that accepts the
parameter.

Create Procedure dbo.MyProc
@Parameter1 int
As

Declare myCursor Cursor Fast_Forward Read_Only
For Select Field2, Field3
From MyTable
Where Field1 = @Parameter1

.......... and so on

Robert.

[quoted text, click to view]
RE: Cursor with Input parameters Ejaz ul Haq
12/2/2004 11:41:01 PM
Thanx for that but i already have this solution,
actually i have migrated from PL/SQL of Oracle to SQL Server,
thats y i was searching for the same concept of that in here.
Any way thanx.

[quoted text, click to view]
Re: Cursor with Input parameters Ejaz ul Haq
12/3/2004 1:45:05 AM
I have a TimeClass_tbl which contains the following fields.
[ID] [Name] [FromTime] [ToTime] [Date(just date, not time)]

I have to write a procedure with input of datetime datatype which can return
the ID of the specific entry matching that parameter. If the 'Date' column
can be null and on matching the date of supplied variable, it should return
the ID of that particular entry, but incase if there are more than one
records returned on matching this date, then i have to check that which entry
conforms to the time duration of 'FromTime' and 'ToTime', just for getting
those number of records and manipulating each record without employing any
extra burden of variables and one after another query i am taking cursors
into account.
If you have any better solution to this problem, i'll be thankfull to you.
--
Re: Cursor with Input parameters David Portas
12/3/2004 2:39:05 AM
The best way to post your table structure is as a CREATE TABLE statement,
including keys and constraints. For the moment I'll assume that your table
looks like this:

CREATE TABLE TimeClass (id INTEGER NOT NULL, name VARCHAR(20) NOT NULL,
fromtime DATETIME NOT NULL, totime DATETIME NOT NULL, date DATETIME NULL /*
??? PRIMARY KEY NOT SPECIFIED */)

In this case the query you want could be:

SELECT id
FROM TimeClass
WHERE (@dt >= date AND @dt < DATEADD(DAY,1,date))
OR (@dt >= fromtime AND @dt < totime)

But possibly your problem is due to you storing dates and times separately
(I'm guessing that based on your column names). That's not a design I would
recommend but if you post the table structure and some sample data too then
maybe we can understand your requirements better.

This article explains what information to post to stand the best chance of
getting help with your problem:

http://www.aspfaq.com/etiquette.asp?id=5006

Hope this helps.

--
David Portas
SQL Server MVP
Re: Cursor with Input parameters David Portas
12/3/2004 8:00:12 AM
The best way to do it in Oracle may not be the best way in SQL Server.
Cursors in SQL Server are slow which is another good reason to avoid cursors
altogether where you can. If you give us some detail on your problem maybe
someone can suggest a set-based solution instead.

--
David Portas
SQL Server MVP
--

AddThis Social Bookmark Button