Insert a string into a temp table

  • Hi Guys,

    I'm trying to insert a string into a temp table. For instance I want a nvarchar(4000) string

    @Ids inserted as separte Ids in a All_id table.

    The user can type in 1,2,3,4,5,6 etc separated by commas. How can I get that into my temp table as idividual Ids in a column?

    Thanks guys

  • Check if this will help you:

    Create Table ##TempTable (Id nvarchar(5))

    declare @Ids nvarchar(4000)

    declare @tempstr nvarchar(4000)

    select @Ids = '1,2,3,4,5,6'

    select @tempstr = @Ids

    WHILE len(@tempstr) > 0

    BEGIN

    --Printing the current temp string and the position of the first comma in it

    Print @tempstr

    Print patindex('%,%',@tempstr)

    -- Inserting the string before the first comma

    Insert ##TempTable(Id)

    values(substring(@tempstr,1,patindex('%,%',@tempstr)-1))

    --resetting the temporary string to start after the first comma in the previous temp string

    select @tempstr = substring(@tempstr,patindex('%,%',@tempstr)+1,Len(@tempstr))

    --Checking if there is no more commas

    IF patindex('%,%',@tempstr)<=0

    Begin

    --Inserting the last ID

    Insert ##TempTable(Id)

    values(@tempstr)

    BREAK

    End

    ELSE

    CONTINUE

    END

    select * from ##TempTable

    drop table ##TempTable

    Regards,Yelena Varsha

  • djiang (1/23/2008)


    Hi Guys,

    I'm trying to insert a string into a temp table. For instance I want a nvarchar(4000) string

    @Ids inserted as separte Ids in a All_id table.

    The user can type in 1,2,3,4,5,6 etc separated by commas. How can I get that into my temp table as idividual Ids in a column?

    Thanks guys

    First things first... you need a "Numbers" or "Tally" table to really make your life easy. All it is is a simple table with a well indexed single column of sequential numbers from 1 to some number. I keep 11,000 numbers in my Tally table because it's larger than VARCHAR(8000) and contains enough numbers for me to generate 30 years worth of dates when I need to. Here's how to make one... add it to your SQL arsenal...


    [font="Courier New"]--=====&nbspCreate&nbspand&nbsppopulate&nbspthe&nbspTally&nbsptable&nbspon&nbspthe&nbspfly&nbsp(2k5&nbspversion)

    &nbspSELECT&nbspTOP&nbsp11000&nbsp--More&nbspthan&nbsp30&nbspyears&nbspworth&nbspof&nbspdays&nbspif&nbspconverted&nbspto&nbspdates

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbspN&nbsp=&nbspIDENTITY(INT,1,1)

    &nbsp&nbsp&nbspINTO&nbspdbo.Tally

    &nbsp&nbsp&nbspFROM&nbspMaster.sys.All_Columns&nbspac1

    &nbsp&nbspCROSS&nbspJOIN&nbspMaster.sys.All_Columns&nbspac2

    --=====&nbspAdd&nbspthe&nbspnecessary&nbspClustered&nbspPK&nbspfor&nbspblinding&nbspspeed

    &nbsp&nbspALTER&nbspTABLE&nbspdbo.Tally

    &nbsp&nbsp&nbsp&nbspADD&nbspCONSTRAINT&nbspPK_Tally_N&nbspPRIMARY&nbspKEY&nbspCLUSTERED&nbsp(N)

    --=====&nbspAllow&nbspeveryone&nbspto&nbspuse&nbspthe&nbsptable

    &nbsp&nbspGRANT&nbspSELECT&nbspON&nbspdbo.Tally&nbspTO&nbspPUBLIC

    [/font]


    After that, everything get's easy. For example, how to split the @lds variable into a Temp Table called #ALL_ID as you requested... I've bolded the part that does all the work... everything else is just setup for the test... do take the time to read the comments in the code...


    [font="Courier New"]--=====&nbspThis&nbspsimulates&nbspthe&nbspparameter&nbspstring&nbspbeing&nbsppassed&nbspto&nbspyour&nbspstored&nbspproc

    DECLARE&nbsp@lds&nbspNVARCHAR(4000)

    &nbsp&nbsp&nbsp&nbspSET&nbsp@lds&nbsp=&nbspN'14,35,9,1234567890,27,12,,12,54,1,34534,45345,234,855,7,,34,53,674,57,56,56,,,,5757567,45745,32'

    --=====&nbspThis&nbspjust&nbspmakes&nbspsure&nbspyour&nbsptemp&nbsptable&nbspdoesn't&nbspalready&nbspexist&nbspduring&nbsprepetative&nbsptesting

    &nbsp&nbsp&nbsp&nbsp&nbspIF&nbspOBJECT_ID('TempDB..#All_ID','U')&nbspIS&nbspNOT&nbspNULL

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspDROP&nbspTABLE&nbsp#All_ID

    --=====&nbspThis&nbspjust&nbspkeeps&nbsprowcounts&nbspfrom&nbspbeing&nbspreturned

    &nbsp&nbsp&nbsp&nbspSET&nbspNOCOUNT&nbspON

    --=====&nbspThis&nbspsplits&nbspthe&nbspparameter&nbspstring&nbspvariable&nbspand&nbspcreates&nbspthe&nbsptemp&nbsptable&nbspall&nbspin&nbsp1&nbspshot.

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbspIt's&nbspall&nbspdone&nbspusing&nbspthe&nbsppower&nbspof&nbspan&nbspimplicit&nbspcross-join&nbspwith&nbspthe&nbspcontents&nbspof&nbspthe&nbsp@lds&nbspvariable.

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbspSide&nbspbenefit&nbspis&nbspthat&nbspthe&nbsp"Element"&nbspcolumn&nbspknows&nbspthe&nbsp"position"&nbspof&nbspeach&nbspparameter&nbspnumerically.

    &nbspSELECT&nbspElement&nbsp=&nbspIDENTITY(INT,1,1),

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspID&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp=&nbspNULLIF(SUBSTRING(','+@lds+',',&nbspt.N+1,&nbspCHARINDEX(',',&nbsp','+@lds+',',&nbspt.N+1)-t.N-1),'')

    &nbsp&nbsp&nbspINTO&nbsp#All_ID

    &nbsp&nbsp&nbspFROM&nbspdbo.Tally&nbspt&nbspWITH&nbsp(NOLOCK)

    &nbsp&nbspWHERE&nbspSUBSTRING(','+@lds,&nbspt.N,&nbsp1)&nbsp=&nbsp','&nbsp

    &nbsp&nbsp&nbsp&nbspAND&nbspt.N&nbsp<=&nbspLEN(','+@lds)&nbsp

    --=====&nbspThis&nbspjust&nbspdisplays&nbspthe&nbspresults...&nbspwhat's&nbspin&nbspthe&nbsptemp&nbsptable.

    &nbspSELECT&nbsp*&nbspFROM&nbsp#All_ID[/font]


    You should see all of the good stuff you can do with a Tally table. You can also do the same thing with a "Tally CTE", but who wants to keep recreating the wheel? 😉

    --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)

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

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