Muliple Comma Seperated values insert into table without using cursor

  • DROP TABLE TestMode

    CREATE TABLE TestMode
    (
      Mode NVARCHAR(MAX),
        ANS NVARCHAR(MAX)
    )

    DECLARE @mode NVARCHAR(MAX)
    SET @mode = 'Mode1|Mode2|Mode3'

    DECLARE @Ans NVARCHAR(MAX)
    SET @Ans = 'Ans1|Ans2|;Ans1|Ans2;|Ans1|Ans2'

    I need a Result like below

    Mode1 Ans1|Ans2
    Mode2 Ans1|Ans2
    Mode3 Ans1|Ans2

    Kindly help me how to query for the above

    i tried below its not working
    INSERT INTO TestMode (Mode) 
    (SELECT Item FROM dbo.SplitString(@mode,'|'))

  • Twice in the same day πŸ™‚ Try this thread.

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • pvuser.chennai - Friday, January 27, 2017 6:41 AM

    DROP TABLE TestMode

    CREATE TABLE TestMode
    (
      Mode NVARCHAR(MAX),
        ANS NVARCHAR(MAX)
    )

    DECLARE @mode NVARCHAR(MAX)
    SET @mode = 'Mode1|Mode2|Mode3'

    DECLARE @Ans NVARCHAR(MAX)
    SET @Ans = 'Ans1|Ans2|;Ans1|Ans2;|Ans1|Ans2'

    I need a Result like below

    Mode1 Ans1|Ans2
    Mode2 Ans1|Ans2
    Mode3 Ans1|Ans2

    Kindly help me how to query for the above

    i tried below its not working
    INSERT INTO TestMode (Mode) 
    (SELECT Item FROM dbo.SplitString(@mode,'|'))

    Given the desired results, you need to split on the semicolon, not the pipe character.
    😎

Viewing 3 posts - 1 through 3 (of 3 total)

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