Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a table named product where there is a field named id its the primary key here. I need to put this key to my another table pro_size_tbl as below
pro_size_tbl
==================
id
productid
sizeid
the matter is when the user adds a product the sizes of the products are saved in session and I am trying to save them in pro_size_tbl by the product key what I just made.
The thing I am doing right now is I am adding the product in the product table and in the next line I am retrieving the max id of the product table and using it as the key for the pro_size_tbl.
My question is is there any better way to do this thing?
This is simple.You can use LAST_INSERT_ID().See this sample:
INSERT INTO table1 (title,userid) VALUES ('test', 1);
SET #last_id_in_table1 = LAST_INSERT_ID();
There is a function LAST_INSERT_ID() which can give you the desired value.
But you need to use right below the insert statement.
owh. from what i understood, u need to put auto generated mysql id to another table?
if so, here is the real deal
$sql_tbl1 = "insert here for first table";
$result_tbl1 = mysql_query($sql_tbl1) or die(mysql_error());
$id = mysql_insert_id();
$sql_tbl2 = "insert here value ($id, 'bla', 'bla')";
mysql_query(sql_tbl2);
LAST_INSERT_ID() function is ok but it is doing the same work as i am doing taking the maximum value.i guess more or less both way are the same.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I wrote this code for one data tabale name "Purchase" i want to link location names, productnames, purchase qty,tfr qty & sales Qty
SELECT location_tbl.location,product_tbl.pro_name,SUM(purchase_tbl.Qty) AS Total_Purchase From((purchase_tbl Inner Join location_tbl ON purchase_tbl.Location_ID = location_tbl.Location_ID)Inner Join product_tbl ON purchase_tbl.item_ID = product_tbl.Pro_ID)GROUP BY location_tbl.location,product_tbl.pro_name;
This is the example table and view
Sorry but to answer ur question u first need to clearly define. You have refered each table primary key as ID. But when u r using that ID into another table u again call it ID. It would be great if u could clearly name each table ID as itemID, storeID so that when u use it another table it is easy to understand. Right now i cannot identify what is the flow in ur data.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm new to C#. I would like to insert data with sub-select into my SQL Server database, I need help to write the string in C# for the following script in SQL Server.
I have two tables, products and category with foreign key between them and a temporary table Mytemp:
CREATE TABLE MyTemp(ID int)
INSERT INTO products (productName, price)
OUTPUT inserted.ID_ProductName
INTO MyTemp
VALUES ('Orange', '2')
INSERT INTO category (ID_productName, Category, Description)
VALUES
((SELECT ID FROM MyTemp), 'Fruits', 'DryFruits');
DROP TABLE MyTemp
You don't need a temp table for this, you can just do it this way:
INSERT INTO products (productName, price)
VALUES ('Orange', '2');
INSERT INTO category (ID_productName, Category, [Description])
VALUES (SCOPE_IDENTITY(), 'Fruits', 'DryFruits');
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this issue in my c# MVC App where i have a receipt and this receipt has a receipt number and i select the last receipt number inserted in the database and add 1 to it, the problem appears when 2 users submitting the receipt at the same time. What happens is that they both get the same receipt number and they both insert into the database, unfortunately one is inserted and the other gives exception and rollback the whole operation. So my question is there a way to prevent this from happening, in other words reserve a receipt number taken by user, so it won’t be used by the other user.
Thanks in advance.
If using sql server you should probably use an IDENTITY column. Another option would be to use a GUID as the key.
So you need to reset the receipt number for each new year? You could make a separate call to get the receipt number first without doing inserts into other tables. This would reserve a number and return immediately. You could use an identity column for this, either a separate table per year or reset the identity value and truncate at the start of each new year. Or you could have a table for Ids with Year and ReceiptNumber as the PK and insert a new row with incrementing receipt number by one.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I work on a small project just to get to understand the mvvm model in C# better.
I work with a Microsoft SQL Server database with three tables: customer, location and address.
Every customer can have one or more locations, and every location has a specific address.
My current thought how to accomplish this:
First insert the customer.
To insert the location, get the highest customer_id and insert the location with the max(customer_id)
Then, to insert the address, get the max(location_id) and insert the address, with the location id
Is there a better way to do this?
I haven't found any tutorial with an example of inserting data into more than one table, especially not using SQL Server.
And my next problem is: what should I bind to my TextBoxes, so that I can insert the content of it?
I thought about having a save button. This button would then execute a method, where I insert the data from the bound TextBoxes. Should I do this with commands, instead of writing a method?
Thanks already!
Sql server has a OUTPUT clause which you can use
something like
INSERT INTO MyTable VALUES({CustomerName})
OUTPUT INSERTED.ID
then you can store the inserted customer's actual ID and do the rest inserts in separate queries.
As for your second question yes you should do it with Command binding to a method in your View model
The most advisable for your situation is to use a transaction.
Example.- https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction(v=vs.110).aspx
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a table that stores information of all the contacts that were added for sending SMS campaign.
Every SMS is associated with campaign. campaignid is used in the record for maintaining that.
I receive the sent mobile_numbers and campaign_id from service provider once the blast is done for campaign.
Now I need to iterate smscontacts table and do the following.
First Get all the records having campaign_id . Update the table by updating column issent as 1 for all the contacts that have mobile number in mobile_numbers list sent by provider after blast.
Edit:
Find rows that have column value for mobile number column in list mobile_numbers and update other columns value called for these records.
Assuming your MobileNumber property is a string and it is null, empty string or white space when it's not set, do the following.
// Get contacts that have a mobile number
IQueryable<Contact> contactsWithANumber = context.Contacts.Select(x => !string.IsNullOrWhiteSpace(x.MobileNumber));
// foreach
foreach(Contact c in contactsWithANumber)
{
c.IsSent = 1;
}
// .ForEach()
contactsWithANumber.ForEach(c => c.IsSent = 1);
// Update in db.
context.SaveChanges();
You can use ExecuteCommand to update multiple records:
yourContext.ExecuteCommand("UPDATE YourTable SET issent= 1 WHERE YourCondition");
For information see DataContext.ExecuteCommand Method