Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 16th, 2006, 05:01 AM
Authorized User
 
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to joxa83 Send a message via Yahoo to joxa83
Default rand function problem

I need to call rand functino within another function but server raises an error.For example I can't create followng function

create function dbo.test()
returns float
as
begin
declare @i float
select @i = rand()
return @i
end


 
Old May 16th, 2006, 05:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You cannot call a nondeterministic function from within a user defined function. Examples of nondeterministic functions are GETDATE, NEWID, and, alas, RAND.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old May 16th, 2006, 06:13 AM
Authorized User
 
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to joxa83 Send a message via Yahoo to joxa83
Default

thnx. I did not know that.

 
Old May 16th, 2006, 07:34 AM
Authorized User
 
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to joxa83 Send a message via Yahoo to joxa83
Default

i came uo with this alternative:

create view vw_test
as
select 1 value1, rand() value2

alter function dbo.test()
returns float
as
begin
declare @i float
set @i = (select value2 from vw_test)
return @i
end

select dbo.test()

 
Old May 16th, 2006, 07:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Just out of curiosity, why do you need a function to return a random number?

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old May 19th, 2006, 06:20 AM
Authorized User
 
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to joxa83 Send a message via Yahoo to joxa83
Default

I need something that will return random number in some interval
like

select myradnom(i1.first,i2.second) from mytable

I need this for creating some examples.

 
Old May 19th, 2006, 07:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

OK. The reason I asked was if you wanted to return a set of rows selected randomly from a table, you can use the NEWID() function, as for example if you needed to select 5 random rows:
Code:
      SELECT TOP 5 *
        FROM mytable
    ORDER BY NEWID();
I've used this trick for generating audit data...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old May 19th, 2006, 08:47 AM
Authorized User
 
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to joxa83 Send a message via Yahoo to joxa83
Default

No but You helped me because I was thinking about that problem too.I need to implement it in some web aplication.Thnx againg :)
 
Old May 19th, 2006, 11:07 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
Default

In 2005 there is the TABLESAMPLE keyword that can be used the in FROM clause, although you can't use it inside of a function or a view.

David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com
 
Old May 19th, 2006, 11:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by David_the_DBA
 In 2005 there is the TABLESAMPLE keyword that can be used the in FROM clause ...
Yeah, but as I understand the way it works, TABLESAMPLE either includes or excludes entire pages in the resultset. Depending on the size of your table rows, and the distribution of values in the clustered index/heap, this may be far from a random sampling of the data.

Even using the NEWID() function, I've had auditors question the ramdomness of a GUID...

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Function problem beetle_jaipur Classic ASP Basics 5 July 6th, 2006 01:08 PM
Msgbox Function Problem theokrtz Classic ASP Basics 4 July 4th, 2006 03:35 AM
Problem with function mat41 Classic ASP Basics 3 July 16th, 2005 01:19 AM
shell function problem bml Excel VBA 1 February 12th, 2005 05:42 PM
Problem with a Javascript function SuMajestad PHP How-To 4 August 17th, 2004 09:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.