I simply want a stored procedure that calculates a unique id (that is separate from the identity column) and inserts it. If it fails it just calls itself to regenerate said id. I have been looking for an example, but cant find one, and am not sure how I should get the SP to call itself, and set the appropriate output parameter. I would also appreciate someone pointing out how to test this SP also.
Edit
What I have now come up with is the following (Note I already have an identity column, I need a secondary id column.
ALTER PROCEDURE [dbo].[DataInstance_Insert]
@DataContainerId int out,
@ModelEntityId int,
@ParentDataContainerId int,
@DataInstanceId int out
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
WHILE (@DataContainerId is null)
EXEC DataContainer_Insert @ModelEntityId, @ParentDataContainerId, @DataContainerId output
INSERT INTO DataInstance (DataContainerId, ModelEntityId)
VALUES (@DataContainerId, @ModelEntityId)
SELECT @DataInstanceId = scope_identity()
END
ALTER PROCEDURE [dbo].[DataContainer_Insert]
@ModelEntityId int,
@ParentDataContainerId int,
@DataContainerId int out
AS
BEGIN
BEGIN TRY
SET NOCOUNT ON;
DECLARE @ReferenceId int
SELECT @ReferenceId = isnull(Max(ReferenceId)+1,1) from DataContainer Where ModelEntityId=@ModelEntityId
INSERT INTO DataContainer (ReferenceId, ModelEntityId, ParentDataContainerId)
VALUES (@ReferenceId, @ModelEntityId, @ParentDataContainerId)
SELECT @DataContainerId = scope_identity()
END TRY
BEGIN CATCH
END CATCH
END