Share
Go down
avatar
Admin
Posts : 155
Join date : 2017-11-11
Age : 27
View user profilehttp://net-media.yoo7.com

How Select Data and Insert data to another table using same stored procedure

on Wed Dec 13, 2017 11:52 am
I want to select some data inside a stored procedure and I want to insert all selected data to another table inside the same stored procedure:
SELECT * FROM @TempTable
then insert into another table like this:
Code:
SELECT * FROM @TempTable
then insert into another table like this:
Code:
SELECT @v_Maxno = isnull(max([A_TEST].ROW_NUM) + 1, 1)
        FROM [A_TEST]

INSERT INTO [dbo].[A_TEST] (ROW_NUM,A, B, C) 
VALUES (@v_Maxno,TempTable.CL_A, TempTable.CL_B, TempTable.CL_C)
I have to calculate row number Manually if any thing like loop so I will manage to achieve this.

Not finding the best way to doing this ..........Using SQL Server

Answer
instead of using values keyword, directly use select.

like this
Code:
insert into Table1(RowNum,Col1,Col2)
select ROW_NUMBER() OVER(ORDER BY Cola),Cola,colb from table2
Back to top
Permissions in this forum:
You can reply to topics in this forum