Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / May 2008

Tip: Looking for answers? Try searching our database.

Need Help with Stored Procedure

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jonathan Wood - 13 May 2008 20:48 GMT
I have two tables, mc_Workouts and mc_WorkoutDetails, that have a
one-to-many relationship with each other.

I use the stored procedure below to copy a workout. It needs to create a new
workout with a new name and then copy all the workout details from the
original workout to the new one.

What it does is create a new workout with the new name but it does not copy
any of the workout details to the new workout. It returns 0, which appears
to indicate success.

Can anyone help spot what I'm missing? Thanks.

ALTER PROCEDURE dbo.mc_Workouts_CopyWorkout
@WorkoutID    bigint,
@UserID     uniqueidentifier,
@NewName    varchar(100)
AS
BEGIN
SET NOCOUNT ON

DECLARE @NewWorkoutID bigint
SELECT @NewWorkoutID=0

BEGIN TRY

 BEGIN TRANSACTION

 -- Create new workout
 INSERT INTO dbo.mc_Workouts (UserID, [Name], Comments)
 SELECT UserID, @NewName, Comments
 FROM dbo.mc_Workouts
 WHERE ID=@WorkoutID AND UserID=@UserID

 -- Get workout ID
 SELECT @NewWorkoutID = SCOPE_IDENTITY()

 -- Copy workout activities
 INSERT INTO dbo.mc_WorkoutDetails (WorkoutID, ActivityID, Reps, Sets,
Minutes, Comments)
 SELECT @NewWorkoutID, ActivityID, Reps, Sets, Minutes, Comments
 FROM mc_WorkoutDetails
 WHERE ID=@WorkoutID

 COMMIT TRANSACTION

END TRY
BEGIN CATCH
 IF @@TRANCOUNT > 0
  ROLLBACK TRANSACTION
 RETURN -1
END CATCH

RETURN @@ERROR
END

Signature

Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Jonathan Wood - 13 May 2008 20:54 GMT
Doh! I got it. The WHERE clause of the second INSERT should be WHERE
WorkoutID=@WorkoutID.

I was worried I had the basic approach wrong. But looks like it was just a
typo.

Signature

Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

>I have two tables, mc_Workouts and mc_WorkoutDetails, that have a
>one-to-many relationship with each other.
[quoted text clipped - 51 lines]
> RETURN @@ERROR
> END

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.