Stored Procedure to Add Details to a table

  • Hi, I have a patient table, nurse table and a doctor table but would like to store the common details, e.g address, phone no etc in a details table rather than have them in the seperate tables.

    I have written a stored procedure that I would like to use to insert data into the details table and will call this procedure from within my application straight after calling the one I have written to populate the patient table, I am trying to extract the last inserted ID from the patient table which will be the foreign key in my detail table, below is the code I am trying as well as the error code I am getting!

    Any suggestions

    CREATE Procedure addDocDetail

    @detailID int = [SELECT IDENT_CURRENT('Doctor')],

    @surname nvarchar(50),

    @firstname nvarchar(50),

    @address1 nvarchar(50),

    @address2 nvarchar(50),

    @town nvarchar(50),

    @county nvarchar(50),

    @postcode nvarchar(10),

    @phoneno nvarchar(20),

    @email nvarchar(50),

    @mobileno nvarchar(20),

    @dob date,

    @gender nvarchar(3)

    AS

    INSERT INTO Detail(DetailID,Surname,FirstName,Address1,Address2,Town,County,PostCode,PhoneNo,Email,MobileNo,DateOfBirth,Gender)

    VALUES(CAST(@detailID AS INT),@surname,@firstname,@address1,@address2,@town,@county,@postcode,@phoneno,@email,@mobileno,@dob,@gender)

    exec addDocDetail @surname ='MacNamara',@firstname='George',@address1='12 Porter Flats',@address2='Fairview Street',@town='Bexleyheath',@county='Kent',@postcode='BX6 7UT',@phoneno='01469885446',@email='mcnamar@hotmail.com',@mobileno='07798655589',@dob='12/8/1973',@gender='M'

    error code

    Msg 245, Level 16, State 1, Procedure addDocDetail, Line 0

    Conversion failed when converting the nvarchar value 'SELECT IDENT_CURRENT('Doctor')' to data type int.

  • CREATE PROCEDURE addDocDetail

    @detailID int = NULL,

    ...,

    ...

    AS

    SELECT @detailID = ISNULL(@detailID, IDENT_CURRENT('Doctor'))

    ...

    ...

    GO

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • Problem is, if another insert occurs on the master table before you get to your detail table, IDENT_CURRENT will not return the one you want. In order to do this, you need to do one of two things, get the identity when you insert the value using SCOPE_IDENTITY and then pass that as a parameter to this new procedure. But, that's limiting because you'll only be able to insert one row at a time. To be able to support multiple rows, you'll need to use the OUTPUT clause of the INSERT statement. That will allow you to capture identity values for more than one row.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply