This stored procedure does the following. Table 1 has a field caled id1. It is the primary key and is an autogenerated incremeneting in an ascending order by 1.
Table 2 has a primary key called id2. The primary key is an autogenerated number incremeneting in an ascending order by 1. It has a foreign key called id1, which is the primary key of Table 1.
So this is how the scenario works out. A HTML form submits to the database. The application logic inserts a record into Table 1 and Table 2 at the same time. As it writes to Table 2 I need the Stored Procedure to grab the last primary key id from Table 1 and insert it as the candidate key. The result would look like the following as records are inserted into both tables
record 1 for Table 1 record 1 for Table 2
id1 id2 id1
1 1 1
record 2 for Table 1 record 2 for Table 2
id1 id2 id1
2 2 2
record 3 for Table 1 record 3 for Table
id1 id2 id1
3 3 3
Here is exactly how I wrot the Stored Procedure. Attached is the error message I got.
CREATE PROCEDURE "SP1"
(@P_Var1 int,
@P_Var2 int)
As
Declare @L_PrimaryID int
Begin Trans
Insert into Table2 (Field1) Values (id1)
SET @L_PrimaryID = Cast(@@IDENTITY as int)
If CAST(@@Error as int) > 0 ErrHandler
Insert into Table2 (field2) Values (@L_PrimaryID)
If CAST(@@Error as int) > 0 ErrHandler
Commit trans
Return 0
ErrHandler:
Rollback Trans
Here are the the errors I get:
Error 170: Line 10: Incorrect syntax near 'Trans'
The name 'id1' is not permitted in this context
Line 16: Incorrect syntax near 'ErrHandler'
Line 20:Incorrect syntax near 'Err Handler'
Incorrect syntax near 'Trans'
Incorrect syntax near 'Trans'
So I guess I goofed on the syntax, but is the logic of this SP kosher? Any suggestions to make this darn thing work would be appreciated. Thanks for all your help.
-G