Last_Insert_Id issues using TableAdapter with MySQL - mysql

I am having an issue using Last_Insert_Id in a VB.NET TableAdapter Insert query wired to a MySQL database. I've read through numerous posts on this site and others regarding Last_Insert_ID and Scope_Identity, etc. None of which have worked in my case.
A little background, I have two tables, one holds login information (Auto-generated ID, username, password). Another table has a foreign key relationship on the ID values and contains ID, first name, last name, city, state.
In my TableAdapter I have an Insert query that inserts values into the first table and is supposed to return the ID value so that an Insert can be done on table 2.
Here is my Query:
INSERT INTO user_logins (user_login, user_pass)
VALUES (#p1, #p2)
I wanted to add Last_Insert_Id to make the query
INSERT INTO user_logins (user_login, user_pass)
VALUES (#p1, #p2)
SELECT LAST_INSERT_ID();
However that will only return a value of 1, regardless of what the ID is. If I open the Query Builder I get a message that states "Unable to parse query text". I tried changing the ExecuteMode to Scalar, but that didn't help either.
The Insert part is working perfectly, if I could only obtain the ID value back after insert.
Does anyone know anything I might try, or alternatively, some better way to achieve this?
Thanks!

You don't even need to use SELECT LAST_INSERT_ID();
just INSERT INTO user_logins (user_login, user_pass) VALUES (#p1, #p2) is OK
To retrieve last insert Id you can use two ways
Dim t As Integer
cmd.ExecuteNonQuery()
t = (Int32) cmd.LastInsertedId
OR
t = Convert.ToInt32(cmd.ExecuteScalar())

Related

SQL Statement to Update Rows using ids from another table

and thanks in advance. I have a table called users which contains an id. I would like to collect the ids only from that table and run and insert using those ids as one of the arguments. I am new to SQL and having some trouble finding help or a solution.
Here is the statement to get the ids
SELECT id FROM public.usersWith the ids that are returned I would like to run an insert or update resembling insert into public.history (user_id, password, change_time) values (<ids from prev SELECT>, 'password', now());
Can I generate a loop? Could someone point me in the right direction using purely sql, I know this can be achieved in php but I'd like to include this in an db init with SQL only.
You can do it with INSERT INTO ... SELECT:
INSERT INTO public.history (user_id, password, change_time)
SELECT id, 'password', now()
FROM public.users

LAST_INSERT_ID doesn't work in my case

$stmt2 = $db->prepare("INSERT INTO
usertabbrige(`tabId`,`uId`)
VALUES
((LAST_INSERT_ID()),$userId)");
anything wrong with this query? It's wrap within my first stmt, which will insert a value into uId (PK) in other table. usertabbrige table contain a field uId which is a FK.
Do not use LAST_INSERT_ID() in your query. You dont know which insert statement was last in current session. You can insert to one table, and if you use LAST_INSERT_ID() in another query, you dont actually know where LAST_INSERT_ID() came from.
As I can see you are using PDO. After you executed an insert query, save id:
$db->query("INSERT INTO ...");
$lastInsertedTabId = $db->lastInsertId;
Use it in your next prepared statement
$stmt2 = $db->prepare("INSERT INTO
usertabbrige(`tabId`,`uId`)
VALUES
($lastInsertedTabId ,$userId)");

MySql Insert to multiple tables based on the ID generated in the first table

I have three tables in my database. A users table, StoreA and StoreB
StoreA and StoreB both have a unique key which is the user ID value.
What I want is; When I create a user and insert them into the database, how can I Insert a row into the other two tables without too many additional queries.
I figure I can do this by inserting the user in one query,
then in another return the newly created user ID,
then in another, using said ID, create rows in StoreA and StoreB
Can I cut out the middle query?
Can I cut out the middle query?
YES
START TRANSACTION;
INSERT INTO user (id, name, other)
VALUES (null, 'John','rest of data');
INSERT INTO storeA (id, user_id, other)
VALUES (null, #user_id:= LAST_INSERT_ID(), 'rest of data');
INSERT INTO storeB (id, user_id, other)
VALUES (null, #user_id, 'rest of data');
COMMIT;
See: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
It's a good idea to do this in a transaction, you you're not stuck with just a user with no other data if something goes wrong.
It's not a DB requirement though.
Yes - there should be a function available to get the last inserted ID (assuming it's an autoincrement field) without another query. In PHP, it's mysql_insert_id(). Just call that after the first query.
YES
Q1: insert into table1 values (...);
Q2: insert into table2 values (last_insert_id(), ...);
last_insert_id is the default mysql build-in function
Most of the mysql libraries in various programming language did support return last insert id.
But You did not mention what sort of language you are using to connect to mysql.,
so cannot provide any example
I just wanted to share a php solution.
If you're using mysqli, first execute your insert query.
Then do
$db_id = $this->db->insert_id;
Why don't you use their username as the primary key instead of creating an arbitrary user_id field thats auto incremented? Their user names are unique, right?

increment multiple row FK Ids with trigger on insetion from XLS batch SQL

I had posted other questions relating to this problem, but haven't had any responses to directly address the issue of multiple row data imports from XLS. I'm an infrequent user of SQL or DBs in general, so my background/experience is limited in regard to writing these queries. If there is an easier or more direct approach to reach my goal, I'm certainly open to them. I don't mean to overpost or anything, but this site seems to be the most helpful (thank you everyone who has replied to my other post).
From some posts I have looked at, I understand that I have a working set-based query/trigger (since multiple rows do get imported). Ultimately I only need to import data into the parent table, and the child table can be populated with static values and or values from the parent table, but the PK/FK relationship needs to be maintained. And this is what I seem to have the most trouble with when more than 1 row of data is imported from XLS.
I have set up a trigger to insert values into a child table when a insert is executed on the parent table. The query executes correctly however I am unable to have the FK match the PK when multiple rows of data are inserted. The FK always has the ID of the last row inserted in the parent table. I have tried several approaches from other forum posts (here and on other sites) but always get errors.
Here is my updatePgVer Trigger code:
ALTER TRIGGER [updatePgVer]
ON [prototype].[dbo].[PageVersion]
FOR INSERT AS
BEGIN
SET NOCOUNT ON;
-- Insert into PageHistory
INSERT
INTO [prototype].[dbo].[PageHistory] ([VersionID], [Date], [Action], [Who], [StateId], [Owner])
SELECT
##IDENTITY
, GETDATE()
, 'created'
, 'xls_user'
, [StateID]
, 'xls_user'
FROM inserted
END
And the query used to insert into the parent table:
INSERT INTO [prototype].[dbo].[PageVersion] ([Number], [PageId], [Properties], [StateId], [Language], [SearchText], [PageMetaDescription], [PageMetaKeyWords], [PageTypeId], [Name], [Title], [Owner], [Admin], [ShowInMenu])
SELECT [Number], [PageId], [Properties], [StateId], [Language], [SearchText], [PageMetaDescription], [PageMetaKeyWords], [PageTypeId], [Name], [Title], [Owner], [Admin], [ShowInMenu]
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=C:\test_import.xls', 'SELECT * FROM [Query$]');
The only other idea I have would to be to create some sort of loop that goes through each row and imports 1 at a time, so that the ##IDENTITY will always match. However, examples I have looked at seem hard to apply to my import.
The value for column VersionID, which appears to be the column with IDENTITY, is in the inserted table. You can reference it in your trigger like this
INSERT
INTO [prototype].[dbo].[PageHistory] ([VersionID], [Date], [Action], [Who], [StateId], [Owner])
SELECT
[VersionID],
, GETDATE()
, 'created'
, 'xls_user'
, [StateID]
, 'xls_user'
FROM inserted
If you want to see what data is available from inserted during the INSERT, temporarily put this in your trigger:
SELECT *
FROM inserted

MySQL LAST_INSERT_ID() used with multiple records INSERT statement

If I insert multiple records with a loop that executes a single record insert, the last insert id returned is, as expected, the last one. But if I do a multiple records insert statement:
INSERT INTO people (name,age)
VALUES ('William',25), ('Bart',15), ('Mary',12);
Let's say the three above are the first records inserted in the table. After the insert statement I expected the last insert id to return 3, but it returned 1. The first insert id for the statement in question.
So can someone please confirm if this is the normal behavior of LAST_INSERT_ID() in the context of multiple records INSERT statements. So I can base my code on it.
Yes. This behavior of last_insert_id() is documented in the MySQL docs:
Important
If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only. The reason for this is to make it possible to reproduce easily the same INSERT statement against some other server.
This behavior is mentioned on the man page for MySQL. It's in the comments but is not challenged, so I'm guessing it's the expected behavior.
I think it's possible if your table has unique autoincrement column (ID) and you don't require them to be returned by mysql itself. I would cost you 3 more DB requests and some processing. It would require these steps:
Get "Before MAX(ID)" right before your insert:
SELECT MAX(id) AS before_max_id FROM table_name`
Make multiple INSERT ... VALUES () query with your data and keep them:
INSERT INTO table_name
(col1, col2)
VALUES
("value1-1" , "value1-2"),
("value2-1" , "value2-2"),
("value3-1" , "value3-2"),
ON DUPLICATE KEY UPDATE
Get "After MAX(ID)" right after your insert:
SELECT MAX(id) AS after_max_id FROM table_name`
Get records with IDs between "Before MAX(ID)" and "After MAX(ID)" including:
SELECT * FROM table_name WHERE id>$before_max_id AND id<=$after_max_id`
Do a check of retrieved data with data you inserted to match them and remove any records that were not inserted by you. The remaining records have your IDs:
foreach ($after_collection as $after_item) {
foreach ($input_collection as $input_item) {
if ( $after_item->compare_content($input_item) ) {
$intersection_array[] = $after_item;
}
}
}
This is just how a common person would solve it in a real world, with parts of code. Thanks to autoincrement it should get smallest possible amount of records to check against, so they will not take lot of processing. This is not the final "copy & paste" code - eg. you have to create your own function compare_content() according you your needs.