User Defined Function for following scnario

  • Dear All,

    In my scenario, i need to generate string like 

    AAA,AAB,AAC.........ZZZ

    My input parameter is just Total count of records

    Example 
    declare @countRecord int

    select @countRecord=count(*) from tablename

    select dbo.udf(@countRecord)

    i want output  like
    AAA
    AAB
    AAC

    .
    .
    .
    ZZZ

  • vs.satheesh - Thursday, January 17, 2019 5:46 AM

    Dear All,

    In my scenario, i need to generate string like 

    AAA,AAB,AAC.........ZZZ

    My input parameter is just Total count of records

    Example 
    declare @countRecord int

    select @countRecord=count(*) from tablename

    select dbo.udf(@countRecord)

    i want output  like
    AAA
    AAB
    AAC

    .
    .
    .
    ZZZ


    CREATE FUNCTION ParseValues(@String varchar(8000), @Delimiter varchar(10) )RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(8000))ASBEGINDECLARE @Value varchar(100)WHILE @String is not nullBEGINSELECT @Value=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN LEFT(@String,PATINDEX('%'+@Delimiter+'%',@String)-1) ELSE @String END, @String=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN SUBSTRING(@String,PATINDEX('%'+@Delimiter+'%',@String)+LEN(@Delimiter),LEN(@String)) ELSE NULL ENDINSERT INTO @RESULTS (Val)SELECT @ValueENDRETURNEND

    Reference link:

    https://visakhm.blogspot.com/2010/02/parsing-delimited-string.html

    Saravanan

  • Function:
    CREATE FUNCTION dbo.udf(@RowCount int)
    RETURNS TABLE
    AS
    RETURN
    WITH X AS (SELECT * FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26)) T(N))
    SELECT TOP(@RowCount)
          SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',a.N,1)+SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',b.N,1)+SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',c.N,1) [Value]
      FROM X a, X b, X c
     ORDER BY 1
    GO

    Usage:
    DECLARE @countRecord int = 100
    SELECT *
      FROM dbo.udf(@countRecord)

  • Jonathan AC Roberts - Thursday, January 17, 2019 6:41 AM

    Function:
    CREATE FUNCTION dbo.udf(@RowCount int)
    RETURNS TABLE
    AS
    RETURN
    WITH X AS (SELECT * FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26)) T(N))
    SELECT TOP(@RowCount)
          SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',a.N,1)+SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',b.N,1)+SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',c.N,1) [Value]
      FROM X a, X b, X c
     ORDER BY 1
    GO

    Usage:
    DECLARE @countRecord int = 100
    SELECT *
      FROM dbo.udf(@countRecord)

    Dear Jonathan

    Thanks  for your reply. My client is using SQl server 2008 . I want only one value . because I called user defined function inside the loop.

  • vs.satheesh - Thursday, January 17, 2019 10:23 PM

    Jonathan AC Roberts - Thursday, January 17, 2019 6:41 AM

    Function:
    CREATE FUNCTION dbo.udf(@RowCount int)
    RETURNS TABLE
    AS
    RETURN
    WITH X AS (SELECT * FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26)) T(N))
    SELECT TOP(@RowCount)
          SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',a.N,1)+SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',b.N,1)+SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',c.N,1) [Value]
      FROM X a, X b, X c
     ORDER BY 1
    GO

    Usage:
    DECLARE @countRecord int = 100
    SELECT *
      FROM dbo.udf(@countRecord)

    Dear Jonathan

    Thanks  for your reply. My client is using SQl server 2008 . I want only one value . because I called user defined function inside the loop.

    This will do that
    DECLARE @countRecord int = 100
    DECLARE @Result nvarchar(MAX) = ''

    SELECT @Result = @Result + Value + CHAR(13) + CHAR(10)
    FROM dbo.udf(@countRecord)

    SELECT LEFT(@Result, LEN(@Result)-2)

  • This seems to perform much better, because it doesn't require an expensive SORT operation. It also uses the documented (and supported) XML concatenation over using the undocumented (and unsupported) quirky update method.

    WITH Base AS (SELECT i FROM ( VALUES(0), (0), (0), (0), (0), (0), (0), (0), (0), (0) ) v(i) )
    , Tally(n) AS ( SELECT ROW_NUMBER() OVER(ORDER BY @@VERSION ) - 1 FROM Base A, Base B, Base C )
    SELECT STUFF(
        (
            SELECT TOP (@RowCount) ',' + CHAR(65 + n/26/26%26) + CHAR(65 + n/26%26) + CHAR(65 + n%26)
            FROM Tally
            FOR XML PATH(''), TYPE
        ).value('.', 'VARCHAR(MAX)'), 1, 1, '');

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • No need to use resources to recompute the string every time.

    Pre-generate all the strings and store them in a permanent table.  Then just pull out the row(s) you need.  Very fast, with a trivial-to-generate exec plan.

    This method also provides a very easy way to skip certain letter/string combinations later.  It's likely someone may not want see "FUK" as a string on a screen and/or document.


    CREATE TABLE dbo.alpha_sequences
    (
      alpha_count smallint CONSTRAINT alha_sequence__PK PRIMARY KEY,
      alpha_sequence char(3) NOT NULL
    )

    ;WITH cte_numbers AS (SELECT * FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26)) numbers(number))
    INSERT INTO dbo.alpha_sequences ( alpha_count, alpha_sequence )
    SELECT
        n1.number + ((n2.number-1) * 26) + ((n3.number-1) * 26 * 26) AS alpha_count,
        SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',n3.number,1) +
        SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',n2.number,1) + 
        SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ',n1.number,1) AS alpha_sequence
    FROM cte_numbers n1
    CROSS JOIN cte_numbers n2
    CROSS JOIN cte_numbers n3
    ORDER BY 1
    GO
    SET ANSI_NULLS ON;
    SET QUOTED_IDENTIFIER ON;
    GO
    CREATE FUNCTION dbo.getAlphaSequence (
        @count smallint
    )
    RETURNS TABLE
    AS
    RETURN (
        SELECT TOP (@count) alpha_sequence
        FROM dbo.alpha_sequences
        ORDER BY alpha_count
    )
    GO
    SELECT *
    FROM dbo.getAlphaSequence(10)

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • vs.satheesh - Thursday, January 17, 2019 5:46 AM

    Dear All,

    In my scenario, i need to generate string like 

    AAA,AAB,AAC.........ZZZ

    My input parameter is just Total count of records

    Example 
    declare @countRecord int

    select @countRecord=count(*) from tablename

    select dbo.udf(@countRecord)

    i want output  like
    AAA
    AAB
    AAC

    .
    .
    .
    ZZZ

    You don't actually want to do this.  There are some pretty offensive words that can be spelled out just in English never mind all of the languages of the world.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Friday, January 18, 2019 3:16 PM

    You don't actually want to do this.  There are some pretty offensive words that can be spelled out just in English never mind all of the languages of the world.

    Yes, it would be even worse if the OP wanted 4 letter codes.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply