Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8034067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:53:43+00:00 2026-06-05T01:53:43+00:00

sql has a Table called emp. emp(emp_id int IDENTITY primary key, EmployeeName varchar(50),…….) I

  • 0

sql has a Table called emp.

emp(emp_id int IDENTITY primary key, EmployeeName varchar(50),.......)

I want to Insert a record to above table. Here is my code in asp.net.

DBconnection dbcon = new DBconnection();
string query = "insert into emp values('" + TextBox_EmpName.Text + "','" + ....);
int no1 = dbcon.insertQuery(query);

I have another table called emp-relation

 emp-relation(emp_id int primary key, count int, ....) 
     -- foreign key (emp_id)references emp(emp_id)

My problem is when I inserting the emp row ,I dont know what is the emp_id since it created by auto. And when I am going to insert to emp-relation , I want to get emp-id since it is the foreign key.

How can I do this? Is there any way to read last Insert row in Sql according to Time stamp or some thing? I believe that records are not sorted according to inserted timestamp in nature. please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:53:45+00:00Added an answer on June 5, 2026 at 1:53 am

    There’s bascally two ways. The first way is to return the new ID from the first insert query:

    insert into emp values(...)
    select scope_identity() as NewID
    

    The second way is to lookup the first row when you insert into the relation table:

    insert  emp-relation
            (emp_idm, ...) 
    select  emp_id
    ,       ...
    from    emp
    where   emp_name = @EmpName
    

    You have to pass in enough columns to make the reference unique.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called table1 which has a composite primary key using 'ScreenId'
I have a SQL database that has the following table: Table: PhoneRecords -------------- ID(identity
I want to create a trigger on a SQL Server table. Table has 8
I have an SQL Server 2005 table that has a varchar(250) field which contains
I have a table in SQL server which has a DATETIME field called Date_Printed.
We are using SQL Server 2008. We have a table called response which has
I have a table called USERS that has a foreign key to the table
I have a SQL Server 2008 database. This database has a Table called Book.
I have a table called Field in my SQL Server database. The table has
I have a table called Shoppings in my SQL db. This one has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.