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

May 16th, 2006, 05:01 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 16th, 2006, 05:20 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

May 16th, 2006, 06:13 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thnx. I did not know that.
|
|

May 16th, 2006, 07:34 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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()
|
|

May 16th, 2006, 07:10 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Just out of curiosity, why do you need a function to return a random number?
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
|
|

May 19th, 2006, 06:20 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

May 19th, 2006, 07:18 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

May 19th, 2006, 08:47 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
No but You helped me because I was thinking about that problem too.I need to implement it in some web aplication.Thnx againg :)
|
|

May 19th, 2006, 11:07 AM
|
|
Friend of Wrox
|
|
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

May 19th, 2006, 11:18 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|
 |