How to create a dynamic Temp Table in the SP

  • Hi,

    How to create a dynamic Temp Table in the SP.

    Here is the one which i need

    DECLARE @Columns VARCHAR(2000),@SQLString NVARCHAR(500)

    DECLARE @NoofDays INT,@I INT

    SET @NoofDays=30

    SET @Columns=''

    SET @I=1

    WHILE @I<=@NoofDays

    BEGIN

    SET @Columns=@Columns+'['+CONVERT(VARCHAR,@I)+']'+' '+'INT'+','

    SET @I=@I+1

    END

    SET @Columns=LEFT(@Columns,LEN(@Columns)-1)

    SET @SQLString =N'DECLARE @TEMP TABLE ('+ @Columns+')'

    Select @SQLString

    I want to create it in the Sp itself and i want to add the Values to that temp table further

    Column 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,27,28,29,30

    ----------------------------------------------------------------------------------------

    Values 1,0,0,0,1,2,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1

    1,0,0,0,1,2,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0

    1,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1

    I will insert values like above and finally i want to show it to client

    The above is 1-30 it may 1-10 or 1-5 or 1-7 etc..

    Values should be inserted to that table

    is it is possible?

    Thanks

    Parthi

    Thanks
    Parthi

  • DECLARE @Columns VARCHAR(2000),@SQLString NVARCHAR(500)

    DECLARE @NoofDays INT,@I INT

    SET @NoofDays=30

    SET @Columns=''

    SET @I=1

    WHILE @I<=@NoofDays

    BEGIN

    SET @Columns=@Columns+'['+CONVERT(VARCHAR,@I)+']'+' '+'INT'+','

    SET @I=@I+1

    END

    SET @Columns=LEFT(@Columns,LEN(@Columns)-1)

    SET @SQLString ='CREATE TABLE TEMP ('+ @Columns+')'

    Select @SQLString

    exec sp_executesql @SQLString

    --insert into #TEMP(..............) VALUES(.................)

    SELECT * FROM TEMP

    DROP TABLE TEMP

  • Hi

    Thanks for your replay,If some one create the Table with the name i specified to my table in this sp ,then it is problem when i run the SP,thats why i try to create in Temp tables

    I need them in # or @ Temp tables

    Thanks
    Parthi

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

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