Hi, I have been reading links relating to the fact that instead of using table variables, then I should be able to switch to using memory optimized tables. To do this, I am meant to define a type, which seems straightforward enough, but I am trying to convert a user defined function, and I can't seem to figure out the exact syntax that would allow me to convert it into using a memory optimized table, rather than a table variable. Can someone suggest how I would convert the below, so that the @temptable table variable can be converted to using a memory optimized table? The code itself just reads formulas with +'s and -'s in them, and converts them to a table that has the value, and whether it is positive or negative, if that matters. SQL Server is actually SQL Server 2016, but I don't see a forum appropriate for that...[code="sql"]/****** Object: UserDefinedFunction [dbo].[rfn_Split_VirtCons] Script Date: 6/20/2016 2:11:06 PM ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE FUNCTION [dbo].[rfn_Split_VirtCons_mod](@String NVARCHAR(MAX))RETURNS @temptable TABLE (items NVARCHAR(MAX), signs NVARCHAR(1), itemcount SMALLINT)ASBEGINDECLARE @delimiterneg nvarchar(1)DECLARE @delimiterpos nvarchar(1)SET @string = REPLACE(REPLACE(REPLACE(REPLACE(@string,'(',''),')',''),'[',''),']','')SET @delimiterneg = '-'SET @delimiterpos = '+'DECLARE @idx INTDECLARE @idxpos INT DECLARE @idxneg INT DECLARE @slice NVARCHAR(MAX)DECLARE @sign NVARCHAR(1)DECLARE @iterations intDECLARE @itemcount SMALLINT SET @idx = 1SET @iterations = 0 IF DATALENGTH(@String)/2<1 OR @String IS NULLRETURN WHILE @idx!= 0 BEGIN SET @iterations = @iterations+1 SET @idxpos = CHARINDEX(@delimiterpos, @string) SET @idxneg = CHARINDEX(@delimiterneg, @string)-- first iteration IF(@iterations = 1)SET @sign = '+'-- negative only if sign is negativeIF(LEFT(@string,1)='-' AND @iterations = 1 )SET @sign = '-'SELECT @idx = MIN(idx)FROM (SELECT CASE WHEN @idxpos = 0 THEN NULL ELSE @idxpos END AS [idx]UNION SELECT CASE WHEN @idxneg = 0 THEN NULL ELSE @idxneg END AS [idx]) AS tmp IF @idx!=0SET @slice = LEFT(@String, @idx -1)ELSE SET @slice = @stringIF (DATALENGTH(@slice)/2>0)INSERT INTO @temptable(items, signs)VALUES (@slice, @sign) SET @sign = LEFT (RIGHT(@String, DATALENGTH(@String)/2 - @idx +1 ),1)SET @String = RIGHT(@String, DATALENGTH(@String)/2 - @idx) IF DATALENGTH(@String)/2 = 0 BREAK IF @idxneg=0 AND @idxpos = 0 BREAK ENDSELECT @itemcount = COUNT(*)FROM @temptable UPDATE @temptable SET itemcount = @itemcountRETURNEND[/code]
↧