| Author |
Topic |
|
dirwin26
Yak Posting Veteran
81 Posts |
Posted - 2006-02-03 : 08:38:24
|
| Hey all,I am trying to create a stored procedure that returns a new recordset everytime I execute it. I have a table (A$) from which I want to grab records from column A(ticker). Each time I execute the sproc, I want to see the next value in column A. I have tried this using a cursor. The follow code returns all values found in column A instead of each value individually. How can I keep the cursor alive so that each new execute returns the next value. Can I use a global cursor or something?? Many thanks.///////CREATE PROCEDURE ssTEST ASDECLARE @ticker VARCHAR(128) DECLARE blp CURSOR FOR SELECT DISTINCT ticker FROM A$OPEN blp FETCH NEXT FROM blp INTO @tickerWHILE @@FETCH_STATUS = 0 BEGINSET NOCOUNT ONCREATE TABLE #RES(TICKER VARCHAR(128))INSERT INTO #RES (TICKER)SELECT @TICKERSELECT TICKERFROM #res FETCH NEXT FROM blp INTO @TICKERDROP TABLE #RESENDCLOSE blpDEALLOCATE blpGO//////// |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-02-03 : 08:44:22
|
| Post some sample data and the result you wantMadhivananFailing to plan is Planning to fail |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-02-03 : 08:45:42
|
| >> How can I keep the cursor alive so that each new execute returns the next value.You can't>> Can I use a global cursorno>> or something??yes - see my signature>> Many thanks.You're welcomeYou need to persist the data to keep track of the previous call. This either means saving the state or returning it with each call.You then have a choice of recreating the recordset with each call or persisting it wit hthe first call - the choiuce may depend on the volatility of the data and the resources needed to create the set.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-02-03 : 08:51:54
|
| [code]use Northwindcreate table #data( ticker varchar(128))insert into #data select FirstName from EmployeesResult : Andrew, Anne, Janet, Laura, Margaret, Michael, Nancy, Robert, StevenCREATE PROCEDURE ssTEST @ticker varchar(128)asbegin select top 1 ticker from #data where @ticker is null or ticker > @ticker order by tickerendexec ssTEST NULLResult : Andrewexec ssTEST 'Andrew'Result : Anneexec ssTEST 'Anne'Result : Janet[/code]----------------------------------'KH' |
 |
|
|
dirwin26
Yak Posting Veteran
81 Posts |
Posted - 2006-02-03 : 09:08:01
|
| Table:columnAabcdresult after first execute = aresult after second execute = bresult after third execute = c |
 |
|
|
dirwin26
Yak Posting Veteran
81 Posts |
Posted - 2006-02-03 : 09:08:40
|
| And I don't have the option of using input parameters...... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-02-03 : 09:11:36
|
quote: Originally posted by dirwin26 And I don't have the option of using input parameters......
any particular reason can't ?----------------------------------'KH' |
 |
|
|
dirwin26
Yak Posting Veteran
81 Posts |
Posted - 2006-02-03 : 09:15:34
|
| I am automating the input. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-02-03 : 09:21:12
|
| Why do you want to do this?Do you want to create alphanumeric primary key?MadhivananFailing to plan is Planning to fail |
 |
|
|
dirwin26
Yak Posting Veteran
81 Posts |
Posted - 2006-02-03 : 09:26:37
|
| nope, i want the user to see a new result each time they click the submit button on the web. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-02-03 : 09:32:52
|
| How do you define the new Result?Do you mean randomly select a record from table?MadhivananFailing to plan is Planning to fail |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-02-03 : 09:35:26
|
Is the stored procedure aware of who is calling it? In other words with many concurrent users each calling the sp 1 time do you still need a different result for each call? And what happens when all the values have been returned 1 time, should it start over at the beginning?How about a random 1 row?select top 1 ticker from a$ order by newid() Be One with the OptimizerTG |
 |
|
|
dirwin26
Yak Posting Veteran
81 Posts |
Posted - 2006-02-03 : 09:59:50
|
| Random is exactly what I need. It works perfectly. Thanks for all the suggestions. Much appreciated.Ciao |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-02-03 : 10:08:13
|
And there is no need of using cursor. You didnt specify the random selection in the question itself. Otherwise you might have got solution quickly MadhivananFailing to plan is Planning to fail |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-02-03 : 10:53:31
|
If we only the knew the right questions to begin with we probably wouldn't need any answers. Be One with the OptimizerTG |
 |
|
|
|