Insert into table access 2010 - ms-access-2010

How can i copy contents of my whole table in sql access and update one column to the same table.
So basically is selecting the whole table and updating one column and pasting that to the same table. thanks.

To clone an existing table back into the same table, but using a different value for one field, use something like the following (see notes that follow!):
INSERT INTO Table1 ( FldA, FldB, MyDate, StateCode )
SELECT Table1.FldA, Table1.FldB, Table1.MyDate, "FL" AS Expr1
FROM Table1;
You can't easily use the * to select/update all fields because your need to change one field would result in a "duplicate destination" error. If needed, you could use an 'IF' statement to change to different values (i.e. IF(FldA="VA","FL",IF(FldA="MD","TX",Flda))

Related

merge secondary database into main one avoiding duplicate

I have two databases with the same structure. The first is the main one, while the second get updated periodically (in reality I have multiple "secondary" databases that I want to merge one by one into the main one).
The structure of the main and the secondary databases is identical.
I want to periodically dump all new values from the secondary database in the main one. However, the second time I do it, I want to exclude rows that were already copied the first time (and so on).
The tables in all these database have:
an ID column set as PRIMARY KEY going from 1 to N for each database (I suspect this was a mistake, but at the moment I can't change this)
a DATE column, representing a posix timestamp (float)
some other columns
My code looks like this:
ATTACH DATABASE secondary.db AS temp_db
DROP TABLE IF EXISTS my_table_temp
CREATE TABLE my_table_temp AS SELECT * FROM my_table
INSERT INTO main.my_table_temp SELECT * FROM temp_db.my_table
DELETE FROM my_table
INSERT INTO main.my_table SELECT DISTINCT * FROM main.my_table_temp ORDER BY date
DROP TABLE my_table_temp
the problem is that - I suspect due to the repeated ID column - the DISTINCT clause returns me:
UNIQUE constraint failed: my_table.id
However I don't care at all of the ID field that could also be dropped or reset.
NOTES:
the secondary databases are constantly updated by a code that - at the moment - I can't change
I initialize the "main" database copy-pasting one of the secondary to avoid regenerating the whole structure from scratch. Maybe there is a better way of doing this
Apologies if this is a naive question, but I'm very new with SQLite.
Thanks
Following the advice from #forpas, I solved this with the following code:
Assuming the columns to be id,date,col1 and col2
ATTACH DATABASE secondary.db AS temp_db
DROP TABLE IF EXISTS my_table_temp
CREATE TABLE my_table_temp AS SELECT date,col1,col2 FROM my_table
INSERT INTO main.my_table_temp SELECT date,col1,col2 FROM temp_db.my_table
DROP TABLE my_table /* I need to recreate my_table as I've removed a column*/
CREATE TABLE main.my_table AS SELECT DISTINCT date,col1,col2 FROM main.my_table_temp ORDER BY date
DROP TABLE my_table_temp
also, I automatized the extraction of the column names doing
SELECT name FROM PRAGMA_TABLE_INFO('my_table');
This is then passed to the python code running the script and the column id is removed from the list. Note that the second (and following) time I run this code, the column id won't be present in my_table to start with. However this approach allows the code to be the same in the two cases: either if the column id is there or not.
This procedure is then iterated over each table name to fully merge the two databases.

Chnage table Name and Its Field Name Also Without missing any Data In Sqlite

I have created one table 'Temp1'. with fields "id,pName,pid" etc.
but i want to Replace this table name with 'temp2' and fields name aslo with "no,name,rollno" without any data loss.
and also add one extra column compName in new created table Temp2.
can any one help me how i can achieve this.
Plz Help me.
Thanx in advance.
CREATE TABLE temp2 (no, name, rollno);
INSERT INTO temp2 SELECT id, pname, pid FROM temp1;
I assumed no datatypes or constraints on columns, so you need to adjust if you want some constraints.
Now you can verify that you have all the data in new table. Then, if you no longer need Temp1, you can drop it:
DROP TABLE temp1;
and if you want to shrink database (remove unused parts of the database file):
VACUUM;

SQLite - select every row from all tables where a column name exists

I need to extract all rows from every table that has the column imgAssetURL to add to a pre loading system.
I think in essence something like:
SELECT imgAssetURL FROM *
What are my options?
The definitions for all tables are located in the sqlite_master table. You would have to read those definitions, figure out which tables have the column in it, and run a query on each of those.
See http://www.sqlite.org/fileformat2.html#sqlite_master

I have to update one table field as another field of different table in sql

I have to update one table field as another field of different table in sql. There are two tables, create_account and Trans_Details. I have to update amount field of create_account table and set it into current_balance of Trans_Details table. How can I join these two tables? please write a query for this.
You can utilize the sub queries to do this...
Example:
I do not know what kind of data base r u using i am just giving a pseudo code..
UPDATE create_account SET (amount) = ( SELECT current_balance FROM Trans_Details
WHERE filter = some_value) WHERE filter = some_value.
I have added the where clause in the query, please ignore if u want to update the entire row in the column amount.

SQLite Query to Insert a record If not exists

I want to insert a record into a sqlite table if its actually not inserted.
Let's say it has three fields pk, name, address
I want to INSERT new record with name if that name not added preveously.
Can we do with this in a single Query. Seems like its slightly different from SQL Queries sometimes.
Yes, you can do that with a single query.
INSERT ON CONFLICT IGNORE should help you: http://www.sqlite.org/lang_conflict.html
Put a unique key on the name, this will create a conflict when you try inserting a record if the name already exists.
The default is ABORT, so without the IGNORE, the statement will return an error. If you don't want that, use IGNORE.
If you can't make use of a UNIQUE INDEX in combination with INSERT INTO or INSERT OR IGNORE INTO, you could write a query like this;
INSERT INTO table (column)
SELECT value
WHERE NOT EXISTS (SELECT 1
FROM table
WHERE column = value)

Resources