Re: Random Number Function
From: miron (nospam_miron.berlin2_at_verizon.net)
Date: 07/05/04
- Next message: roger: "selecting top rows of ordered values"
- Previous message: Aaron [SQL Server MVP]: "Re: INPUT type Table"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 05 Jul 2004 00:25:45 GMT
CAST( CAST( newid() AS binary( 4 ) ) AS INT ) % @modulo
use it inline --
"Randy" <randy@NoSpamForMe.com> wrote in message
news:107vl9294slo9f4@corp.supernews.com...
> Hi all.
>
> I have recently figured out how to do Stored Procedures and Triggers,
> but now I need to write a function, and what I wrote looks to me like it
> should work, but SQL Server doesn't like it.
>
> Here's the situation - I need to write data to a table, and it is done
> in one batch. Each line needs to have a unique number. Since I am
> doing a batch update to the table from MS Access, I can't use a counter
> variable and loop through, incrementing one at a time. I want a
> function that will select the max sequence number from the table I am
> writing to, and add 1 (or some number) to it, then write that number
> back into the table. Using a function, as far as I understand them,
> will do this, even in the midst of a batch update, because the function
> will get called every time a line is added to the table.
>
> Here is what I wrote:
> CREATE function dbo.F_SemiRandomNo(@Seed INT )
> RETURNS Double
> AS
> BEGIN
> DECLARE @Number INT
> DECLARE @RandomNumber DOUBLE
>
> SET @Number = (select max(SEQNUMBR) FROM ptest.dbo.ivc10300)
> RETURN @RandomNumber = @Number + @SEED
> END
>
> I don't particularly want to pass a number, but BOL seems to think that
> I should always pass a function something.
>
> Here is what Query Analyzer tells me when I parse this:
> Server: Msg 156, Level 15, State 1, Procedure F_SemiRandomNo, Line 4
> Incorrect syntax near the keyword 'AS'.
> Server: Msg 156, Level 15, State 1, Procedure F_SemiRandomNo, Line 9
> Incorrect syntax near the keyword 'SET'.
> Server: Msg 137, Level 15, State 1, Procedure F_SemiRandomNo, Line 10
> Must declare the variable '@RandomNumber'.
>
> Anyone that can help, anyone that is smarter then me (which is most of
> you) is much appreciated.
>
> Thanks,
> Randy
>
>
- Next message: roger: "selecting top rows of ordered values"
- Previous message: Aaron [SQL Server MVP]: "Re: INPUT type Table"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|