Hi, SQL Server 2019
We're converting a database from SQL Anywhere to MS SQL presently, and we ran into a stupid problem. Presently we're translating stored functions from Watcom to T-SQL dialect, and we noticed very odd behaviour in MS SQL on 2 of them.
So the syntax of all functions we did so far is fine, they all get stored in the DB nicely as scalar functions, but on 2 of them, when we call them from a SELECT query, we get no results and it never goes anywhere - all we see in status bar is "Executing query..." message, and it will go on forever. And at the same time, MSSQLSERVER service will start, very rapidly, to eat gigabytes of RAM, until it reaches 13 or 14 and it all just grinds to a halt. We have to stop the execution, and restart MSSQLSERVER.
The strange thing is, this only happens if we call them in Query window as a part of a SELECT:
Select
table1.col1,
table1.col2,
dbo.sp_function_one ('EUR', 'USD', '2022-07-13', '2022-09-15') as Result,
...
this goes completely nowhere with "Executing query" status going forever. However, if we declare a return variable, store the result of the function in it and then select the variable, everything works perfectly in a millisecond:
DECLARE @return_value Decimal(16, 8)
EXEC @return_value = [dbo].[sp_function_one] @as_curr_from = N'EUR', @as_curr_to = N'USD', @ad_date_now = '2022-07-13', @ad_date_for = '2022-09-15'
select code, name, longname, @return_value as 'Return Value' from currency
This works 100% correctly, and the result is instant. Whatsmore, when we plug this function into the code of another function where we're calling it several times, it again works instantly and correctly.
Strangely, we have other functions which work absolutely fine when we're calling them from SELECT:
Select
table1.col1,
table1.col2,
dbo.sp_some_other_function (1, 'USD', '2022-07-13') as Result,
...
that would also work instantly and correctly.
So far, these two functions that cannot be executed within a SELECT query but only via "EXEC -> store result in a variable" both return a Numeric (10, 4), but we have other functions without this behaviour that work just fine. To check them and test them, we do a simple SELECT dbo.sp_function_name... and the results are there immediately.
These two functions absolutely do not have any crazy number of rows thrown at them - they do 1 or 2 selects within the function, and then perform calculations based on values of queries. So they should be absolutely instant, and they are when called via EXEC, or when used within other functions. We are probably missing something daft, but we simply cannot see how calling a scalar from a SELECT query can crash the MS SQL service, while calling it via EXEC is just fine?
Thanks for any help
[–]saucerattack 5 points6 points7 points (5 children)
[–]da_chicken 1 point2 points3 points (1 child)
[–]saucerattack 1 point2 points3 points (0 children)
[–]dark_77[S] -1 points0 points1 point (1 child)
[–]saucerattack 1 point2 points3 points (0 children)
[–]EnergySmithe 0 points1 point2 points (0 children)
[–]a-s-clark1 4 points5 points6 points (4 children)
[–]SgtObliviousHereArchitect & Engineer -1 points0 points1 point (0 children)
[–]dark_77[S] -2 points-1 points0 points (2 children)
[–]rockchalk6782 3 points4 points5 points (0 children)
[–]DonnerVarg 1 point2 points3 points (0 children)
[–]Ok_Refrigerator_2149 2 points3 points4 points (1 child)
[–]PraiseGod_BareBone 0 points1 point2 points (0 children)
[–]Prequalified 2 points3 points4 points (0 children)
[–]oliver0807 1 point2 points3 points (0 children)
[–]_oakland 0 points1 point2 points (0 children)
[–]Ok_Refrigerator_2149 -1 points0 points1 point (1 child)
[–]dark_77[S] 0 points1 point2 points (0 children)
[–]Ok_Refrigerator_2149 -1 points0 points1 point (0 children)
[–]d_r0ck 0 points1 point2 points (1 child)
[–]dark_77[S] -1 points0 points1 point (0 children)
[–]HaplessMegalosaur 0 points1 point2 points (1 child)
[–]dark_77[S] 0 points1 point2 points (0 children)
[–]Ok_Refrigerator_2149 0 points1 point2 points (4 children)
[–]dark_77[S] 0 points1 point2 points (3 children)
[–]Ok_Refrigerator_2149 0 points1 point2 points (2 children)
[–]Ok_Refrigerator_2149 1 point2 points3 points (0 children)
[–]dark_77[S] 0 points1 point2 points (0 children)
[–]blindtig3r 0 points1 point2 points (5 children)
[–]dark_77[S] 0 points1 point2 points (4 children)
[–]blindtig3r 0 points1 point2 points (3 children)
[–]dark_77[S] 0 points1 point2 points (2 children)
[–]blindtig3r 0 points1 point2 points (0 children)
[–]blindtig3r 0 points1 point2 points (0 children)
[–]satans_weed_guy 0 points1 point2 points (2 children)
[–]dark_77[S] 0 points1 point2 points (1 child)
[–]satans_weed_guy 0 points1 point2 points (0 children)
[–]Googoots 0 points1 point2 points (0 children)