Started getting the above error on some servers that communicate via soap. I control both ends, so I was pretty sure it wasn't a code issue. After searching round and experimenting I discovered that the query that produced the results that the soap server was being asked to supply could, under heavy load conditions, take longer than 60s, and the connection was dropping at the 60s mark.
Turns out this is the default for the PHP ini variable default_socket_timeout.
I reset it to zero - bad move. 0 = timeout after zero seconds.
ini_set("default_socket_timeout",0);
I reset it to 3600 = an hour and all seems OK.
ini_set("default_socket_timeout",3600);
Note that the SOAP method is being called by a crontab-scheduled batch script, so waiting a l-o-n-g time for results is perfectly OK in this case. Might not be if there was a person on the other end! ;)
Thursday, 15 September 2016
Wednesday, 4 May 2016
How to use NEWID() in SQL Server User Defined Functions (UDF)
Using NEWID() in an SQL Server user defined function returns an error message:
Msg 443, Level 16, State 1, ...
Invalid use of a side-effecting operator 'newid' within a function.
Here is a trick to get round this. Create a simple view that returns one row containing one column:
CREATE VIEW dbo.vu_newid
AS
SELECT NEWID() as mynewid
Test this out to prove it is working and returns a different NEWID each time.
Then in your function, use the view to return a NEWID or set of NEWIDs as follows:
To populate a variable:
DECLARE @mynewid uniqueidentifier
...
SELECT @mynewid = mynewid FROM dbo.vu_newid
To use in ORDER BY:
SELECT mt.*, vid.mynewid as mynewid
FROM mytable mt
CROSS JOIN dbo.vu_newid vid
ORDER BY vid.mynewid
To use in ROW_NUMBER():
SELECT ROW_NUMBER() OVER(ORDER BY vid.mynewid) as rn,
mt.*
FROM mytable mt
CROSS JOIN dbo.vu_newid vid
Happy coding!
Msg 443, Level 16, State 1, ...
Invalid use of a side-effecting operator 'newid' within a function.
Here is a trick to get round this. Create a simple view that returns one row containing one column:
CREATE VIEW dbo.vu_newid
AS
SELECT NEWID() as mynewid
Test this out to prove it is working and returns a different NEWID each time.
Then in your function, use the view to return a NEWID or set of NEWIDs as follows:
To populate a variable:
DECLARE @mynewid uniqueidentifier
...
SELECT @mynewid = mynewid FROM dbo.vu_newid
To use in ORDER BY:
SELECT mt.*, vid.mynewid as mynewid
FROM mytable mt
CROSS JOIN dbo.vu_newid vid
ORDER BY vid.mynewid
To use in ROW_NUMBER():
SELECT ROW_NUMBER() OVER(ORDER BY vid.mynewid) as rn,
mt.*
FROM mytable mt
CROSS JOIN dbo.vu_newid vid
Happy coding!
Labels:
defined,
Error 443,
function,
GROUPING,
Microsoft,
MS Sql Server,
NEWID(),
operator,
Server,
side-effecting,
SQL,
SSIS,
user,
view
Subscribe to:
Posts (Atom)