tags:

views:

24

answers:

1

hi.

May I use variable to declaring cursors??

I want to creating dynamic cursor, how can i do this??

Regards

I have table: CREATE TABLE [UsersUniTask] ( [id] uniqueidentifier CONSTRAINT [DF_UsersUniTask_id] DEFAULT newid() NOT NULL, [userUniID] uniqueidentifier NOT NULL, [taskID] int NOT NULL, [time] datetime NOT NULL, [doTask] int NOT NULL, [priority] int NOT NULL, CONSTRAINT [PK_UsersUniTask] PRIMARY KEY CLUSTERED ([id]), CONSTRAINT [FK_UsersUniTask_UsersUni] FOREIGN KEY ([userUniID]) REFERENCES [UsersUni] ([id]) ON UPDATE NO ACTION ON DELETE CASCADE ) ON [PRIMARY] GO

+2  A: 

Can you explain more about what you mean? If you are talking about declaring the cursor in a dynamic context as in the following example, then yes you can:

DECLARE @i int -- variable input
DECLARE @valuableData int

SET @i = 1 -- value for that input, this could be set by a query

DECLARE cursorFoo CURSOR FOR
    SELECT valuableData
    FROM myTable
    WHERE someParameter = @i

OPEN cursorFoo 

WHILE (1=1)
BEGIN
    FETCH NEXT FROM cursorFoo 
         INTO @valuableData

    IF (@@FETCH_STATUS <> 0) BREAK

    SELECT @valuableData -- Do something with your data
END

CLOSE cursorFoo

EDIT due to discussion in comments

You should have two separate program loops here

Loop 1:

  1. Webservice adds tasks to permanent table with information on priority.

Loop 2:

  1. Server script queries permanent table for most important task
  2. Server script removes task from table and processes task (or hands that information off to a script that does the work
  3. Server script goes back to permanent table and looks for most important task

SQL is meant to store data, not loop around and process it. The processing should be done with a server script. That script should get the data from the database. You start to have the concurrency issues you have having right now when SQL is writing and reading and looping through the same temporary table concurrently. You can do processing in SQL, but you should only use temp tables for data that is relevant only to that particular process.

Boerema
I have recurrency procedure with cursor, and I have got an error that cursor exists, and i want to create dynamic cursors. I must create dynamic name of cursors, which was unique. I want to do something like this:Declare @CursorName nvarchar(500) set @CursorName = 'AddTaskCursor'; DECLARE @CursorName CURSOR FAST_FORWARD READ_ONLY FOR ...But in this way i can't :(
dzajdol
No, you cannot declare a cursor name dynamically. You can only dynamically define the cursor's parameter. If you are having issues with the cursor not closing after the script runs, add "DEALLOCATE cursorName" after your CLOSE statement.
Boerema
my cursor looks like this:DECLARE AddTaskCursor CURSOR FAST_FORWARD READ_ONLY FOR (SELECT id, userUniID, taskID, doTask, priority FROM @Tasks) Open AddTaskCursor --TODO ZROBIĆ WYWOŁANIE PROCEDURY --In this place i execute procedure in with this cursor is - and this procedure cannot declare cursor with name which in use CLOSE AddTaskCursor; DEALLOCATE AddTaskCursor;As you see a have Close AND Deallocate
dzajdol
Maybe other solutions for my problem:I want to execute procedure for all records in temporary table
dzajdol
Can you post your code? I am very confused as to what it looks like. If you are trying to recursively call a stored procedure, that is just a bad idea. Calling cursors inside of cursors is also a bad idea.
Boerema
yes I call recursively a stored procedure (with cursors) and therefore I have the cursor in cursor :(This procedure must add Task in time (in each time maybe one task). That i used recursievly procedure. If one task are save to do in this time a execute procedure with new time. Task has priprity and i check which task more important, and important task are save to do in time that his wants, less important are save to do after important task. If in new time are task i must do this same procedure (check which task are more important and so on)What other solution for this situation you see?
dzajdol
I may do this in my Webservice in c#, but I thing taht do this in sql are faster. I may get task form DB for x time and save new list. Is this solution are better??
dzajdol
The adding of tasks and the processing of tasks should be different stored procedures entirely (and they should definitely have different cursor names associated with them). The procedure that runs the tasks doesn't need a cursor, it should just be called continually (either by a cron job or something else). It should take the top tasks from the queue, ordered by priority, and do whatever is needed. If need be, this can be run as a blocking procedure to prevent more than one instance of the procedure from running at any time.
Boerema
But i don't do any task a create only list of task to do for other program, who communicate for DB by webservices. This procedure must add task to list. Task has priority and time needed to perform. In this procedura i want to add task to list. Task in lower priority must do in time that it wants, moving the task with higher priority for later (and so I decided on a recursively stored procedure). Cursors are ther because task to move maybe more then one. I don't find better solutions.Any other idea??
dzajdol
SQL shouldn't be used to execute tasks like this. You should have a permanent table that your webservice writes to when a new task needs to be queued. Then have a cron or server script pick up the most important tasks each time it completes an old one. SQL should be used for storage and referencing, not infinite looping and processing. I'll edit this answer with more suggestions.
Boerema
but adding taks maybe have time now + 30 seconds, and cron which are execute for one hour(one minute) doesn't solve the problem.Assumptions:- i need current list of task after each add new task- task must have priority, time needed to perform task- For each time one task may be plan to doingI edit post and put there my table - each users (UserUniId) has own tasks list.
dzajdol
You could have a script that runs infinitely (have it pause for 2 seconds if you want to make sure your server doesn't commit suicide. Either way, the script can pick up tasks in a very timely manner. And if that script does its processing asynchronously, you can have it pick up a new task as quickly as it can loop around. This is done MUCH better by a server script (C++, Shell, Bash, PERL, whatever) than by SQL itself. This will be my last comment because I honestly have nothing more to add to this discussion.
Boerema
ok thanks for sugestions
dzajdol