I need to loop one temp table and i need to insert data to another #temp table from first temp table based on some condition. how its possible?
I don't want to use cursor
I assume you know about https://www.w3schools.com/SQL/sql_insert_into_select.asp ? If you have a problem because of your condition we would need to know about it so that one can see it
it could still be done using that or few SQL statements.
At some point if you have really no way around looping on each and every row, cursors are intended for doing that.
Post your table structure and sample data and your expected result. You may think in loop but you should implement set based solution in database. Post your sample and you will get help from there. Thanks.
Member
35 Points
156 Posts
Loop table and retrieve data
Mar 20, 2019 11:48 AM|binustrat|LINK
I need to loop one temp table and i need to insert data to another #temp table from first temp table based on some condition. how its possible?
I don't want to use cursor
All-Star
48520 Points
18073 Posts
Re: Loop table and retrieve data
Mar 20, 2019 12:28 PM|PatriceSc|LINK
Hi,
I assume you know about https://www.w3schools.com/SQL/sql_insert_into_select.asp ? If you have a problem because of your condition we would need to know about it so that one can see it it could still be done using that or few SQL statements.
At some point if you have really no way around looping on each and every row, cursors are intended for doing that.
All-Star
123252 Points
10024 Posts
Moderator
Re: Loop table and retrieve data
Mar 20, 2019 01:37 PM|limno|LINK
Post your table structure and sample data and your expected result. You may think in loop but you should implement set based solution in database. Post your sample and you will get help from there. Thanks.
Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm
None
0 Points
3 Posts
Re: Loop table and retrieve data
Mar 22, 2019 06:59 PM|BillJordan|LINK
I would think you would want to do the following:
INSERT INTO #temp2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM #temp1
WHERE condition;
Hope this helps you.