Click here to Skip to main content
15,891,713 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi ,i have two tables

Persons

id(pk)
firstname
lastname


and

Orders

orderNumber
orderdetails
id(fk)

I have to inserting data firstname,lastname,ordernumber,orderdetails using storeprocedure.Anyone pls help me.
Posted
Comments
[no name] 28-Jul-12 16:07pm    
Help with what? So write a stored procedure to insert that data into those two tables.

What is your question ? Your sproc can run any sql you like. It can do more than one statement. It can do more than one insert. Read here[^] to see how to get the last identity value that you inserted, I assume that is your core issue, but you don't say, you show no signs of having done research or thought about this, you just ask us for the answer.
 
Share this answer
 
Comments
chinta123 28-Jul-12 17:32pm    
I think you have no idea regarding this.If you know the exact answer what i asked then tell me otherwise don't waste my tym..
Sergey Alexandrovich Kryukov 29-Jul-12 1:01am    
OK, I got it. You simply have rotten morale. You repeat the same pattern as when I tried to answer your question.

First, you accuse the expert in having no idea on your topic, quite baseless accusation, when people comment on your posts, you play the offended card, and then you go straight rude. I don't know why you do it, perhaps this just how you feel.

You also apply double morale: you do not accept quite polite criticism against you, but think that you have an every right to say offending words (and even obscene words, as I can see).

I do understand that any advice to re-think your life is useless (even though it could greatly help you). I just note that with such attitude you cannot be welcome here; and probably anywhere else. Sorry for you. No, no need to thank me... :-)

Good luck,
--SA
chinta123 29-Jul-12 1:27am    
hello,I got my answer .you first tell your expert how to talk people,and you don't worry about my attitude.2nd thing i am just a beginner in .net.So may be my question easy for you,but if you have no interest to give my answer then don't . I am not forcing you.,and 2nd thing who r u to tell me u r not welcome here ,first you change your attitide then tell me.
Christian Graus 29-Jul-12 1:29am    
Please don't keep brawling with people. If you're too much of a beginner to be able to read articles, and if you need us to give you the code, then you're not a beginner, but a leech. If you want to learn programming, choose an entry point where you can read documentation and understand it. That's the core issue here. A 'beginner' should not be doing what you're doing ( and doubtless charging for it ), you should be working through a beginners book and LEARNING, not just posting questions here and hoping for code you can paste, but don't understand.
chinta123 29-Jul-12 1:41am    
It's sunday .I have many work .I am not in a mood to talk with you.better you shut up..and if you want the code then see..


Collapse | Copy Code

Create proc asp_insert_Persons_order
(
@FirstName varchar(100),
@LastName varchar(100),
@OrderNumber int,
@OrderDetails varchar(100)
)
AS
BEgin

Begin tran
Declare @Id int
Insert into Persons(FirstName, LastName ) values (@FirstName,@LastName )
Select @Id=@@Identity

Insert into Orders values (@OrderNumber ,@OrderDetails ,@Id)

Commit tran
END
You need to create SP where you have two insert queries and one select query. Insert data in table 1 first. Post that, use the key created(you need to fetch it) for this new record inserted and insert a new record in table2.

Following couple of links will help you in learning on how to use SP:
ADO.NET : Using Stored Procedures[^]
MSDN: CREATE PROCEDURE (Transact-SQL)[^]
 
Share this answer
 
Comments
Christian Graus 28-Jul-12 17:34pm    
I don't think he needs a select ? A select for the id he inserted is not very reliable unless he knows the info he inserted is always unique. He needs @@IDENTITY.
Sandeep Mewara 29-Jul-12 3:07am    
I agree Christian. I was talking of selecting the last inserted ID using @@Identity only. (My mistake, I missed mentioning that.)

Example: SELECT @Id=@@Identity
SQL
Create proc asp_insert_Persons_order
(
@FirstName varchar(100),
@LastName varchar(100),
@OrderNumber int,
@OrderDetails varchar(100)
)
AS
BEgin

Begin tran
Declare @Id int
 Insert into Persons(FirstName, LastName ) values (@FirstName,@LastName )
Select @Id=@@Identity

Insert into Orders values (@OrderNumber ,@OrderDetails ,@Id)

Commit tran
END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900