Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-04-17 : 09:37:19
|
| Wegwijzer writes "I will use one stored procedure to insert and update a record of a table. This table have some foreign key relation ships. When I will add a new record the foreign key fields must have a correct value.The foreign key fields will be later in the stored procedure correct updated with a parameter but before the update those fields must have a correct value when the insert statement will be executed. When I use defaults the values must exist and thats not the solution.Can someone help me with a correct solution ?See exampleCREATE PROCEDURE upd_ref_locatie @id NUMERIC(9, 0), @description varchar(50) ASIF (ISNULL(@id, 0 ) = 0 ) /* id = NULL => new record */BEGIN INSERT INTO ref_locatie DEFAULT VALUES IF (@@ERROR <> 0) BEGIN RAISERROR('Error:', 16, 1) RETURN (99) END /* id in INSERT */ SET @id = IDENT_CURRENT( 'ref_locatie')END/* update record */UPDATE ref_locatie SET mutdat = GETDATE(), description= @descriptionWHERE id = @idIF (@@ERROR <> 0)BEGIN RAISERROR('Fout: Kan record niet wijzigen', 16, 1) RETURN (99)ENDRETURN 0GO" |
|
|
motokevin
Starting Member
36 Posts |
Posted - 2002-04-17 : 12:37:03
|
| It sounds like your procedure has two parts and maybe consists of two separate procedures. I'm assuming the steps are as follows:Step 1: Get data into the first table with any values as foriegn keys, as long as they are valid foriegn keys.Step 2: Update the inserted records with the correct foriegn key values.With those assumtions, here's what I think you should do.Declare @FK_1 Integer -- guessing that you're using Integers for keysDeclare @FK_2 IntegerDeclare @FK_3 IntegerSelect Top 1 @FK_1=Table_1.ID,@FK_2=Table_2.ID,@FK_3=Table_3.IDFrom Table_1,Table_2,Table_3INSERT INTO ref_locatie (FK_1,FK_2,FK_3,...other column values here)VALUES (@FK_1,@FK_2,@FK_3,...other column values here)-- continue with code to update record... |
 |
|
|
|
|
|
|
|