I was trying to insert a new row into two tables which has a relationship between. I wrote the stored procedure as follows.
ALTER PROCEDURE InsertUserProfile
(
@UserID varchar(10),
@Pass varchar(50),
@Enabled int,
@Permission int,
@Rank int,
@FName varchar(50),
@LName varchar(50),
@Phone varchar(50),
@Email1 varchar(50),
@Email2 varchar(50)
)
AS
BEGIN TRANSACTION
INSERT INTO tbl_user_login VALUES (@UserID, @Pass, @Enabled, @Permission, @Rank)
IF @@ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
INSERT INTO tbl_user_profile VALUES (@FName, @LName, @Phone, @Email1, @Email2)
IF @@ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
COMMIT
From this follow ASP.NET Code
SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnString"].ConnectionString);
SqlCommand cmd = new SqlCommand("dbo.InsertUserProfile", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@UserID", DbType.String).Value = txtUserID.Text;
cmd.Parameters.Add("@Pass", DbType.String).Value = txtPass.Text;
cmd.Parameters.Add("@Enabled", DbType.Int32).Value = 1;
cmd.Parameters.Add("@Permission", DbType.Int32).Value = Convert.ToInt32(ddlPermission.SelectedValue);
cmd.Parameters.Add("@Rank", DbType.Int32).Value = Convert.ToInt32(ddlRank.SelectedValue);
cmd.Parameters.Add("@FName", DbType.String).Value = txtFName.Text;
cmd.Parameters.Add("@LName", DbType.String).Value = txtLName.Text;
cmd.Parameters.Add("@Phone", DbType.String).Value = txtPhone.Text;
cmd.Parameters.Add("@Email1", DbType.String).Value = txtEmail1.Text;
cmd.Parameters.Add("@Email2", DbType.String).Value = txtEmail2.Text;
sqlConn.Open();
int rows = cmd.ExecuteNonQuery();
sqlConn.Close();
But I'm getting the following error.
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl_user_profile_tbl_user_login". The conflict occurred in database "My DB Location", table "dbo.tbl_user_login", column 'ID'. The statement has been terminated.
I'm new to Stored Procedures so any suggestions how should I fix it so that I could insert data into two tables?
TABLE SCHEMA
tbl_user_login
ID (int)
UserID (varchar10)
Pass (varchar50)
Enabled (int)
Permission (int)
Rank (int)
tbl_user_profile
ID (int)
FName (varchar50)
LName (varchar50)
Phone (varchar50)
Email1 (varchar50)
Email2 (varchar50)
@Richard Its "ID" which is the Auto Increment in both tables.
Having an auto-increment (IDENTITY
) act as a primary key is fine, but using it as a foreign key is dangerous, since you can't really guarantee that they will always be in sync; any rollback could leave them broken (rollback does not undo identity increments, as this would affect other SPIDs). Also, any thread-race between two concurrent INSERT
s will be in jeopardy.
The correct approach here is to query SCOPE_IDENTITY()
after the first insert, and use that in the INSERT
to the second table; i.e. in the second table you tell it the value. Note that since @@ERROR
and SCOPE_IDENTITY()
are floating values, you should query them both directly after the first INSERT
:
SELECT @Error = @@ERROR, @NewId = SCOPE_IDENTITY()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With