Click here to Skip to main content
15,908,768 members
Home / Discussions / Database
   

Database

 
QuestionUser Creation in SQL Server 2005 Pin
Brendan Vogt2-Apr-06 21:08
Brendan Vogt2-Apr-06 21:08 
QuestionArray in SQL Pin
Brendan Vogt2-Apr-06 21:02
Brendan Vogt2-Apr-06 21:02 
AnswerRe: Array in SQL Pin
Colin Angus Mackay2-Apr-06 23:18
Colin Angus Mackay2-Apr-06 23:18 
GeneralRe: Array in SQL Pin
Brendan Vogt3-Apr-06 0:57
Brendan Vogt3-Apr-06 0:57 
GeneralRe: Array in SQL Pin
Colin Angus Mackay3-Apr-06 2:11
Colin Angus Mackay3-Apr-06 2:11 
GeneralRe: Array in SQL Pin
Brendan Vogt4-Apr-06 4:14
Brendan Vogt4-Apr-06 4:14 
GeneralRe: Array in SQL Pin
Colin Angus Mackay4-Apr-06 5:21
Colin Angus Mackay4-Apr-06 5:21 
QuestionRe: Array in SQL Pin
Brendan Vogt10-Apr-06 3:56
Brendan Vogt10-Apr-06 3:56 
Just getting back to the this stored procedure. Just a little confused.

The stored procedure is called sp_InsertMember.

What it does is to add the member details, like first and last name, DOB, username and password to the tblMember table. If it @@ERROR is equal to 0, then it loops through the roles string passed as a parameter. The stored procedure used to add the member role is called sp_MemberInRoles_InsertMemberRole, and this I call using the EXECUTE function.

Now my questions are:

In sp_InsertMember I start off with a BEGIN TRANSACTION. If the member was added successfully, where do I add the COMMIT TRANSACTION? Before or after looping through the string of roles?

In the sp_MemberInRoles_InsertMemberRole I also have a BEGIN TRANSACTION and a COMMIT TRANSACTION. Lets say the member was added succesully, then it starts to loop through the string of roles. And lets say the member ahs 3 different roles. After the inserting the first role, it encounters an error, so in sp_MemberInRoles_InsertMemberRole it does a ROLLBACK TRANSACTION. Hoe far does it do this rollback? Will it remove the first role it added? What I want it to do is if the member was inserted successfully, and there is an error with inserting roles, that it removes all roles added, plus the member details.

Here is a modified copy of sp_Member_InsertMember:

ALTER PROCEDURE [dbo].[sp_Member_InsertMember]
(
-- input parameters here
)

AS

BEGIN

DECLARE @ErrorCode int
SET @ErrorCode = 0

DECLARE @TranStarted bit
SET @TranStarted = 0

DECLARE @RemainingRoles nvarchar(4000);
SET @RemainingRoles = @Roles;

DECLARE @CommaIndex int;
SET @CommaIndex = CHARINDEX(',', @RemainingRoles);

DECLARE @CurrentRole sysname

IF (@@TRANCOUNT = 0)
BEGIN
BEGIN TRANSACTION
SET @TranStarted = 1
END

IF (EXISTS(SELECT MemberUsername
FROM dbo.Members
WHERE MemberUsername = @Username))
BEGIN
SET @ErrorCode = -2 -- Username already exists
GOTO CleanUp
END

INSERT INTO Members
(
-- column names
)
VALUES
(
-- input parameters
)

IF @@ERROR <> 0
BEGIN
SET @ErrorCode = -1
GOTO CleanUp
END

-- Loop thru roles string
WHILE(@CommaIndex <> 0)
BEGIN
SET @CurrentRole = LEFT(@RemainingRoles, @CommaIndex - 1);
EXECUTE dbo.sp_MemberInRoles_InsertMemberRole @@IDENTITY, @CurrentRole
SET @RemainingRoles = RIGHT(@RemainingRoles, LEN(@RemainingRoles) - @CommaIndex);
SET @CommaIndex = CHARINDEX(',', @RemainingRoles);
END

-- If all is cool
IF (@TranStarted = 1)
BEGIN
SET @TranStarted = 0
COMMIT TRANSACTION
END

RETURN 0

CleanUp:
IF (@TranStarted = 1)
BEGIN
SET @TranStarted = 0
ROLLBACK TRANSACTION
END
RETURN @ErrorCode

END

My sp_MemberInRoles_InsertMemberRole has the exact same structure, but just different input values and column names. Also, please comment on the wat that I have doe things here.

Regards,
ma se
South Africa
AnswerRe: Array in SQL Pin
HimaBindu Vejella3-Apr-06 2:16
HimaBindu Vejella3-Apr-06 2:16 
AnswerRe: Array in SQL Pin
PaulMcKenzie4-Apr-06 11:07
PaulMcKenzie4-Apr-06 11:07 
AnswerRe: Array in SQL Pin
Igor Sukhov4-Apr-06 15:38
Igor Sukhov4-Apr-06 15:38 
QuestionHow to uPload a photo?:) Pin
coolshad2-Apr-06 11:06
coolshad2-Apr-06 11:06 
AnswerRe: How to uPload a photo?:) Pin
Dinuj Nath2-Apr-06 19:51
Dinuj Nath2-Apr-06 19:51 
QuestionOleDbException:"no value given for one or more required parameters" Pin
MohammadAmiry2-Apr-06 10:06
MohammadAmiry2-Apr-06 10:06 
QuestionNewbie ? User Defined SQL Types and C# Pin
verniy_muzhschina1-Apr-06 19:05
verniy_muzhschina1-Apr-06 19:05 
QuestionViewing large databases Pin
Pius__X1-Apr-06 10:14
Pius__X1-Apr-06 10:14 
AnswerRe: Viewing large databases Pin
Colin Angus Mackay1-Apr-06 11:11
Colin Angus Mackay1-Apr-06 11:11 
GeneralRe: Viewing large databases Pin
Pius__X1-Apr-06 12:10
Pius__X1-Apr-06 12:10 
GeneralRe: Viewing large databases Pin
Colin Angus Mackay1-Apr-06 12:26
Colin Angus Mackay1-Apr-06 12:26 
GeneralRe: Viewing large databases Pin
Pius__X2-Apr-06 1:16
Pius__X2-Apr-06 1:16 
Questionhow to connect VB.Net webform with mysql Pin
Amit Agarrwal1-Apr-06 0:28
Amit Agarrwal1-Apr-06 0:28 
AnswerRe: how to connect VB.Net webform with mysql Pin
Paul Conrad1-Apr-06 10:48
professionalPaul Conrad1-Apr-06 10:48 
GeneralRe: how to connect VB.Net webform with mysql Pin
Amit Agarrwal2-Apr-06 20:41
Amit Agarrwal2-Apr-06 20:41 
GeneralRe: how to connect VB.Net webform with mysql Pin
nguyenvhn2-Apr-06 21:42
nguyenvhn2-Apr-06 21:42 
GeneralRe: how to connect VB.Net webform with mysql Pin
Paul Conrad3-Apr-06 4:57
professionalPaul Conrad3-Apr-06 4:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.