PC Review


Reply
Thread Tools Rate Thread

Need Help with Stored Procedure

 
 
Jonathan Wood
Guest
Posts: n/a
 
      13th May 2008
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

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

 
Reply With Quote
 
 
 
 
Jonathan Wood
Guest
Posts: n/a
 
      13th May 2008
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.

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

"Jonathan Wood" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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
>
> --
> Jonathan Wood
> SoftCircuits Programming
> http://www.softcircuits.com
>


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what are the memory caps for threads running as a CLR stored procedure executed by sql server 2005? is it limited by OS only or also by sql servers memory limits? e.g. lets say my clr stored procedure is executed by sql server 2005 then it creates 10 DR Microsoft C# .NET 1 25th Oct 2007 04:27 PM
Map Stored Procedure dependencies from ASP pages through methods to stored procedures dwilliams@newportgroup.com Microsoft Dot NET 6 18th Mar 2005 04:02 AM
Map Stored Procedure dependencies from ASP pages through methods to stored procedures dwilliams@newportgroup.com Microsoft ADO .NET 2 17th Mar 2005 01:47 PM
Error: Executing Stored Procedures-- Cannot Pass TimeStamp Values From sqlCommand Object Parameter to A SQL Stored Procedure =?Utf-8?B?VGVjaE1E?= Microsoft ADO .NET 3 17th Mar 2004 05:03 AM
Calling a Stored Procedure in a Stored Procedure Group DJM Microsoft ADO .NET 1 21st Jan 2004 09:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:37 PM.