'Insert into one column by selecting another column in other table but how fill the second column
i have a table which has two columns i'd fill one of the columns by selecting other table column data but how can i fill the next column cause i can't use VALUE. Here's the code
INSERT INTO Numbers(number, val) SELECT LaptopID FROM Laptop WHERE Laptop.Pid = 2
as you can see the "val" column left empty how can i fill that?
Solution 1:[1]
Use NULL
if the column allows it:
INSERT INTO Numbers(number, val)
SELECT LaptopID, NULL
FROM Laptop WHERE Laptop.Pid = 2
Or use the intended (hardcoded) value that you want.
If number:
INSERT INTO Numbers(number, val)
SELECT LaptopID, 2
FROM Laptop WHERE Laptop.Pid = 2
or if text:
INSERT INTO Numbers(number, val)
SELECT LaptopID, 'val'
FROM Laptop WHERE Laptop.Pid = 2
Solution 2:[2]
If you don't have a corresponding value that needs to go into number; then you can just put zero or NULL:
Somethign like this---
INSERT INTO numbers (number, val)
SELECT NULL, laptopid
FROM laptop
WHERE laptop.pid = 2
Solution 3:[3]
You would add it as an argument on the SELECT. For example:
INSERT INTO Numbers(number, val)
SELECT LaptopID, 'val'
FROM Laptop
WHERE Laptop.Pid = 2
Solution 4:[4]
IMHO, you need an INNER JOIN with that other table, something like this:
INSERT INTO Numbers(number, val) SELECT L.LaptopID, OT.OTHER_COLUMN FROM Laptop L
INNER JOIN OTHER_TABLE OT ON L.SOME_COLUMN = OT.ANOTHER_SOME_COLUMN
WHERE Laptop.Pid = 2
Solution 5:[5]
SQLCE does not support Multiple table Update etc, so i used the following method
SELECT Laptop.LaptopID, Table2.val FROM Laptop, Table2 WHERE
Laptop.Pid = 2 or where laptop.pid= table2.pid
then bulk insert using http://sqlcebulkcopy.codeplex.com/
hope it helps :)
Solution 6:[6]
INSERT INTO answer(userans) VALUES(OptionA) Select username From answer WHERE username= 'Name';
I want to add a data in a column with reference to another column in the same table.
Solution 7:[7]
INSERT INTO Numbers(number, val) SELECT LaptopID, '"+yourlabel.Text+"' FROM Laptop WHERE Laptop.Pid = 2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | aF. |
Solution 2 | Roberto Navarro |
Solution 3 | Gordon Linoff |
Solution 4 | Nathan |
Solution 5 | chridam |
Solution 6 | habib |
Solution 7 | nazer bahaa |