Reply to topic
Stored Procedure Help......
MBMunday


Joined: 06 Apr 2004
Posts: 76
Location: Dallas/Fort Worth
Reply with quote
I need some help with this Stored Procedure that I'm using with .NET. What I need it to do first, is select the last record entered, then add 1 to the "ArrestID" field and hold that value for the next section of the SP. Then, I want it to insert the data submitted from the web page form and insert that, along with the new Arrest ID (That now has the 1 added to it) into the "ArrestID" field for the new record.

We were having the ArrestID has an autoincrement and has the PK, but have since decided that we should remodel the table design, and just add 1 to the last Arrest ID each time a new record is added.

Here's the SP. I've stared at this for 3 days and have changed this, change that, in an attempt to get it to work, to no avail. It may be way off by now and I may have unncessary commands in it.

Code:
CREATE  PROCEDURE dbo.sp_assignAdultArrestID

(
   @Name varchar(125),
   @CaseNo varchar(15),
   @OfficerID int,
   @ArrestDate smalldatetime,
   @Charge varchar(125),
   @AssignedBy varchar(125),
       @NewArrestID INTEGER OUTPUT,
   @New_pkid INTEGER OUTPUT

)

AS

SELECT TOP 1 [pkid], [ArrestID] +1  AS LastArrestID
FROM   [dbo].[tbl_ArrestIDNumbers]
ORDER BY [pkid] DESC;

DECLARE @LastArrestID int
SET @NewArrestID = @LastArrestID;

INSERT INTO [dbo].[tbl_ArrestIDNumbersTesting] ([Name],[CaseNo],[OfficerID],[ArrestDate],[Charge],[AssignedBy], [ArrestID],[AssignedDateTime])
VALUES (@Name,@CaseNo,@OfficerID,@ArrestDate,@Charge,@AssignedBy,@NewArrestID,GetDate());


SELECT @New_pkid = pkid FROM dbo.tbl_ArrestIDNumbersTesting WHERE (pkid = SCOPE_IDENTITY());
GO


When I run it in Query Analyzer, it returns the value of the "NewArrestID", but it doesn't insert it into the table in the Insert Command.

Any suggestions?

Thanks,

MB
byron
Forum Admin

Joined: 07 Mar 2004
Posts: 160
Location: Newark, DE, USA
Reply with quote
DECLARE @LastArrestID int

SELECT TOP 1 @LastArrestID = [pkid], [ArrestID] +1
FROM [dbo].[tbl_ArrestIDNumbers]
ORDER BY [pkid] DESC;

SET @NewArrestID = @LastArrestID;

INSERT INTO [dbo].[tbl_ArrestIDNumbersTesting] ([Name],[CaseNo],[OfficerID],[ArrestDate],[Charge],[AssignedBy], [ArrestID],[AssignedDateTime])
VALUES (@Name,@CaseNo,@OfficerID,@ArrestDate,@Charge,@AssignedBy,@NewArrestID,GetDate());


SELECT @New_pkid = pkid FROM dbo.tbl_ArrestIDNumbersTesting WHERE (pkid = SCOPE_IDENTITY());
GO
Stored Procedure Help......
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic