July 16, 2013 at 12:13 am
i need to insert records from table1 to table 2
out of 15 columns from table1 i need to insert 6 columns to table 2
but
if i use
insert into table2
select col1,col4,col5,col8,col10, @sqno from table1 where custid=11
if table1 has 30 records all will be inserted at once
but i need to have a loop such that
row1 will, be inserted , than 2 and so on till 30
if its possible to do without cursor it will be more good
July 16, 2013 at 12:58 am
ssurekha2000 (7/16/2013)
i need to insert records from table1 to table 2out of 15 columns from table1 i need to insert 6 columns to table 2
but
if i use
insert into table2
select col1,col4,col5,col8,col10, @sqno from table1 where custid=11
if table1 has 30 records all will be inserted at once
but i need to have a loop such that
row1 will, be inserted , than 2 and so on till 30
if its possible to do without cursor it will be more good
Can you explain why you would want to do that? Presumably something to do with @sqno? Most people on this forum would feel dirty writing such a thing 🙂
But if RBAR processing is really what you want, a CURSOR is as good a way as any.
Your INSERT syntax is deficient though - you should list the columns you are inserting into:
insert table2
(col1
,col4
,col5
,col8
,col10
,sqno
)
select col1
,col4
,col5
,col8
,col10
,@sqno
from table1
where custid = 11
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
July 16, 2013 at 7:24 am
As Phil said, although more gently than me, you do not need a cursor for this at all. There is absolutely NO NEED for one. If you can explain why you think you need a cursor for some inserts we can quite easily demonstrate some alternatives that do not involve any kind of looping for this.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
July 16, 2013 at 8:00 pm
Sean Lange (7/16/2013)
As Phil said, although more gently than me, you do not need a cursor for this at all. There is absolutely NO NEED for one. If you can explain why you think you need a cursor for some inserts we can quite easily demonstrate some alternatives that do not involve any kind of looping for this.
+1 to Sean and Phil.
Posted so the OP might gain inspiration from my mantra (below).
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply