Databases like SQL are designed to process bulk data (all records that match certain criteria).
When you need to process data sequentially (pretty much like for each ... next in VB) you need a cursor. Cursor will retrieve record after record (given your criteria) and allow you to handle each record separately.
hmmm thanks...but i dont understand why we use cursors for inserting data..can you help me in this..
Personally, I don't recall ever using a cursor for inserting data. Could you give us a little more detail about the circumstance you have in mind? Maybe post an example? If we knew a bit more we might be able to come up with a rationale or an explanation (or maybe an alternative?).
I've used a cursor to insert data. The reason was that the table receiving the data had a trigger for insert, that would handle 1 record at a time (was building a string for communication with an MVS host and was using variables. Who ever created the trigger wasn't checking the number of records in INSERTED and I just found it easier to go with the trigger than to mess the whole interface).
Other than that I can only think that the insert is based on a VERY complex ruleset that would be too much for case to handle or that would change tables/ select statements (perhaps dynamic SQL?) depending on the data received.
Oh wait, also it makes it easier to create header/detail records under some very specific circumstances (using OUTPUT when inserting header if no keys are available).
We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.