Hello,
I recently wrote a survey application. Each question is very similar.
The first questions gives me a problem when there are multiple people
taking the survey (The Database connection Timed out) I am using the
Data Access Application Blocks as ASP.NET (using VB.NET) and SQL 2000.
In there first question there can be up to 27 answers. So I figured
instead of making 27 different trips to the database I woulc just
concatenate my answers on the Stored Procedure and loop through them
and inserting them into my Answers table using a CURSOR. This all
worked great until I stress tested the application. I really think my
problem is that the CURSOR just has way to much overhead when multiple
people will be running the stored procedure.
What I am looking for is an alternative to using a cursor or if
someone could tell me what I am doing wrong (I am sure I am doing
something stupid :-)). Any help would be great.
Also the error I am getting in testing is:
Timeout expired. The timeout period elapsed prior to completion of
the operation or the server is not responding.
I included the Stored procedure that I mentioned above.
CREATE PROCEDURE dbo.usp_InsertQ 1(
@UserName varchar(7),
@FinishedQuesti on int,
@A1 varchar(1)=0,@A 2 varchar(1)=0,@A 3 varchar(1)=0,@A 4
varchar(1)=0,@A 5 varchar(1)=0,@A 6 varchar(1)=0,@A 7 varchar(1)=0,@A 8
varchar(1)=0,@A 9 varchar(1)=0,@A 10 varchar(1)=0,@A 11 varchar(1)=0,@A 12
varchar(1)=0,
@A13 varchar(1)=0,@A 14 varchar(1)=0,@A 15 varchar(1)=0,@A 16
varchar(1)=0,@A 17 varchar(1)=0,@A 18 varchar(1)=0,@A 19
varchar(1)=0,@A 20 varchar(1)=0,@A 21 varchar(1)=0,@A 22
varchar(1)=0,@A 23 varchar(1)=0,@A 24 varchar(1)=0,
@A25 varchar(1)=0,@A 26 varchar(1)=0,@A 27 varchar(1)=0, @UserDepartment
varchar(50),@Co stCenter varchar(6)
)
AS
SET NOCOUNT ON
DECLARE @QuestionID char(11),
@NewSurveyID uniqueidentifie r,
@SurveyIDforIns ert int ,
@AnsNumber int,
@ConAnswers varchar(27),
@myAnswer int,
@RealUserDept int
SET @NewSurveyID = newid()
SET @AnsNumber = 1
SET @ConAnswers = (@A1 + @A2 + @A3 + @A4 + @A5 + @A6 + @A7 + @A8 + @A9
+ @A10 + @A11 + @A12 + @A13 + @A14 + @A15
+ @A16 + @A17 + @A18 + @A19 + @A20 + @A21 + @A22 + @A23 +
@A24 + @A25 + @A26 + @A27)
SELECT @RealUserDept = ccDepartmentID FROM t_CostCenters WHERE
ccCostCenter = @CostCenter
--Insert a new Survey since this is the First Question!
INSERT INTO t_Survey(
ID,
Name,
FinishedQuestio n,
DepartmentFK,
CostCenter)
VALUES(@NewSurv eyID,
@UserName,
@FinishedQuesti on,
@RealUserDept,
@CostCenter)
--Get the Newly Inserted Survey ID
SELECT @SurveyIDforIns ert =SurveyID FROM t_Survey WHERE ID =
@NewSurveyID
DECLARE c1 CURSOR FOR
SELECT qQuestionID
FROM t_Question
WHERE Left(qSequence, 3) = 'Q01'
ORDER BY qQuestionID
OPEN c1
FETCH NEXT FROM c1
INTO @QuestionID
WHILE @@FETCH_STATUS = 0
BEGIN
IF SUBSTRING(@ConA nswers, @AnsNumber, 1) = 0
BEGIN
SET @myAnswer = NULL
END
ELSE
BEGIN
SET @myAnswer = SUBSTRING(@ConA nswers, @AnsNumber, 1)
END
INSERT INTO t_Answers (
aSurveyFK,
aQuestionFK,
aAnswer)
VALUES(@SurveyI DforInsert,
@QuestionID,
@myAnswer
)
SET @AnsNumber = @AnsNumber +1
FETCH NEXT FROM c1
INTO @QuestionID
END
CLOSE c1
DEALLOCATE c1
GO
I recently wrote a survey application. Each question is very similar.
The first questions gives me a problem when there are multiple people
taking the survey (The Database connection Timed out) I am using the
Data Access Application Blocks as ASP.NET (using VB.NET) and SQL 2000.
In there first question there can be up to 27 answers. So I figured
instead of making 27 different trips to the database I woulc just
concatenate my answers on the Stored Procedure and loop through them
and inserting them into my Answers table using a CURSOR. This all
worked great until I stress tested the application. I really think my
problem is that the CURSOR just has way to much overhead when multiple
people will be running the stored procedure.
What I am looking for is an alternative to using a cursor or if
someone could tell me what I am doing wrong (I am sure I am doing
something stupid :-)). Any help would be great.
Also the error I am getting in testing is:
Timeout expired. The timeout period elapsed prior to completion of
the operation or the server is not responding.
I included the Stored procedure that I mentioned above.
CREATE PROCEDURE dbo.usp_InsertQ 1(
@UserName varchar(7),
@FinishedQuesti on int,
@A1 varchar(1)=0,@A 2 varchar(1)=0,@A 3 varchar(1)=0,@A 4
varchar(1)=0,@A 5 varchar(1)=0,@A 6 varchar(1)=0,@A 7 varchar(1)=0,@A 8
varchar(1)=0,@A 9 varchar(1)=0,@A 10 varchar(1)=0,@A 11 varchar(1)=0,@A 12
varchar(1)=0,
@A13 varchar(1)=0,@A 14 varchar(1)=0,@A 15 varchar(1)=0,@A 16
varchar(1)=0,@A 17 varchar(1)=0,@A 18 varchar(1)=0,@A 19
varchar(1)=0,@A 20 varchar(1)=0,@A 21 varchar(1)=0,@A 22
varchar(1)=0,@A 23 varchar(1)=0,@A 24 varchar(1)=0,
@A25 varchar(1)=0,@A 26 varchar(1)=0,@A 27 varchar(1)=0, @UserDepartment
varchar(50),@Co stCenter varchar(6)
)
AS
SET NOCOUNT ON
DECLARE @QuestionID char(11),
@NewSurveyID uniqueidentifie r,
@SurveyIDforIns ert int ,
@AnsNumber int,
@ConAnswers varchar(27),
@myAnswer int,
@RealUserDept int
SET @NewSurveyID = newid()
SET @AnsNumber = 1
SET @ConAnswers = (@A1 + @A2 + @A3 + @A4 + @A5 + @A6 + @A7 + @A8 + @A9
+ @A10 + @A11 + @A12 + @A13 + @A14 + @A15
+ @A16 + @A17 + @A18 + @A19 + @A20 + @A21 + @A22 + @A23 +
@A24 + @A25 + @A26 + @A27)
SELECT @RealUserDept = ccDepartmentID FROM t_CostCenters WHERE
ccCostCenter = @CostCenter
--Insert a new Survey since this is the First Question!
INSERT INTO t_Survey(
ID,
Name,
FinishedQuestio n,
DepartmentFK,
CostCenter)
VALUES(@NewSurv eyID,
@UserName,
@FinishedQuesti on,
@RealUserDept,
@CostCenter)
--Get the Newly Inserted Survey ID
SELECT @SurveyIDforIns ert =SurveyID FROM t_Survey WHERE ID =
@NewSurveyID
DECLARE c1 CURSOR FOR
SELECT qQuestionID
FROM t_Question
WHERE Left(qSequence, 3) = 'Q01'
ORDER BY qQuestionID
OPEN c1
FETCH NEXT FROM c1
INTO @QuestionID
WHILE @@FETCH_STATUS = 0
BEGIN
IF SUBSTRING(@ConA nswers, @AnsNumber, 1) = 0
BEGIN
SET @myAnswer = NULL
END
ELSE
BEGIN
SET @myAnswer = SUBSTRING(@ConA nswers, @AnsNumber, 1)
END
INSERT INTO t_Answers (
aSurveyFK,
aQuestionFK,
aAnswer)
VALUES(@SurveyI DforInsert,
@QuestionID,
@myAnswer
)
SET @AnsNumber = @AnsNumber +1
FETCH NEXT FROM c1
INTO @QuestionID
END
CLOSE c1
DEALLOCATE c1
GO
Comment