February 6, 2007 at 3:56 pm
I have a UDF (calculate business hours) that calls another UDF (get start and end of business hours per day) within it. SQL Server lists them both as non-deterministic.
This works:
DECLARE
@x as int
Exec @x = fnCalcBusHours 1, 1, '8/31/2004 10:18:30 PM', '9/5/2004 4:23:35 PM'
@x
This doesn't work:
select
Incident_Number, fnCalcBusHours(Cust_Id, Service_Id, open_time, close_time) from tablename
I need to return compliance based on business hours for several select statements which are bound to reports. Will post a larger section of code on request, but I'm pretty sure someone will be able to see my problem without it. Looks like I'm violating rules somewhere, but I don't know how to fix. Also tried same code in sproc form instead of udf and get this error: 'spCalcBusHours' is not a recognized built-in function name.
February 7, 2007 at 6:31 am
pretty much all functions have to be preceeded by the owner (usually dbo.)
this will work:
select Incident_Number, dbo.fnCalcBusHours(Cust_Id, Service_Id, open_time, close_time) from tablename
Lowell
February 7, 2007 at 11:00 am
Tried that but it didn't work. Get this error:
sg 557, Level 16, State 2, Line 1 Only functions and extended stored procedures can be executed from within a function.
Also tried the same with a stored procedure version and get this:
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.spCalcBusHours", or the name is ambiguous.
Search on sys.objects can find both:
fnCalcBusHours 1495676376 NULL 1 0 FN SQL_SCALAR_FUNCTION
spCalcBusHours 1527676490 NULL 1 0 P SQL_STORED_PROCEDURE
Any other suggestions? Again, I can post all the code if it will help.
February 7, 2007 at 11:16 am
does this return a value? this should do the same thing you did when assigning it to the @x variable.
SELECT dbo.fnCalcBusHours( 1, 1, '8/31/2004 10:18:30 PM', '9/5/2004 4:23:35 PM')
Lowell
February 7, 2007 at 11:28 am
Same error:
Msg 557, Level 16, State 2, Line 1 Only functions and extended stored procedures can be executed from within a function.
I think technically it should work fine. But it will only run under EXEC and not through SELECT, even if you provide specific values instead of variables. I can send you the code for both functions involved if it will help, but I don't want to post it here b/c it is rather lengthy. Please let me know. (by the way, your quote is great for my problem )
February 7, 2007 at 12:17 pm
based on the error messge, it sounds like the function is calling a sotred proc in it's body; if you want to send me a priv message witht eh function in it i'd be glad to look at it.
Lowell
February 7, 2007 at 12:26 pm
Yes, that's correct. I have dynamic-sql in the 2nd function, which from what I can find is not allowed. This is irritating b/c 1/2 the people on the web say 'oh yeah - you can do that' when the reality is you can't.
Dymanic SQL cannot be used in a function.
I don't want to waste your time, so I'll go back to the drawing board. I'll msg you private later if I can't get anywhere. thanks.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy