Inserting data into a table from a stored procedure MS SQL 2008

Posted on 2012-09-08
Last Modified: 2012-09-09

I am using MS SQL 2008.

I have a stored procedure that is called by other stored proedures.

This store procedure generates a temporary table and fills it with data. It does some manipulations on the data and then returns back to the calling stored procedure.

However now i want to create a "real" table in the database and use the stored procedure to fill it.

I seemed to have trouble doing insert ** into MyTable from ... because it claims the table exists - which it does!  I have created an empty table.  Does the insert into statement also create the table?  I wanted to do it myself so i have some control over the column definitions.

What is the best way to do this?
Question by:soozh
    1 Comment
    LVL 16

    Accepted Solution

    If you use this code:

    declare @a int, @b int, @c varchar(24), @d datetime
    --       Add code here to give values to variables
    select  @a, @b, @c, @d into #temptab

    then you'll get a temporary table called #temptab with the structure [int, int, varchar(24), datetime], as you might expect. The "into" keyword in the select statement causes sql server to create an appropriate table structure to use for the data.

    You don't have to make this a temp table - you can equally well use

    select  @a, @b, @c, @d into permtab

    Then refresh the tables list in SSMS and you should see your new table. However, do it again and you'll get an error because the table is already there!

    A more controlled way would be to have an already-created table and then use the following code in the stored procedure:

    --     clear the table first if needed:
    truncate table permtab
    --     and then put the data in
    insert into permtab
        select  @a, @b, @c, @d



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    Title # Comments Views Activity
    Error message when scheduling a job using a linked Server 12 40
    SQL HELP 2 65
    SQL Date from a string 4 41
    SQL Question 9 34
    This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
    In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now