Search code examples
sqlsql-serverstored-procedureserror-handlingtemp-tables

How to create a stored procedure to copy data from a query to a temporary table?


I have need of inserting data to a temporary table from an existing table/query. The following produces the error detailed below.

CREATE TABLE SPTemporary
AS
BEGIN
    SELECT * into #temppT
        FROM SampleTable
END

Throws this error:

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'begin'.


Solution

  • Correct your syntax, use procedure instead of table :

    create procedure SPTemporay
    as
    begin
    select * into #temppT
    from SampleTable
    end
    

    However, if you want only copy of data then only subquery is enough :

    select st.* into #temppT
    from SampleTable st