SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


cursor for inserting one record at a time


cursor for inserting one record at a time

Author
Message
ssurekha2000
ssurekha2000
SSC-Enthusiastic
SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)

Group: General Forum Members
Points: 143 Visits: 207
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
Phil Parkin
Phil Parkin
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18299 Visits: 20410
ssurekha2000 (7/16/2013)
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


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




Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25950 Visits: 17519
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7231 Visits: 6431
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 mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search