ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Databases  >>  SQL Server
 
 


 

 
 Oracle interview questions  Oracle Interview Questions
 SQL Server interview questions  SQL Server Interview Questions
 MS Access interview questions  MS Access Interview Questions
 MySQL interview questions  MySQL Interview Questions
 Postgre interview questions  Postgre Interview Questions
 Sybase interview questions  Sybase Interview Questions
 DB Architecture interview questions  DB Architecture Interview Questions
 DB Administration interview questions  DB Administration Interview Questions
 DB Development interview questions  DB Development Interview Questions
 SQL PLSQL interview questions  SQL PLSQL Interview Questions
 Databases AllOther interview questions  Databases AllOther Interview Questions
Question
What is row by row processing ?
 Question Submitted By :: Devesh
I also faced this Question!!     Rank Answer Posted By  
 
  Re: What is row by row processing ?
Answer
# 1
row by row processing by Using Cursors

Row by row processing means for example we can update the 
one column , on that column we have 500 records ,we are 
going to update the all recodes row by row by using cursors.

SQL cursors have been a curse to database programming for 
many years because of their poor performance. On the other 
hand, they are extremely useful because of their 
flexibility in allowing very detailed data manipulations at 
the row level. Using cursors against SQL Server tables can 
often be avoided by employing other methods, such as using 
derived tables, set-based queries, and temp tables. A 
discussion of all these methods is beyond the scope of this 
article, and there are already many well-written articles 
discussing these techniques.The focus of this article is 
directed at using non-cursor-based techniques for 
situations in which row-by-row operations are the only, or 
the best method available, to solve a problem. Here, I will 
demonstrate a few programming methods that provide a 
majority of the cursor’s flexibility, but without the 
dramatic performance hit.

Let’s begin by reviewing a simple cursor procedure that 
loops through a table. Then we’ll examine a non-cursor 
procedure that performs the same task.

if exists (select * from sysobjects where name = 
N’prcCursorExample’)
drop procedure prcCursorExample
go

CREATE PROCEDURE prcCursorExample
AS
/*
** Cursor method to cycle through the Customer table and 
get Customer Info for each iRowId.
**
** Revision History:
** —————————————————
** Date Name Description Project
** —————————————————
** 08/12/03 DVDS Create —-
**
*/

SET NOCOUNT ON

– declare all variables!
DECLARE @iRowId int,
@vchCustomerName nvarchar(255),
@vchCustomerNmbr nvarchar(10)

– declare the cursor
DECLARE Customer CURSOR FOR
SELECT iRowId,
vchCustomerNmbr,
vchCustomerName
FROM CustomerTable
OPEN Customer
FETCH Customer INTO @iRowId,
@vchCustomerNmbr,
@vchCustomerName

– start the main processing loop.

WHILE @@Fetch_Status = 0
BEGIN
– This is where you perform your detailed row-by-row 
processing.

– Get the next row.
FETCH Customer INTO @iRowId,
@vchCustomerNmbr,
@vchCustomerName

END

CLOSE Customer
DEALLOCATE Customer
RETURN
--------------------------
2.row by row processing in general


As you can see, this is a very straight-forward cursor 
procedure that loops through a table called CustomerTable 
and retrieves iRowId, vchCustomerNmbr and vchCustomerName 
for every row. Now we will examine a non-cursor version 
that does the exact same thing:

if exists (select * from sysobjects where name = 
N’prcLoopExample’)
drop procedure prcLoopExample
go

CREATE PROCEDURE prcLoopExample
AS
/*
** Non-cursor method to cycle through the Customer table 
and get Customer Info for each iRowId.
**
** Revision History:
** ——————————————————
** Date Name Description Project
** ——————————————————
** 08/12/03 DVDS Create —–
**
*/

SET NOCOUNT ON

– declare all variables!

DECLARE @iReturnCode int,
@iNextRowId int,
@iCurrentRowId int,
@iLoopControl int,
@vchCustomerName nvarchar(255),
@vchCustomerNmbr nvarchar(10)
@chProductNumber nchar(30)

– Initialize variables!
SELECT @iLoopControl = 1
SELECT @iNextRowId = MIN(iRowId)
FROM CustomerTable

– Make sure the table has data.
IF ISNULL(@iNextRowId,0) = 0
BEGIN
SELECT ‘No data in found in table!’
RETURN
END

– Retrieve the first row

SELECT @iCurrentRowId = iRowId,
@vchCustomerNmbr = vchCustomerNmbr,
@vchCustomerName = vchCustomerName
FROM CustomerTable
WHERE iRowId = @iNextRowId

– start the main processing loop.
WHILE @iLoopControl = 1
BEGIN

– This is where you perform your detailed row-by-row 
processing.
– Reset looping variables.
SELECT @iNextRowId = NULL

– get the next iRowId
SELECT @iNextRowId = MIN(iRowId)
FROM CustomerTable
WHERE iRowId > @iCurrentRowId

– did we get a valid next row id?

IF ISNULL(@iNextRowId,0) = 0
BEGIN
BREAK
END

– get the next row.
SELECT @iCurrentRowId = iRowId,
@vchCustomerNmbr = vchCustomerNmbr,
@vchCustomerName = vchCustomerName
FROM CustomerTable
WHERE iRowId = @iNextRowId
END
RETURN

There are several things to note about the above procedure.

For performance reasons, you will generally want to use a 
column like “iRowId” as your basis for looping and row 
retrieval. It should be an auto-incrementing integer data 
type, along with being the primary key column with a 
clustered index.

There may be times in which the column containing the 
primary key and/or clustered index is not the appropriate 
choice for looping and row retrieval. For example, the 
primary key and/or clustered index may have already been 
built on a column using uniqueindentifier as the data type. 
In such a case, you can usually add an auto-increment 
integer data column to the table and build a unique index 
or constraint on it.

The MIN function is used in conjunction with greater 
than “>” to retrieve the next available iRowId. You could 
also use the MAX function in conjunction with less than “<” 
to achieve the same result:

SELECT @iNextRowId = MAX(iRowId)
FROM CustomerTable
WHERE iRowId < @iCurrentRowId

Be sure to reset your looping variable(s) to NULL before 
retrieving the next @iNextRowId value. This is critical 
because the SELECT statement used to get the next iRowId 
will not set the @iNextRowId variable to NULL when it 
reaches the end of the table. Instead, it will fail to 
return any new values and @iNextRowId will keep the last 
valid, non-NULL, value it received, throwing your procedure 
into an endless loop. This brings us to the next point, 
exiting the loop.

When @iNextRowId is NULL, meaning the loop has reached the 
end of the table, you can use the BREAK command to exit the 
WHILE loop. There are other ways of exiting from a WHILE 
loop, but the BREAK command is sufficient for this example.

You will notice that in both procedures I have included the 
comments listed below in order to illustrate the area in 
which you would perform your detailed, row-level processing.



 
Is This Answer Correct ?    3 Yes 1 No
Bhaskar
 

 
 
 
Other SQL Server Interview Questions
 
  Question Asked @ Answers
 
Well sometimes sp_reanmedb may not work you know because if some one is using the db it will not accept this command so what do you think you can do in such cases?  2
write the query for taking database backup in sql Logica-CMG6
What is a table called, if it does not have neither Cluster nor Non-cluster Index?  1
How to find the date and time of last updated table?  2
CREATE TABLE [dbo].[HPMS_CompetencyTypes](CompetencyType varchar(50) ) go create trigger hpms_create_Insert on HPMS_CompetencyTypes for insert as if Exists ( select * from [HPMS_CompetencyTypes] where CompetencyType=(select * from [HPMS_CompetencyTypes])) begin Rollback tran Raiserror ('duplicate value',12,6) go insert HPMS_CompetencyTypes (CompetencyType) values ('new') I'm new to trigger can any one tell me where is the issue. Please.  2
How do you use DBCC statements to monitor various aspects of a SQL server installation?  1
What is the role that is required for killing a process What is the role that is required for creating a linked server IBM1
when we use function instead of procedure? plz tell me the situation with ex?  1
Can we use Truncate command on a table which is referenced by FOREIGN KEY?  2
employee table has employee id ----------- empid ---------------- 1 2 3 3 3 4 5 5 5 6 6 6 7 here the values r repeated two times.how to select the repeated values only.i.e 3,5,6 should alone come.  2
what are the new features of sql server 2005? HP5
How do you implement one-to-one, one-to-many and many-to- many relationships while designing tables? TCS3
Can a stored procedure call itself(recursive). If yes what level and can it be controlled.?  2
What are three SQL keywords used to change or set someone?s permissions?  3
Can you explain the types of Joins that we can have with Sql Server?  2
sql server has its default date format in da form "yy-mm-dd" its possible to convert da current date format of sql server to desired format. Now my question is dat how to get da previous and comin days date in my desired format??  1
what is normalization? what is denormalization? Satyam5
i have 4 tables.. T1, T2, T3, T4.. these tables have the same structure and they store the information entered in different years.. T1 stored 2002, T2 stored 2003, T3 stored 2004 and T4 stored 2005.. i want to copy contents in T1 to T2, T2 to T3, T3 to T4 and T4 to T1.. how do i do that? Temp tables cannot be used..  2
how to retrive only second row from table in sql server 2000? CTS7
What's the maximum size of a row?  4
 
For more SQL Server Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com