Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server stored procedure creating temp table and inserting value

I am trying to select values from different table and inset it in to the temporary table.

I need a identity field in the temporary table. When I try to execute the following code it throws an error:

*Msg 2714, Level 16, State 1, Procedure SelectCashDetails, Line 27
There is already an object named '#ivmy_cash_temp1' in the database.*

I try to change the temp table into different names even after it throws the same error.

This is my code:

ALTER PROCEDURE [dbo].[SelectCashDetails] 
(
   @trustcompanyid BigInt,
   @trustaccountid BigInt,
   @planid BigInt,
   @fromdate varchar(20),
   @todate varchar(20),
   @movetype varchar(20),
   @typedesc varchar(20)
)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    CREATE TABLE #ivmy_cash_temp1          
     ( tmovedate datetime,          
       tmovedesc varchar(20),    
       tmoneymovetype varchar(20),
       tplanbal decimal(18,6),         
       tsourcetype BigInt,           
       tdestinationtype BigInt) 

   SELECT 
       IDENTITY(int) AS id, 
       CMM.movedate,
       CDCP.paramdesc,
       CMM.movementtypecd,
       CMM.amountmoved,
       CMM.planbalance,
       cmm.sourceaccounttypeid,
       cmm.destinationaccounttypeid 
   into #ivmy_cash_temp1  
   from 
       cash_moneymove CMM 
   inner join 
       CDC_PARAMETERS CDCP on CMM.movedescriptioncd=CDCP.paramcd 
   where 
       CMM.movedescriptioncd = @typedesc 
       and PARAMNAME = 'Cash AccountType Desc'

   select * from #ivmy_cash_temp1
END
like image 828
Ivan Avatar asked Jan 15 '13 08:01

Ivan


People also ask

How do you create a temp table within a stored procedure in SQL?

Global Temporary Tables. Stored procedures can reference temporary tables that are created during the current session. Within a stored procedure, you cannot create a temporary table, drop it, and then create a new temporary table with the same name.

How do you create and add values to a temp table in SQL?

Using SQL Select Statement The syntax for creating a temp table with the select statement is as shown: SELECT column_list INTO #temporary_table_name FROM TABLE_NAME WHERE conditional_expression; We use the select statement followed by the name of the temporary table.

How can we store stored procedure result in temp table in SQL Server?

When the stored procedure returns a lot of columns and you do not want to manually "create" a temporary table to hold the result, I've found the easiest way is to go into the stored procedure and add an "into" clause on the last select statement and add 1=0 to the where clause.

How do I move data to a temp table in SQL?

Such as, if we want to copy the Location table data into a temp table using the INSERT INTO SELECT statement, we have to specify the temporary table explicitly and then insert the data. In fact, these two statements accomplish the same task in different ways.


1 Answers

A SELECT INTO statement creates the table for you. There is no need for the CREATE TABLE statement before hand.

What is happening is that you create #ivmy_cash_temp1 in your CREATE statement, then the DB tries to create it for you when you do a SELECT INTO. This causes an error as it is trying to create a table that you have already created.

Either eliminate the CREATE TABLE statement or alter your query that fills it to use INSERT INTO SELECT format.

If you need a unique ID added to your new row then it's best to use SELECT INTO... since IDENTITY() only works with this syntax.

like image 81
Dave K Avatar answered Sep 28 '22 09:09

Dave K