Justin Blog

O/R mapper fanboy

I'm feeling RANDy tonight and stored procedure deja vu

I'll be the first to admit that I'm a far cry from a SQL (Server or not) guru. So I'm at a loss as to what system destabilizing result you could generate from a UDF that you could not replicate in a stored procedure in which the RAND() function is called. Attempting to use RAND in a UDF would result in a non-deterministic result, that's a different result for the same input parameters. I googled for a few minutes to try and satisfy my curiousity but unfortunately paying work beckons me back. Luckily for me I was able to get my desired functionality with a sproc instead of a UDF, but I do think the UDF would have been cleaner. Oh well.

On a self referential note... I had one of those rare instances of having to use recursion in my T-SQL. I remember drifting in and out of boredom induced sleep during the recursive portion of my C++ classes but until now I had never actually needed it in a “real world” scenario.

Legacy Comments


Damian
2003-10-06
re: I'm feeling RANDy tonight and stored procedure deja vu
But did you dream about falling asleep in your C++ recursion classes while falling asleep in your C++ recursion classes ?


Jeff
2003-10-07
re: I'm feeling RANDy tonight and stored procedure deja vu
Damian -- i really enjoyed that one! and i really enjoyed that I enjoyed that one! very clever

Lavos
2003-10-09
re: I'm feeling RANDy tonight and stored procedure deja vu
The documentation clearly says that you can't, but I define a view with my builtin non-deterministic function as a calculated column, and then used a SELECT from the view to get the value.

Justin
2003-10-10
re: I'm feeling RANDy tonight and stored procedure deja vu
I'm curious, which off limits function/variable did you use? The following simple scalar UDF wont compile....

create function GetCurrentDate()
returns datetime
as
begin
return getdate()
end

Lavos
2003-10-13
re: I'm feeling RANDy tonight and stored procedure deja vu

Ahh, but that's where the View comes into play:

CREATE VIEW IllegalFunctions AS
SELECT GETDATE() as CurrentDate

/* put any other nondeterministic functions you would like to use as computed columns here also
*/

GO

create function GetCurrentDate()
returns datetime
as
begin
DECLARE @CurrentDate datetime
SELECT @CurrentDate=CurrentDate FROM IllegalFunctions

return @CurrentDate
end
GO

SELECT dbo.GetCurrentDate()

/*
Strangely, I can't find the place in the documentation that disallows calling derived columns that are based on the builtin nondeterministic functions, so I may have been misremembering another item.
*/

Prom Dress
2006-06-04
re: I'm feeling RANDy tonight and stored procedure deja vu
Thanks! Great site, interesting post!
===
my home:
http://yourpromdress.bravehost.com

Prom Dress
2006-06-04
re: I'm feeling RANDy tonight and stored procedure deja vu
Thanks! Great site, interesting post!
===
my home:
http://yourpromdress.bravehost.com