2

I am having a problem with my database problem.. errr..

INSERT INTO Customer (CustomerName)
VALUES ('Joe Schmoe'); LAST_INSERT_ID();

I used something like that in my winform so this statement:

  INSERT INTO Customer (CustomerName)
  VALUES ('Joe Schmoe'); LAST_INSERT_ID();

-shows me the last inserted id in my customer column

I want to store the value that I can get into a variable so that whenever I put a value to its child table (foreign key) I will use what value that I have stored in my variable to my insert statement.

How can I store it in a variable or a control?

0

1 Answer 1

2

Look at below examples. Cannot give the exact SQL without knowing your database.

SQL Server and C#: get last inserted id

Get inserted id.

How to get last inserted id?

Getting the identity of the most recently added record

C# Code Snippet - Get Identity Column Value from SQL Server

EDIT

If it is MySQL Database, you could use LAST_INSERT_ID() built in function.

Here is a code sample.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.