You can either use Uri.UnescapeDataString (in System
), in which case you will also need to do a Replace('+', ' ')
on the string before passing it to Uri.UnescapeDataString
, or if you would rather not bother with it, this function is available in the Free version of SQL# (which I am the author of).
Importing System.Web
is probably more work than it's worth. And in fact, it can be risky. There is a good reason that System.Web
is not in the "Supported Libraries" list that you linked to in the question: it is not guaranteed to work! You might run into situations, especially when dealing with non-US ASCII character sets, that do not behave as expected, and Microsoft will not fix it. So, unless you absolutely have to, you should be careful about adding non-supported DLLs. The DLLs in the "Supported" list have been fully tested and verified to work with SQL Server collations and any other environmental issues that are different between standard CLR running in Windows and the CLR running inside of SQL Server.
Here are some additional resources from Microsoft regarding several pitfalls to incorporating unsupported .NET Framework libraries:
A few notes about your code:
- Please do not use .NET types for parameters, input or output. Hence, change
string encodedXML
to be SqlString encodedXML
.
- This function is deterministic so should be marked as such, else you will suffer a performance hit. Add
IsDeterministic = true
to the SqlFunction
attribute.
You can discover all of these events that are in the current event log cycle using sp_readerrorlog
:
EXEC sys.sp_readerrorlog @p1 = 0, @p2 = 1, @p3 = N'OFFLINE';
You can cycle through values of @p1
if you don't find it in the current event log. By default you should be able to read the current and prior 6 error log files, so use 0-6 as the arguments there to go back as far as possible (on my system I could not get 0
/NULL
to aggregate across all log files; YMMV).
Will return something like this:
LogDate ProcessInfo Text
------------- ----------- ---------------------------------------------------------
yyyy-mm-dd... spid72 Setting database option OFFLINE to ON for database 'foo'.
There's a chance, of course, that the error log gets populated enough that the event(s) happened before the current set of error logs. In that case, you are out of luck. To keep a longer running history in the future, you can change the number of error logs that are kept. In Object Explorer, expand Management, right-click SQL Server Logs, and choose Configure. There you can change error log file recycling settings, including keeping up to the previous 99 files. Also see this answer.
Note that sp_readerrorlog
is undocumented and unsupported, though many people have written about it. In the end, the error log files are just plain text files, so you could write your own PowerShell, CLR etc. that just parses the files and returns the same information. You can determine where the error log files are for this instance using:
SELECT SERVERPROPERTY('ErrorLogFileName');
The files will be named ERRORLOG
, ERRORLOG.1
, ERRORLOG.2
, etc. You can go and open the files in a basic text editor to see the structure, though I would be cautious about opening the current file in use (ERRORLOG
).
Best Answer
You need to import the module using
import-module dbatools
See Properly importing the module
hope that helps you !
Also, what Scott commented is documented in the same link you have (just read the page carefully to see if you are not missing any steps):