Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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
Author
Message
Posted Wednesday, October 17, 2012 5:57 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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)

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


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 7:52 AM
Points: 906, Visits: 2,856
CREATE PROCEDURE addDocDetail
@detailID int = NULL,
...,
...
AS
SELECT @detailID = ISNULL(@detailID, IDENT_CURRENT('Doctor'))

...
...
GO






The SQL Guy @ blogspot

@SeanPearceSQL

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


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 8:10 AM
Points: 15,726, Visits: 28,131
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 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

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

Add to briefcase

Permissions Expand / Collapse