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.
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.
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.
Edited 31-Dec-10 23:23pm
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.
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)