'SQLite: How to insert the same value to multiple columns?
I want to provide the same value which is a TimeStamp to all the columns in the table. Let's say the columns are unknown.
Is there any way to do this like
INSERT INTO table VALUES ('one value');
without specifying the column names?
UPDATE:
Let's say i have a table named 'student' and i have 'f_name', 'm_name', and 'l_name' in it.
Let's say (Hypothetically) i do INSERT INTO student VALUES ('John');
Then i want my table to look like this.
-----------student------------
f_name | m_name | l_name
------------------------------
John | John | John <--- (The record i just inserted)
Solution 1:[1]
This can be done in postgres actually. I dont think you can do it in sqlite. With the following query you can generate the insert statement that you want without having to know the columns of the table. Replace MY_TABLE with the table name you have in your DB.
SELECT 'INSERT INTO MY_TABLE ' || array_to_string(ARRAY(SELECT '' || c.column_name
FROM information_schema.columns As c
WHERE table_name = 'MY_TABLE'
), ',') || ' VALUES (' ||
array_to_string(ARRAY(SELECT '' || '\'MY_VALUE\''
FROM information_schema.columns As c
WHERE table_name = 'MY_TABLE'
), ',') || ');'
As sqlstmt
Solution 2:[2]
Sqlite and Mysql does not support multi-table insertion on a single query.
Sqlite http://www.sqlite.org/lang_insert.html
MySql http://dev.mysql.com/doc/refman/5.7/en/insert.html
However there are some other databases that support such features i.e Oracle. As far as I know.
Solution 3:[3]
You could do it with UPDATE:
UPDATE TableName SET ColumnName = 'Value';
This will only work if you already have data in your Table.
Solution 4:[4]
If you know the numbers of columns then you can use
INSERT INTO table VALUES ('Cardinal',..........,'Cardinal'); //As many times as number of columns
Solution 5:[5]
If you know the column names, you can do this:
insert into student (f_name, m_name, l_name)
select a, a, a from (select 'John' as a ) x
Works on mysql and probably most other databases
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 | Apostolos |
| Solution 2 | Twahanz |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | Jasper Vandaele |
