1

I need to be able to parse a string array with STRING_SPLIT on a SQL 2016 instance. The vendor has required the one user DB we have access to be set with a compatibility level of 100 while the system DBs are at 130. Can STRING_SPLIT and other system functions be referenced when the context is set to the 100-level DB?

3
  • No, not without dynamic SQL/procs used in the context of the other databases. Commented Nov 16, 2021 at 23:12
  • @ErikDarling Are you sure? What about sp_ms_marksystemobject Commented Nov 21, 2021 at 4:05
  • If you feel comfortable with suggesting it, sure. I haven't used it much, and only tried it once sort of recently. One thing I dislike about it is that there's no way to undo it without updating system views. Commented Nov 21, 2021 at 15:01

1 Answer 1

0

Since master has the correct compatibility level, you can always execute STRING_SPLIT there.

So we can create inline TVF functions to execute it, then mark them as system functions so you can access them anywhere.

USE master;
GO

CREATE FUNCTION STRING_SPLITV
(@string varchar(max), @separator char(1))
RETURNS TABLE AS RETURN

SELECT value FROM STRING_SPLIT(@string, @separator);
GO

CREATE FUNCTION STRING_SPLITN
(@string nvarchar(max), @separator nchar(1))
RETURNS TABLE AS RETURN

SELECT value FROM STRING_SPLIT(@string, @separator);
GO

EXEC sp_ms_marksystemobject 'STRING_SPLITV';
EXEC sp_ms_marksystemobject 'STRING_SPLITN';

Note that sp_ms_marksystemobject is an undocumented system procedure, use at your own risk.

You can now use these functions in every database without schema-qualifying

USE YourDB;
GO

SELECT * FROM STRING_SPLITV('a,b,c', ',');

If you do not use sp_ms_marksystemobject you will still have access, but must qualify it with database and schema. If you have no access to master on a regular basis, perhaps create it in model, then it will appear in tempdb on restart.

Note: This is a copy of a similar question and answer by me from Stack Overflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.