Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

cursor for inserting one record at a time Expand / Collapse
Author
Message
Posted Tuesday, July 16, 2013 12:13 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 5:04 AM
Points: 79, 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
Post #1473990
Posted Tuesday, July 16, 2013 12:58 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:23 AM
Points: 5,074, Visits: 11,852
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.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1474003
Posted Tuesday, July 16, 2013 7:24 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:12 PM
Points: 13,441, Visits: 12,303
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 Moden's 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)
Post #1474122
Posted Tuesday, July 16, 2013 8:00 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:55 PM
Points: 3,648, Visits: 5,321
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!
Post #1474350
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse