Code:
Create procedure ProcedureName
(
@IdField varchar(255)=NULL, -- Used for Update/Delete
@Col1 varchar(255)=NULL, -- For update alone
@Col2 varchar(10)=NULL, -- For Update alone
@Reason varchar(512)=NULL, -- Used for Inserting into Audit table.
@Operation Bit=0 -- used to decide Update/Delete, 0=update, 1=delete, default is set to UPDATE
)
as
BEGIN TRAN
If @IdField is NULL
BEGIN
Rollback Tran
SELECT 1 as Result -- any nonzero value is unsuccessful transaction.
Return --Cannot do anything further
End
If @Operation=0 -- Update code
If @Col1 is NULL or @Col2 is NULL
BEGIN
Rollback Tran
Select 2 as Result
Return
END
ELSE -- Can run update here.
BEGIN
Update YourTable set Col1=@Col1, Col2=@col2 where IDFIELD=@IdField
--Use your Insert code for audit table
Insert AuditTable Values(@Reason, Getdate, @IdField, 'Update')...
If @@ERROR<>0
BEGIN
Rollback Tran
Select 3 as Result -- ZERO means successful transaction here.
Return
END
END
If @Bit=1 -- Delete Code
BEGIN
Delete From YOURTABLE where IDFIELD=@IdField
--Use your Insert code for audit table
Insert AuditTable Values(@Reason, Getdate, @IdField, 'Delete')...
If @@ERROR<>0
BEGIN
Rollback Tran
Select 4 as Result -- ZERO means successful transaction here.
Return
END
END
Commit Tran
Select 0 as Result -- ZERO means successful transaction here.
return
In your ASP page, you can call this as follows.
In your delete page it should look like...
[code]idFieldVal = Request.Form("IdField")
'define your connection object and recordset object
StrSql="Execute YourProcedure @Operation=1, @IdField=" IdFieldVal
Rsobject = connectionObj.Execute
Result=RsObject("Result")
If cint(Result) = 0 Then Response.write "Deleted"
If cint(Result) = 1 Then Response.write "Delete Failed, Missing IDField value."
If cint(Result) = 4 Then Response.write "Delete Failed, Please try again."
In your delete page it should look like...
[code]idFieldVal = Request.Form("IdField")
Col1Val = Request.Form("Col1Val")
Col2Val = Request.Form("Col2Val")
'define your connection object and recordset object
StrSql="Execute YourProcedure @Operation=0, @IdField=" & IdFieldVal & ", @Col1='" & Col1Val & "', @Col2='" & Col2Val & "'"
Rsobject = connectionObj.Execute
Result=RsObject("Result")
If cint(Result) = 0 Then Response.write "Updated"
If cint(Result) = 1 Then Response.write "Delete Failed, Missing IDField value."
If cint(Result) = 2 Then Response.write "Update Failed, Missing Col1 and Col2 value."
If cint(Result) = 3 Then Response.write "Update Failed, Please try again."
Maybe you got to do additional validations that are necessary.
Hope that helps.
Cheers!
_________________________
-Vijay G

Strive for Perfection
