Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Stored Procedure to Add Details to a table Expand / Collapse
Posted Wednesday, October 17, 2012 5:57 AM


Group: General Forum Members
Last Login: Thursday, June 6, 2013 2:25 PM
Points: 24, Visits: 50
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)

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='',@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.
Post #1373742
Posted Wednesday, October 17, 2012 6:05 AM

Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, October 3, 2016 5:19 AM
Points: 1,144, Visits: 3,432
@detailID int = NULL,
SELECT @detailID = ISNULL(@detailID, IDENT_CURRENT('Doctor'))


The SQL Guy @ blogspot


About Me
Post #1373748
Posted Wednesday, October 17, 2012 8:21 AM



Group: General Forum Members
Last Login: Today @ 4:52 AM
Points: 17,172, Visits: 32,134
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
The Scary DBA
Author of:
SQL Server Query Performance Tuning
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1373827
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse