![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Over the last couple of days, I have tried lots of ways to generate sequential numbers within a user defined function, including a stored proc to compute the next number and a function to return it. (I need this ability as part of a larger scientific package, and it needs to be a function). I get errors similar to this: "Only functions and extended stored procedures can be executed from within a function." Here is the latest non-working code I have after many changes. In essence, each time I call num_gen() function, I would like a new number to be returned, and which can be used from other functions: |
#3
| |||
| |||
|
|
A user-defined function cannot change state, so you are on the wrong track entirely. |
|
Since you say "it needs to be a function", I am not going to show you any code now. You first have to back and change that requirement. |
#4
| |||
| |||
|
|
The state changes are a by-product of my attempts to get it working in lieu of alternatives involving cursors. Other solutions that avoid the state change are most welcome. |
|
Well, it is needed in several places, and I was looking for a way to avoid copying code blocks all over the place and instead have something that could be named. If you think there is a better alternative, please do show some code. I can seek to relax the requirements on my end. |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
Say that you need to insert a number of rows in a table, with each row being assigned a unique, sequential, id, the typical construct is: ... |
|
Whether that fits in the context you are working, I don't know. Furthermore, this solution requires SQL 2005 or higher. |
#7
| |||
| |||
|
|
Here is another example: http://www.sqlmag.com/Article/Articl...er_101339.html |
#8
| |||
| |||
|
|
Unfortunately not. I use this technique elsewhere but what I need is the ability to generate such numbers one at a time. And sometimes, I need 2 or 3 of these at a time, depending on the results of the formulas I use in the package. |
![]() |
| Thread Tools | |
| Display Modes | |
| |