|
-
Mar 6th, 2012, 11:05 AM
#1
Re: SQL 2005 - Stored Procedures
Something like this?
Code:
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE IncrementQuantityUsed
-- Add the parameters for the stored procedure here
@dateUsed datetime
AS
BEGIN
UPDATE b
set QtyUsed = QtyUsed + 1
from tblBatch b
inner join tblCards c on b.BatchNo = c.BatchNo
where c.Issued = 'Y' and c.DateUsed = @dateUsed
END
GO
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
-
Mar 7th, 2012, 07:53 AM
#2
Thread Starter
Hyperactive Member
Re: SQL 2005 - Stored Procedures
I think I figured this out, I've created a stored procedure using the following code and found a way to call the stored procedure, then voila :-)
Code:
USE SAMPLE_Cards
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Greyskull
-- Create date: 2012-03-07
-- Description: Automatically update SAMPLE_tblBatch of how many Cards are used
-- =============================================
CREATE PROCEDURE IncrementQtyUsed_SAMPLEtblCards
-- Add the parameters for the stored procedure here
@batchNo int
AS
BEGIN
DECLARE @QtyLeft int
DECLARE @OrigQty int
SELECT @QtyLeft=COUNT(*) FROM SAMPLE_tblCards WHERE
BatchNo=@batchNo and Issued IS NULL
SELECT @OrigQty=BatchQty FROM SAMPLE_tblBatch WHERE
BatchNo=@batchNo
UPDATE b
SET QtyUsed = @OrigQty - @QtyLeft
FROM SAMPLE_tblBatch b
INNER JOIN SAMPLE_tblCards s on b.BatchNo = s.BatchNo
WHERE b.BatchNo=@batchNo
END
GO
Do you think this is ok?
Thanks
Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered 
If someone helped you today then please consider rating their post.
-
Mar 7th, 2012, 02:10 PM
#3
Re: SQL 2005 - Stored Procedures
Have you considered using a trigger for this?
From your description it sounds like you want this to happen every time you update the Issued field on any card record. Is that right? If so, a trigger could be your freind. You set up an Update trigger on your Card table and tell it to run your sproc. That way you won't have to call your sproc from code when you update the card table. This is a good thing because it means you'll never forget to put that call in your code, leading to data corruption.
There is a downside though. Many developers (myself included) get very nervous around triggers. The reason is that it's easy to forget about a trigger. Sometimes we run an update against the table and forget the trigger is there. All of a sudden the trigger starts firing and updating stuff we weren't expecting it to. So te rule of thumb is, put it in a trigger if you realy, REALLY want the update to happen EVERY time.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|