Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want save my stored procedure result in new table

this is my procedure

SQL
SELECT 
    PaymentTracker1.*
    , convert(varchar(20),DATEADD(m,N,StartDate),110) AS DueDate
    ,(Numbers.N * MonthlyPay) AS DueAmount
FROM 
    PaymentTracker1 
    INNER JOIN Numbers on DATEDIFF(m,PaymentTracker1.StartDate,PaymentTracker1.EndDate) >= Numbers.N 
    
    where  datepart(mm,DATEADD(mm,N,StartDate))= datepart(mm,getdate()) and  datepart(yy,DATEADD(y,N,StartDate))= datepart(yy,getdate())
ORDER BY
    PaymentTracker1.Name
    ,Numbers.N


how to save resulted value of this procedure into new table
Posted
Updated 1-Nov-15 21:37pm
v2

you can try SELECT INTO. This will create a new table for you, which can be temporary if you want by prefixing the table name with a pound sign (#).
SQL
SELECT *
INTO #tempTableName
FROM TableName

your query will be
SQL
SELECT PaymentTracker1.*, convert(varchar(20),DATEADD(m,N,StartDate),110) AS DueDate
,(Numbers.N * MonthlyPay) AS DueAmount
INTO #tempResult
FROM PaymentTracker1
INNER JOIN Numbers on DATEDIFF(m,PaymentTracker1.StartDate,PaymentTracker1.EndDate) >= Numbers.N
where datepart(mm,DATEADD(mm,N,StartDate))= datepart(mm,getdate()) and datepart(yy,DATEADD(y,N,StartDate))= datepart(yy,getdate())
ORDER BY
PaymentTracker1.Name
,Numbers.N

If you want to store it in permanent table then first you have to define a table with exactly same columns which are you selecting in query the use this table in above query.
 
Share this answer
 
Comments
Member 11382784 2-Nov-15 3:20am    
thanks for your help sir..
i want to save the result in permanent table,
and i have created a table with same columns , and how to define that in procedure .
i have tried but iam getting error There is already an object named 'MYPaymentTracker1' in the database.


SELECT PaymentTracker1.*, convert(varchar(20),DATEADD(m,N,StartDate),110) AS DueDate
,(Numbers.N * MonthlyPay) AS DueAmount
INTO [MYPaymentTracker1]
FROM PaymentTracker1
INNER JOIN Numbers on DATEDIFF(m,PaymentTracker1.StartDate,PaymentTracker1.EndDate) >= Numbers.N
where datepart(mm,DATEADD(mm,N,StartDate))= datepart(mm,getdate()) and datepart(yy,DATEADD(y,N,StartDate))= datepart(yy,getdate())
ORDER BY
PaymentTracker1.Name
,Numbers.N
[no name] 2-Nov-15 3:33am    
What is your table name in which you want to store data ?
Member 11382784 2-Nov-15 3:42am    
MYPaymentTracker1
[no name] 2-Nov-15 3:53am    
You can try
INSERT INTO MYPaymentTracker1 SELECT PaymentTracker1.*, convert(varchar(20),DATEADD(m,N,StartDate),110) AS DueDate
,(Numbers.N * MonthlyPay) AS DueAmount
FROM PaymentTracker1
INNER JOIN Numbers on DATEDIFF(m,PaymentTracker1.StartDate,PaymentTracker1.EndDate) >= Numbers.N
where datepart(mm,DATEADD(mm,N,StartDate))= datepart(mm,getdate()) and datepart(yy,DATEADD(y,N,StartDate))= datepart(yy,getdate())
ORDER BY
PaymentTracker1.Name
,Numbers.N
Member 11382784 2-Nov-15 4:02am    
am getting error ..Incorrect syntax near the keyword 'INTO'.
create a temp table

SQL
CREATE TABLE #tmp
(
   COL1 INT,
   COL2 INT
)


write insert statement and execute the stored procedure

SQL
INSERT INTO #tmp
Exec SpName 'Params'
 
Share this answer
 
Comments
Member 11382784 2-Nov-15 3:02am    
thanks for your help sir ,
can we do edit update in grid (asp.net c#)with temp table and is tamp table delete automaticalyy
write2varun 2-Nov-15 3:11am    
you will have to drop the table
drop table #tmp
you can add the temp table in the same sp and retrieve and bind the value into dataset or datatable and bind it with the grid and perform the update, retrieve the value from the grid and save it via table method or via dataset method
Try this

SQL
INSERT NEW_TABLE
EXEC SPNAME
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900