How to load data into table from store procedure?

  • Dear Experts,

    I want to load data from a store procedure to a temporary table. As long as I know, we cannot using SELECT...INTO (dynamically), like:

    SELECT INTO #TBL EXEC USP_STOREPROC

    We need to create the table first and specify the columns, as well as datatype. Only after the table created, we can load the data from store procedure to the table:

    INSERT INTO #TBL EXEC USP_STOREPROC

    The problem is I do not know the structure of the temporary table before run the store procedure. Any idea how to dynamically create the temporary table?

     



    Regards,
    kokyan

  • You could use a linked server (even to the server on) and using openquery to call the sp do the select into:

     

    Ex:

     

    Select * into #x from openquery (linksrv,'sp_name')

     

    but you are limited in that you cannot pass in conditions to the sp unless you write using dynamic SQL.

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

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