Click here to Skip to main content
15,665,942 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had to discuss a bit of conceptual problem here. The scenario is like this:
A user comes on my my website and creates a new profile. As the new profile is created, I tend to save the result in tow tables in datbase viz.

1. profiles
2. profiles_plugins

The 'id' in profiles table is self-generated and hence there is no problem in storing the data it in the profiles table
But at the same time, this self-generated id in table 1 has to be stored in table 2 (profiles_plugins) in the column profile_id.
Data reader cannot fetch the value from table while it is being generated.

So, basically the problem is how to fetch this self generated id from table1 to be saved in table 2 at the same time when the user submits his details.

You can fetch that id using @@RowCount in sql server. But instead of this u can go for the stored procedure and insert the value in profile table first then get the max(profile_id) and insert this into Profile_plugin table it may be easy way to do that.
Share this answer
I hope the Self Generated ID column in Table Profile is IDENTITY Column. In this case you can retreive the newly Generated Identity Value using

@@Identity : Is a system function that returns the last-inserted identity value.
Scope_Identity() : Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch.

Now if you are using same procedure to ad record in both table then simple access the newly created Identity value using @@Identity or Scope_identity.
If you are using 2 seperate Procedure To Create new Profile and insert record in profiles_plugins then you can use OUTPUT Parameter which will return the newly generated Identity Value to the calling code.
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