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');
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 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 5 years ago.
Improve this question
I have two tables in SQL Server as:
MobileMaster (MobileId int (PK), MobileName varchar)
and
MobileDetails (MobDetailId int (PK), MobileId int (FK), Color char, Ram int, Camera float)
And I have a Windows form with textboxes for Mobile Name, RAM, Camera, Color.
Now I want to insert the data inserted by user, in these tables in such a way that entry should be done in both tables keeping in mind that MobileID from MobileDetails is a foreign key referencing MobileID in the MobileMaster table, and MobileID and MobDetailsID are kept as Identity (auto-increment).
So help me to do this using SQL Server as database and .net as application development platform. I am not able to write query for it which will be executed when user clicks on the Save button.
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 7 years ago.
Improve this question
i have 2 data table have same column and contain value.I want to compare data from both table and find the missmatching data in another table 2.Both table contain 38 column each.
Select
CASE WHEN t1.value = t2.value THEN 'True' ELSE 'False' END AS Result
From Table1 t1
Inner Join Table2 t2 on t1.ID = t2.ID
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 7 years ago.
Improve this question
I have a problem with diacritics in my database, it should store words in Romanian. I'm using nvarchar datatype, what should I do?
I have tried this:
INSERT INTO Raion
VALUES
(1,'Chișinău'),
(2,'Bălți'),
(3,'Comrat'),
(4,'Tiraspol')
But I end up with entries like this:
?oldane?ti
?tefan Voda
Anenii Noi
Bal?i
Basarabeasca
Briceni
Cahul
Here's the table:
CREATE TABLE Raion (
id int Primary Key,
denumire nvarchar(255) Not Null Unique
)
Prefix your denumire value with N to indicate a Unicode constant string
Schema
CREATE TABLE Raion
([id] int, [denumire] nvarchar(8))
;
INSERT INTO Raion
([id], [denumire])
VALUES
(1,N'Chișinău'),
(2,N'Bălți'),
(3,N'Comrat'),
(4,N'Tiraspol')
;
Query
SELECT *
FROM raion
Output
id denumire
1 Chișinău
2 Bălți
3 Comrat
4 Tiraspol
SQL Fiddle: http://sqlfiddle.com/#!3/e91d8/1/0
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.