SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stored Procedure to Add Details to a table


Stored Procedure to Add Details to a table

Author
Message
soulchyld21
soulchyld21
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 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.
Sean Pearce
Sean Pearce
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1766 Visits: 3432
CREATE PROCEDURE addDocDetail
@detailID int = NULL,
...,
...
AS
SELECT @detailID = ISNULL(@detailID, IDENT_CURRENT('Doctor'))

...
...
GO






The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Grant Fritchey
Grant Fritchey
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39438 Visits: 32630
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 and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search