Author |
Topic |
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2007-12-26 : 11:46:53
|
Guys,I have following scenario which I have to return next values when I pass the id of the table, no of next values.id checknum________________1 6002 300getnextcheck(1, 5) should give next 5 values 601, 602, 603, 604, 605getnextcheck(2, 3) should give next 3 values 301, 302, 303Is this possible with a database functionAny suggestions/inputs would helpThanks |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-26 : 12:04:30
|
A WHILE loop maybe?or SELECT d.CheckNum + z.NumberFROM Table1 AS dCROSS JOIN master..spt_values AS zWHERE d.ID = @Param1 AND z.Type = 'p' AND z.Number BETWEEN 1 AND @Param2 E 12°55'05.25"N 56°04'39.16" |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-12-26 : 21:54:49
|
Careful using sequence tables or BOOM!!! Hundreds of deadlocks. See the following...[url]http://www.sqlservercentral.com/Forums/Topic436388-145-1.aspx[/url]--Jeff Moden |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-27 : 01:29:13
|
or simply:-CREATE FUNCTION getnextcheck(@ID int,@Number int)RETURNS @Result TABLE (Number int )ASWHILE @Number >0BEGIN INSERT @Result SELECT checknum+1 FROM Table WHERE id=@ID SET @Number=@Number-1ENDGO |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-27 : 05:50:33
|
quote: Originally posted by Jeff Moden Careful using sequence tables or BOOM!!! Hundreds of deadlocks. See the following...http://www.sqlservercentral.com/Forums/Topic436388-145-1.aspx
SELECT d.CheckNum + z.NumberFROM Table1 AS d WITH (ROWLOCK)CROSS JOIN master..spt_values AS z WITH (NOLOCK)WHERE d.ID = @Param1 AND z.Type = 'p' AND z.Number BETWEEN 1 AND @Param2 E 12°55'05.25"N 56°04'39.16" |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-12-27 : 07:08:47
|
That's kinda where I went in the URL I mentioned, Peter... except I used a Tally table instead of spt_Values to increase the possible number of checks.The other thing is that this is a sequence table. I posted the only way I've ever seen to update the sequence table correctly without using an explicit transaction and without causing deadlocks on what turns out to be a real "hot spot" in the system.--Jeff Moden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-27 : 07:23:54
|
quote: Originally posted by Jeff Moden Careful using sequence tables or BOOM!!! Hundreds of deadlocks. See the following...[url]http://www.sqlservercentral.com/Forums/Topic436388-145-1.aspx[/url]--Jeff Moden
Why do I get this error despite giving the username and password?quote: You are not authorized to view this pageYou might not have permission to view this directory or page using the credentials you supplied. --------------------------------------------------------------------------------If you believe you should be able to view this directory or page, please try to contact the Web site by using any e-mail address or phone number that may be listed on the https://www.sqlservercentral.com home page.You can click Search to look for information on the Internet.HTTP Error 403 - Forbidden Internet Explorer
MadhivananFailing to plan is Planning to fail |
 |
|
graz
Chief SQLTeam Crack Dealer
4149 Posts |
Posted - 2007-12-27 : 08:07:28
|
(madhivanan,I updated your post and replaced the CODE tag with a QUOTE tag so it would wrap better so I could read it.-graz)=================================================Creating tomorrow's legacy systems today. One crisis at a time. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-27 : 08:17:57
|
quote: Originally posted by graz (madhivanan,I updated your post and replaced the CODE tag with a QUOTE tag so it would wrap better so I could read it.-graz)=================================================Creating tomorrow's legacy systems today. One crisis at a time.
Thanks Graz MadhivananFailing to plan is Planning to fail |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-12-27 : 11:25:22
|
quote: Why do I get this error despite giving the username and password?
Dunno... I'll take a look...--Jeff Moden |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-12-27 : 11:26:20
|
Heh... the answer is "ya gotta be a member". It's free and they don't spam your email...--Jeff Moden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-28 : 01:12:47
|
quote: Originally posted by Jeff Moden Heh... the answer is "ya gotta be a member". It's free and they don't spam your email...--Jeff Moden
Well. It is working now MadhivananFailing to plan is Planning to fail |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-12-28 : 02:30:43
|
Perfect... and sorry, I forgot you were already a member, Madhivanan...--Jeff Moden |
 |
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2007-12-28 : 10:56:48
|
I am getting the following error when I try to compile the function"Msg 156, Level 15, State 1, Procedure getnextcheck, Line 7Incorrect syntax near the keyword 'WHILE'."Thanksquote: Originally posted by visakh16 or simply:-CREATE FUNCTION getnextcheck(@ID int,@Number int)RETURNS @Result TABLE (Number int )ASWHILE @Number >0BEGIN INSERT @Result SELECT checknum+1 FROM Table WHERE id=@ID SET @Number=@Number-1ENDGO
|
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2007-12-28 : 11:06:08
|
you need to enclose the function code in a BEGIN/END block:CREATE FUNCTION getnextcheck(@ID int,@Number int)RETURNS @Result TABLE (Number int )ASbegin WHILE @Number >0 BEGIN INSERT @Result SELECT checknum+1 FROM [Table] WHERE id=@ID SET @Number=@Number-1 END returnendGO EDIT:you also need a "return" statement at the end.Be One with the OptimizerTG |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2007-12-28 : 11:17:41
|
Looks like you've got a problem with that code though. Nothing is updating your [table].checknum value so all the @result.number values will be the same.Be One with the OptimizerTG |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-12-28 : 13:17:23
|
scelamko,TG is correct and, like I said on the other forum... you can't do this with a function because you cannot tables external to the function... it's gotta be a stored proc...--Jeff Moden |
 |
|
|