Click here to Skip to main content
13,596,105 members
Rate this:
Please Sign up or sign in to vote.
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.
Posted 31-Dec-10 20:07pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.180621.3 | Last Updated 1 Jan 2011
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100