Not sure how to google this... Here's what I need to do but I'm not sure how to do the insert and generate the NewID at the same time.
I have 2 tables one (pcx_candidate_to_pcx_vacancyId) is empty and only has 3 fields candidateid, vacancyId and its primary key, all 3 fields are guids. I need to get the data from a second table that has the matching fields but I also have to create and insert a guid at the same time. The source table (pcx_vacancyassociationExtensionBase) has 2 matching fields. Finally I will use NewID() to generate the new guid for the primary key.
You can insert directly from one table to another via an insert into ... select ... query:
insert into pcx_candidate_to_pcx_vacancyId (id, candidateid, vacancyId)
select NewID(), candidateid, vacancyId
from pcx_vacancyassociationExtensionBase
Related
I want to create a table using query result. But I want to also add a auto increment primary key field to it. Is it possible to achieve it using SQLite?
Example:
Select two fields from table_a. But I want the output schema as (id, field_a, field_b).
create table foo as
select field_a, field_b
from tablel_a
Currently using SQLite 3, but solutions using other database systems are also fine.
This is not possible with a single statement; CREATE TABLE ... AS ... does not create constraints.
You have to use two statements:
CREATE TABLE foo ( ID INTEGER PRIMARY KEY, [...] );
INSERT INTO foo (...) SELECT ...;
by default sqlite adds a rowid column in every table you create , so unless there's some specific need here, you can use this rowid column
check this out https://www.sqlite.org/lang_createtable.html#rowid
I have two SQL databases with the same schema. Both have different Data but are using the same primary keys. I want to add the Data from one database to the second one, but all solution I found just update the rows with the same primary keys and dont attach them at the end.
Anyone has a solution?
If your primary key column is an ID field that uses an auto-incrementing sequence (if not, then how would you choose a different primary key for overlapping records anyway?), you should just be able to insert the records from the first schema, excluding the ID in your select query.
For example:
Table Schema:
ID INT(10)
Name VARCHAR(20)
Desc TEXT
SQL
INSERT INTO schema2.table (Name, Desc)
SELECT Name, Desc
FROM schema1.table
I was trying to insert records to a table using statement like
insert into table
(table_id, xxx,xxx)
values
(100,xxx,xxx)
and get the unique violation on the primary key.
and i did a select
select * from table where table_id = 100;
There is no record there.Thought insert before then, delete record.
And if i dont insert the table id, i got a null violation.
I was wondering if there is anyway to deal with this(maybe sequence)? I need to insert several thousands records using the inserting statement. some hundered have this prob.
Thanks in Advance!
I have two tables in two separate sqlite3 databases. The datatypes are identical, but the schemas slightly different. I want them to be a single table in a single database with the same schema as Table 2
Table 1
CREATE TABLE temp_entries (
id INTEGER PRIMARY KEY,
sensor NUMERIC,
temp NUMERIC,
date NUMERIC);
Table 2
CREATE TABLE "restInterface_temp_entry" (
"id" integer NOT NULL PRIMARY KEY,
"dateTime" integer NOT NULL,
"sensor" integer NOT NULL,
"temp" integer NOT NULL
);
id is not unique between the two tables. I would like to create another table with the same schema as Table 2. I would like the id for the entries in Table 1 to start from 0 and then the entries from table 2 to start after the last entry from table 1.
Ideally I would like to just add the entries from Table 1 to Table 2 and "reindex" the primary key so that it was in the same ascending order that "dateTime" is.
UPDATE: I now have both tables using the same schema, I did this by creating a new table with the same schema as Table 2 into the database that held Table 1. I than copied the data to the new table with something like:
INSERT INTO restInterface_temp_entry(id,dateTime,sensor,temp)
...> select id,date,sensor,temp FROM temp_entries;
Background
I used to record a bunch of temp_entries to a csv file. I wanted to put the data into a format that was easier to work with and chose sqlite3. I wrote a program that pulled all of the entries out and put them into Table 1. I wasn't sure what I was doing at the time, and used Table 2 for all new entries. Now I want to combine them all, hopefully keeping id and date in ascending order.
Figured it out.
Open current database.
Attach to original database
ATTACH '/orig/db/location' as orig
Move records from current database to old database, leaving out the PK
insert into orig.restInterface_temp_entry(dateTime,sensor,temp)
...> select dateTime,sensor,temp from main.restInterface_temp_entry;
Clear current databases table
delete from main.restInterface_temp_entry where id > 0
Copy everything updated records from original databases table back to current.
insert into main.restInterface_temp_entry(id,dateTime,sensor,temp)
...> select id,dateTime,sensor,temp
...> from orig.restInterface_temp_entry;
I'm assuming SQLLite supports INSERT INTO SELECT
INSERT INTO newtable (id, datetime, sensor, temp)
SELECT id, date, sensor, temp
FROM temp_entries
ORDER BY id;
INSERT INTO newtable (id, datetime, sensor, temp)
SELECT "id", "dateTime", "sensor", "temp"
FROM "restInterface_temp_entry"
ORDER BY "id";
This should do the trick.
I have two tables, the first has a primary key that is an identity, the second has a primary key that is not, but that key has a foreign key constraint back to the first table's primary key.
If I am inserting one record at a time I can use the Scope_Identity to get the value for the pk just inserted in table 1 that I want to insert into the second table.
My problem is I have many records coming from selects I want to insert in both tables, I've not been able to think of a set based way to do these inserts.
My current solution is to use a cursor, insert in the first table, get key using scope_identity, insert into second table, repeat.
Am I missing a non-cursor solution?
Yes, Look up the output clause in Books online.
I had this problem just this week: someone had introduced a table with a meaningless surrogate key into the schema where naturally keys are used. No doubt I'll fix this soon :) until then, I'm working around it by creating a table of data to INSERT from: this could be a permanent or temporary base table or a derived table (see below), which should suit your desire for a set-based solution anyhow. Use a join between this table and the table with the IDENTITY column on the natural key to find out the auto-generated values. Here's a brief example:
CREATE TABLE Test1
(
surrogate_key INTEGER IDENTITY NOT NULL UNIQUE,
natural_key CHAR(10) NOT NULL CHECK (natural_key NOT LIKE '%[^0-9]%') UNIQUE
);
CREATE TABLE Test2
(
surrogate_key INTEGER NOT NULL UNIQUE
REFERENCES Test1 (surrogate_key),
data_col INTEGER NOT NULL
);
INSERT INTO Test1 (natural_key)
SELECT DT1.natural_key
FROM (
SELECT '0000000001', 22
UNION ALL
SELECT '0000000002', 55
UNION ALL
SELECT '0000000003', 99
) AS DT1 (natural_key, data_col);
INSERT INTO Test2 (surrogate_key, data_col)
SELECT T1.surrogate_key, DT1.natural_key
FROM (
SELECT '0000000001', 22
UNION ALL
SELECT '0000000002', 55
UNION ALL
SELECT '0000000003', 99
) AS DT1 (natural_key, data_col)
INNER JOIN Test1 AS T1
ON T1.natural_key = DT1.natural_key;