Bind Output Results to Variable - output

I am using a SQLDataSource to insert information into a database.
While it is inserting it also grabs the ID of that row
INSERT INTO InkInventory(CodeNumber, InkType, PMS, Color, Description, PoundAvailable, Location, Supplier)
OUTPUT INSERTED.ID
VALUES (#CodeNumber, #InkType, #PMS, #Color, #Description, #PoundsAvailable, #Location, #Supplier)
When I use Query Builder it gives me the output perfectly, but how do I then refer to those results in my code behind, to use that number in another place?

USE AdventureWorks2012;
GO
--Display the value of LocationID in the last row in the table.
SELECT MAX(LocationID) FROM Production.Location;
GO
INSERT INTO Production.Location (Name, CostRate, Availability, ModifiedDate)
VALUES ('Damaged Goods', 5, 2.5, GETDATE());
GO
SELECT ##IDENTITY AS 'Identity';
GO
--Display the value of LocationID of the newly inserted row.
SELECT MAX(LocationID) FROM Production.Location;
GO

Related

MySQL REPLACE without all fields provided

I have mysql query to replace some records:
REPLACE INTO product_core
(id, title, description, category_id)
VALUES (2, 'new_title', 'new_description', 33)
Can I do the same, but not providing all needed values? Example:
REPLACE INTO product_core
(id, title, description, category_id)
VALUES (2, 'new_title', 'new_description') #no category_id
Got error wrong number of values here near
I want to bulk replace many records, but I do not want to query all fields before. In this example, I want to update category_id for some records, but not for all.
REPLACE INTO product_core
(id, title, description, category_id)
VALUES (2, 'new_title_2', 'new_description_2'), #no category_id
(3, 'new_title_3', 'new_description_3', 34) #with category_id
Is it real to do this? Replace some fields for one record and other fields for second record in one query.
Or if is it real to provide special variable meaning that some fields will be the same as before replace (category_id)?
VALUES (2, 'new_title_2', 'new_description_2', #category_id_same_as_before)
Can I do the same, but not providing all needed values? Example:
REPLACE INTO product_core (id, title, description, category_id) VALUES
(2, 'new_title', 'new_description') #no category_id
Yes, the correct query is:
REPLACE INTO product_core
(id, title, description)
VALUES (2, 'new_title', 'new_description') #no category_id
EDIT: As Tom commented below the above might be misleading as for the omitted columns default values will be used, not the ones set for the record which is being replaced.
Is it real to do this? Replace some fields for one record and other fields for second record in one query.
It's not possible in MySQL. The column list is common for all the values sets.
Or if is it real to provide special variable meaning that some fields
will be the same as before replace (category_id)?
It's perhaps possible, but not straightforward and not in all MySQL versions. In the docs they say: "You cannot refer to values from the current row and use them in the new row".
In MySQL > 8.0.19 perhaps VALUES(ROW) can help. Or you can perhaps write you own UDF which does it.
You can't omit these columns from a REPLACE command, unless it is the default value for the column.
According to the documentation:
Any missing columns are set to their default values. [...] You cannot refer to values from the current row and use them in the new row.
It may be better to use a standard UPDATE command instead, which can reference the current column value.

Have column auto assigned through query

Essentially I want a column to give itself a value based off a value from another table when it gets inserted.
version_numbers (table) (id, ...): 1, 2, 3, 4, 5, 6
application_debug (table) (debug_id, ver_id ...)
On insertion, debug_id is set by the auto increment, but I would also like ver_id to be set with a query like this ver_id = SELECT MAX(id) FROM Version_Numbers
In this example, when inserting into application_debug I would like ver_id to be populated with the greatest id stored in version_numbers. I'm aware this can be done in multiple statements, but was wondering if there was a way to make the default values of a column be evaluated through a query on insertion.
Hi you can use select from as below:
insert into application_debug(ver_id) select max(id) from version_numbers;
If you want to insert any static values into application_debug which are not from verions_numbers, then you can write the query as below:
insert into application_debug(ver_id,col2) select max(id),'val2' from version_numbers;

Insert Into query broken in mysql

I need to run this kind of query.
INSERT INTO `book_newBookOrder` (id, volume, 5, chapterNr, sectionNr, `text`)
SELECT id, volume, bookNr, chapterNr, sectionNr, `text`
FROM book_oldBookOrder
WHERE booknr = 1;
The fixed value 5 in the INSERT INTO part breaks it.
I must be able to specify the two values as above.
So I want to select everything with bookNr = 1 in the oldbooknr table and store that as booknr 5 in the newbookorder table.
Please advise me. Thanks.
You have a syntax error: The items in the first set of brackets in the INSERT should be the field names. "5" is not a field name, that's the value you want to be inserted (I assume you wish to set this value the same in every row which gets inserted?). That should be in the SELECT:
INSERT INTO `book_newBookOrder` (id, volume, bookNr, chapterNr, sectionNr, `text`)
SELECT id, volume, 5, chapterNr, sectionNr, `text`
FROM book_oldBookOrder
WHERE booknr = 1;

Last_Insert_Id issues using TableAdapter with 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())

Using Mysql IF condition for multiple INSERT

I would like to use condition in Mysql as below
I have 2 tables- tblPage , tblContent
The SQL should do something in the following order
check if page_name="Home" in tblPage, if false, go to step 2.
Insert some values in tblPage, and get the Last Insert Id.
Insert some values in tbContent with the last Insert Id (of tblPage), and
get the lastInsertId of its own.
Insert some values in tbContent with the lastInsertId (of tbContent), and
get the lastInsertId of its own.
Go on inserting this way.
Step 1 checks to see if any specific value exists in specified column
Step 2 IF the specified value="Home" exist,skip step, If Not, insert some values in tblPage and get the last Insert Id
Step 3 insert some values with the lastInsertId of the previous INSERT in the same table
The step may go on this way inserting some other rows in the same table,with each INSERT getting the
Last Insert Id for the next
I think its posible with MySql IFNULL,NULIF,LAST_INSERT_ID etc, but I cant get it through
I tried with this but the IF condition is still absent.I need to execute the rest of the query if name='home' exists in tblPage
BEGIN
INSERT INTO 'tblPage' ('id','name','url') VALUES
(NULL,'index','home/index');
SET #last_id_in_page = LAST_INSERT_ID();
INSERT INTO 'tblcontent' ('id','page_id','parent') VALUES
(NULL,#last_id_in_page,'home/index');
SET #last_id_in_content = LAST_INSERT_ID();
INSERT INTO 'tblcontent' ('id','page_id','parent') VALUES
(NULL,#last_id_in_page,#last_id_in_content);
SET #last_id_in_content = LAST_INSERT_ID();
INSERT INTO 'tblcontent' ('id','page_id','parent') VALUES
(NULL,#last_id_in_page,#last_id_in_content);
COMMIT;
To get the last inserted id you can use,
$id= mysql_insert_id();
after you insert query and you will get the last inserted id in table .