Re: Random Number Function

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: miron (nospam_miron.berlin2_at_verizon.net)
Date: 07/05/04


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
>
>



Relevant Pages

  • Please Help with Transaction + Alter Procedure statement
    ... I am struggling with the following SQL. ... Incorrect syntax near the keyword 'PROCEDURE'. ... Must declare the variable '@mySomething'. ...
    (microsoft.public.sqlserver)
  • Re: WITH problem
    ... ROW_NUMBEROVERAS RowNumber, ... But now im trying to declare a variable @i before the select, ... Incorrect syntax near the keyword 'DECLARE'. ...
    (microsoft.public.sqlserver.programming)
  • Create Stored Procedures
    ... I am creating a few Stored Procedures after some tables and I always ... Incorrect syntax near the keyword 'procedure'. ... Must declare the scalar variable "@TotalCount". ...
    (microsoft.public.sqlserver.programming)
  • Re: [META] Talking about talking about C.
    ... with the fact that you can declare things of the type size_t after you ... information that size_t is probably an alias for a standard integer ... to reserve the name __SIZE_T as a keyword that's the name ...
    (comp.lang.c)
  • Re: Why not auto?
    ... auto keyword can not be used to ... >>> declare formal parameter. ... >> No storage class specifier except register can be used for function ... The keyword "static" is the factotum of the C Standard, ...
    (comp.lang.c)