I am working on a Stored Procedure that needs to re-insert elements in a table with an identity column. Doing this, for some rows it needs to specify the identity column, for some others it would be preferable for a new identity value the be generated by the identity column. Is it possible of doing this in a single statement instead of an IF-ELSE
?
Here the solution I have for now (where id_column
is the identity column) :
DECLARE @id_to_insert INT, @val1 INT, @val2 INT, @val3 INT, @val4 INT;
-- Do some things
IF @id_to_insert IS NOT NULL
BEGIN
SET IDENTITY_INSERT dbo.table1 ON;
INSERT INTO dbo.table1 (id_column, col1, col2, col3, col4, col4)
VALUES (@id_to_insert, @val1, @val2, @val3, @val4);
SET IDENTITY_INSERT dbo.table1 OFF;
END
ELSE
BEGIN
INSERT INTO dbo.table1 (col1, col2, col3, col4, col4)
VALUES (@val1, @val2, @val3, @val4);
END
As you can see, both statements are very similar, it would be interesting not to have to repeat almost the same insert.
EDIT:
This table holds current contacts data. I move archived/deleted contacts to an other table so we keep this table clean and free of unwanted data, but I may sometimes restore them to the current data table. If possible I would like to keep the identity the contact had before being archived/deleted. If not possible (the identity was already reassigned) I need to generate a new one.