How can I convert split function to inline table valued udf in SQL server?











up vote
1
down vote

favorite
1












Assuming I have this query ( pseudo) :



Select T.a,
T.b,
(select top 1 element from fn_split(c,',') where element=T.element)
From largeTable T


Where fn_split runs for each row , I would like to use inline table valued udf so , that performance will be better.



NB : fn_split just create a table via splitting via , :



enter image description here



But looking at inline table valued udf structure :



create FUNCTION [dbo].[fn_...]
(
...
)
RETURNS table
AS
RETURN SELECT ...(!!!)


It should return the select right away as the first statement !



But what if my UDF looks like :



CREATE FUNCTION [dbo].[FN_Split] 
(
@InDelimitedString varchar(max),
@InDelimiter varchar(10)
)
RETURNS
@tblArray TABLE
(
ElementID smallint IDENTITY(1,1),
Element varchar(1000)
)

AS
BEGIN

DECLARE @StrPos smallint,
@StrStart smallint,
@DelimiterLength smallint

SET @DelimiterLength = LEN(@InDelimiter)

WHILE LEN(@InDelimitedString) > 0
BEGIN
--Removed for clarity . do some CHARINDEX manipulation ETc.
END

RETURN
END


Question :



I can't return select right away , but still , I want to change the fn_split to inline table valued udf.



How can I do it ?










share|improve this question


























    up vote
    1
    down vote

    favorite
    1












    Assuming I have this query ( pseudo) :



    Select T.a,
    T.b,
    (select top 1 element from fn_split(c,',') where element=T.element)
    From largeTable T


    Where fn_split runs for each row , I would like to use inline table valued udf so , that performance will be better.



    NB : fn_split just create a table via splitting via , :



    enter image description here



    But looking at inline table valued udf structure :



    create FUNCTION [dbo].[fn_...]
    (
    ...
    )
    RETURNS table
    AS
    RETURN SELECT ...(!!!)


    It should return the select right away as the first statement !



    But what if my UDF looks like :



    CREATE FUNCTION [dbo].[FN_Split] 
    (
    @InDelimitedString varchar(max),
    @InDelimiter varchar(10)
    )
    RETURNS
    @tblArray TABLE
    (
    ElementID smallint IDENTITY(1,1),
    Element varchar(1000)
    )

    AS
    BEGIN

    DECLARE @StrPos smallint,
    @StrStart smallint,
    @DelimiterLength smallint

    SET @DelimiterLength = LEN(@InDelimiter)

    WHILE LEN(@InDelimitedString) > 0
    BEGIN
    --Removed for clarity . do some CHARINDEX manipulation ETc.
    END

    RETURN
    END


    Question :



    I can't return select right away , but still , I want to change the fn_split to inline table valued udf.



    How can I do it ?










    share|improve this question
























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      Assuming I have this query ( pseudo) :



      Select T.a,
      T.b,
      (select top 1 element from fn_split(c,',') where element=T.element)
      From largeTable T


      Where fn_split runs for each row , I would like to use inline table valued udf so , that performance will be better.



      NB : fn_split just create a table via splitting via , :



      enter image description here



      But looking at inline table valued udf structure :



      create FUNCTION [dbo].[fn_...]
      (
      ...
      )
      RETURNS table
      AS
      RETURN SELECT ...(!!!)


      It should return the select right away as the first statement !



      But what if my UDF looks like :



      CREATE FUNCTION [dbo].[FN_Split] 
      (
      @InDelimitedString varchar(max),
      @InDelimiter varchar(10)
      )
      RETURNS
      @tblArray TABLE
      (
      ElementID smallint IDENTITY(1,1),
      Element varchar(1000)
      )

      AS
      BEGIN

      DECLARE @StrPos smallint,
      @StrStart smallint,
      @DelimiterLength smallint

      SET @DelimiterLength = LEN(@InDelimiter)

      WHILE LEN(@InDelimitedString) > 0
      BEGIN
      --Removed for clarity . do some CHARINDEX manipulation ETc.
      END

      RETURN
      END


      Question :



      I can't return select right away , but still , I want to change the fn_split to inline table valued udf.



      How can I do it ?










      share|improve this question













      Assuming I have this query ( pseudo) :



      Select T.a,
      T.b,
      (select top 1 element from fn_split(c,',') where element=T.element)
      From largeTable T


      Where fn_split runs for each row , I would like to use inline table valued udf so , that performance will be better.



      NB : fn_split just create a table via splitting via , :



      enter image description here



      But looking at inline table valued udf structure :



      create FUNCTION [dbo].[fn_...]
      (
      ...
      )
      RETURNS table
      AS
      RETURN SELECT ...(!!!)


      It should return the select right away as the first statement !



      But what if my UDF looks like :



      CREATE FUNCTION [dbo].[FN_Split] 
      (
      @InDelimitedString varchar(max),
      @InDelimiter varchar(10)
      )
      RETURNS
      @tblArray TABLE
      (
      ElementID smallint IDENTITY(1,1),
      Element varchar(1000)
      )

      AS
      BEGIN

      DECLARE @StrPos smallint,
      @StrStart smallint,
      @DelimiterLength smallint

      SET @DelimiterLength = LEN(@InDelimiter)

      WHILE LEN(@InDelimitedString) > 0
      BEGIN
      --Removed for clarity . do some CHARINDEX manipulation ETc.
      END

      RETURN
      END


      Question :



      I can't return select right away , but still , I want to change the fn_split to inline table valued udf.



      How can I do it ?







      sql-server sql-server-2008-r2 inline-table-function






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 30 '15 at 7:26









      Royi Namir

      73.9k98326584




      73.9k98326584
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          The problem is with your split function. It is doing the split in an RBAR fashion. You should use a set-based splitter. Here is the DelimitedSplit8k by Jeff Moden, which is one of the fastest splitter there is:



          CREATE FUNCTION [dbo].[DelimitedSplit8K](
          @pString VARCHAR(8000), @pDelimiter CHAR(1)
          )
          RETURNS TABLE WITH SCHEMABINDING AS
          RETURN
          WITH E1(N) AS (
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
          )
          ,E2(N) AS (SELECT 1 FROM E1 a, E1 b)
          ,E4(N) AS (SELECT 1 FROM E2 a, E2 b)
          ,cteTally(N) AS(
          SELECT TOP (ISNULL(DATALENGTH(@pString), 0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
          )
          ,cteStart(N1) AS(
          SELECT 1 UNION ALL
          SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString, t.N, 1) = @pDelimiter
          ),
          cteLen(N1, L1) AS(
          SELECT
          s.N1,
          ISNULL(NULLIF(CHARINDEX(@pDelimiter, @pString, s.N1),0) - s.N1, 8000)
          FROM cteStart s
          )
          SELECT
          ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
          Item = SUBSTRING(@pString, l.N1, l.L1)
          FROM cteLen l


          Note: Be sure to look into the article for the updated function





          For more split functions, read these articles by Sir Aaron Bertrand:




          • Split strings the right way – or the next best way

          • Splitting Strings : A Follow-Up

          • Splitting Strings : Now with less T-SQL

          • Comparing string splitting / concatenation methods

          • Processing a list of integers : my approach

          • Splitting a list of integers : another roundup

          • More on splitting lists : custom delimiters, preventing duplicates, and maintaining order

          • Removing Duplicates from Strings in SQL Server






          share|improve this answer























          • is this inline table valued udf ??
            – Royi Namir
            Apr 30 '15 at 7:31










          • Yes, it is an ITVF.
            – Felix Pamittan
            Apr 30 '15 at 7:31






          • 1




            God I love those CTE's
            – Royi Namir
            Apr 30 '15 at 7:32













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29961576%2fhow-can-i-convert-split-function-to-inline-table-valued-udf-in-sql-server%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          The problem is with your split function. It is doing the split in an RBAR fashion. You should use a set-based splitter. Here is the DelimitedSplit8k by Jeff Moden, which is one of the fastest splitter there is:



          CREATE FUNCTION [dbo].[DelimitedSplit8K](
          @pString VARCHAR(8000), @pDelimiter CHAR(1)
          )
          RETURNS TABLE WITH SCHEMABINDING AS
          RETURN
          WITH E1(N) AS (
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
          )
          ,E2(N) AS (SELECT 1 FROM E1 a, E1 b)
          ,E4(N) AS (SELECT 1 FROM E2 a, E2 b)
          ,cteTally(N) AS(
          SELECT TOP (ISNULL(DATALENGTH(@pString), 0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
          )
          ,cteStart(N1) AS(
          SELECT 1 UNION ALL
          SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString, t.N, 1) = @pDelimiter
          ),
          cteLen(N1, L1) AS(
          SELECT
          s.N1,
          ISNULL(NULLIF(CHARINDEX(@pDelimiter, @pString, s.N1),0) - s.N1, 8000)
          FROM cteStart s
          )
          SELECT
          ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
          Item = SUBSTRING(@pString, l.N1, l.L1)
          FROM cteLen l


          Note: Be sure to look into the article for the updated function





          For more split functions, read these articles by Sir Aaron Bertrand:




          • Split strings the right way – or the next best way

          • Splitting Strings : A Follow-Up

          • Splitting Strings : Now with less T-SQL

          • Comparing string splitting / concatenation methods

          • Processing a list of integers : my approach

          • Splitting a list of integers : another roundup

          • More on splitting lists : custom delimiters, preventing duplicates, and maintaining order

          • Removing Duplicates from Strings in SQL Server






          share|improve this answer























          • is this inline table valued udf ??
            – Royi Namir
            Apr 30 '15 at 7:31










          • Yes, it is an ITVF.
            – Felix Pamittan
            Apr 30 '15 at 7:31






          • 1




            God I love those CTE's
            – Royi Namir
            Apr 30 '15 at 7:32

















          up vote
          4
          down vote



          accepted










          The problem is with your split function. It is doing the split in an RBAR fashion. You should use a set-based splitter. Here is the DelimitedSplit8k by Jeff Moden, which is one of the fastest splitter there is:



          CREATE FUNCTION [dbo].[DelimitedSplit8K](
          @pString VARCHAR(8000), @pDelimiter CHAR(1)
          )
          RETURNS TABLE WITH SCHEMABINDING AS
          RETURN
          WITH E1(N) AS (
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
          )
          ,E2(N) AS (SELECT 1 FROM E1 a, E1 b)
          ,E4(N) AS (SELECT 1 FROM E2 a, E2 b)
          ,cteTally(N) AS(
          SELECT TOP (ISNULL(DATALENGTH(@pString), 0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
          )
          ,cteStart(N1) AS(
          SELECT 1 UNION ALL
          SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString, t.N, 1) = @pDelimiter
          ),
          cteLen(N1, L1) AS(
          SELECT
          s.N1,
          ISNULL(NULLIF(CHARINDEX(@pDelimiter, @pString, s.N1),0) - s.N1, 8000)
          FROM cteStart s
          )
          SELECT
          ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
          Item = SUBSTRING(@pString, l.N1, l.L1)
          FROM cteLen l


          Note: Be sure to look into the article for the updated function





          For more split functions, read these articles by Sir Aaron Bertrand:




          • Split strings the right way – or the next best way

          • Splitting Strings : A Follow-Up

          • Splitting Strings : Now with less T-SQL

          • Comparing string splitting / concatenation methods

          • Processing a list of integers : my approach

          • Splitting a list of integers : another roundup

          • More on splitting lists : custom delimiters, preventing duplicates, and maintaining order

          • Removing Duplicates from Strings in SQL Server






          share|improve this answer























          • is this inline table valued udf ??
            – Royi Namir
            Apr 30 '15 at 7:31










          • Yes, it is an ITVF.
            – Felix Pamittan
            Apr 30 '15 at 7:31






          • 1




            God I love those CTE's
            – Royi Namir
            Apr 30 '15 at 7:32















          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          The problem is with your split function. It is doing the split in an RBAR fashion. You should use a set-based splitter. Here is the DelimitedSplit8k by Jeff Moden, which is one of the fastest splitter there is:



          CREATE FUNCTION [dbo].[DelimitedSplit8K](
          @pString VARCHAR(8000), @pDelimiter CHAR(1)
          )
          RETURNS TABLE WITH SCHEMABINDING AS
          RETURN
          WITH E1(N) AS (
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
          )
          ,E2(N) AS (SELECT 1 FROM E1 a, E1 b)
          ,E4(N) AS (SELECT 1 FROM E2 a, E2 b)
          ,cteTally(N) AS(
          SELECT TOP (ISNULL(DATALENGTH(@pString), 0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
          )
          ,cteStart(N1) AS(
          SELECT 1 UNION ALL
          SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString, t.N, 1) = @pDelimiter
          ),
          cteLen(N1, L1) AS(
          SELECT
          s.N1,
          ISNULL(NULLIF(CHARINDEX(@pDelimiter, @pString, s.N1),0) - s.N1, 8000)
          FROM cteStart s
          )
          SELECT
          ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
          Item = SUBSTRING(@pString, l.N1, l.L1)
          FROM cteLen l


          Note: Be sure to look into the article for the updated function





          For more split functions, read these articles by Sir Aaron Bertrand:




          • Split strings the right way – or the next best way

          • Splitting Strings : A Follow-Up

          • Splitting Strings : Now with less T-SQL

          • Comparing string splitting / concatenation methods

          • Processing a list of integers : my approach

          • Splitting a list of integers : another roundup

          • More on splitting lists : custom delimiters, preventing duplicates, and maintaining order

          • Removing Duplicates from Strings in SQL Server






          share|improve this answer














          The problem is with your split function. It is doing the split in an RBAR fashion. You should use a set-based splitter. Here is the DelimitedSplit8k by Jeff Moden, which is one of the fastest splitter there is:



          CREATE FUNCTION [dbo].[DelimitedSplit8K](
          @pString VARCHAR(8000), @pDelimiter CHAR(1)
          )
          RETURNS TABLE WITH SCHEMABINDING AS
          RETURN
          WITH E1(N) AS (
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
          )
          ,E2(N) AS (SELECT 1 FROM E1 a, E1 b)
          ,E4(N) AS (SELECT 1 FROM E2 a, E2 b)
          ,cteTally(N) AS(
          SELECT TOP (ISNULL(DATALENGTH(@pString), 0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
          )
          ,cteStart(N1) AS(
          SELECT 1 UNION ALL
          SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString, t.N, 1) = @pDelimiter
          ),
          cteLen(N1, L1) AS(
          SELECT
          s.N1,
          ISNULL(NULLIF(CHARINDEX(@pDelimiter, @pString, s.N1),0) - s.N1, 8000)
          FROM cteStart s
          )
          SELECT
          ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
          Item = SUBSTRING(@pString, l.N1, l.L1)
          FROM cteLen l


          Note: Be sure to look into the article for the updated function





          For more split functions, read these articles by Sir Aaron Bertrand:




          • Split strings the right way – or the next best way

          • Splitting Strings : A Follow-Up

          • Splitting Strings : Now with less T-SQL

          • Comparing string splitting / concatenation methods

          • Processing a list of integers : my approach

          • Splitting a list of integers : another roundup

          • More on splitting lists : custom delimiters, preventing duplicates, and maintaining order

          • Removing Duplicates from Strings in SQL Server







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 22:47









          Aaron Bertrand

          206k27360401




          206k27360401










          answered Apr 30 '15 at 7:30









          Felix Pamittan

          29.1k62444




          29.1k62444












          • is this inline table valued udf ??
            – Royi Namir
            Apr 30 '15 at 7:31










          • Yes, it is an ITVF.
            – Felix Pamittan
            Apr 30 '15 at 7:31






          • 1




            God I love those CTE's
            – Royi Namir
            Apr 30 '15 at 7:32




















          • is this inline table valued udf ??
            – Royi Namir
            Apr 30 '15 at 7:31










          • Yes, it is an ITVF.
            – Felix Pamittan
            Apr 30 '15 at 7:31






          • 1




            God I love those CTE's
            – Royi Namir
            Apr 30 '15 at 7:32


















          is this inline table valued udf ??
          – Royi Namir
          Apr 30 '15 at 7:31




          is this inline table valued udf ??
          – Royi Namir
          Apr 30 '15 at 7:31












          Yes, it is an ITVF.
          – Felix Pamittan
          Apr 30 '15 at 7:31




          Yes, it is an ITVF.
          – Felix Pamittan
          Apr 30 '15 at 7:31




          1




          1




          God I love those CTE's
          – Royi Namir
          Apr 30 '15 at 7:32






          God I love those CTE's
          – Royi Namir
          Apr 30 '15 at 7:32




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29961576%2fhow-can-i-convert-split-function-to-inline-table-valued-udf-in-sql-server%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values