Is there any Insert overwrite mode in postgres sql like below.
INSERT OVERWRITE INTO TABLE table2 select * FROM table1;
PostgreSQL has the TRUNCATE command to wipe the contents of a table but keep the table itself. You would have to use two statements:
TRUNCATE table2;
INSERT INTO table2 SELECT * FROM table1;
If you want to INSERT new records and UPDATE existing records you can use the ON CONFLICT argument of the INSERT statement:
INSERT INTO table2 (id, name)
(SELECT id, name FROM table1)
ON CONFLICT (id_pkey) DO UPDATE SET name = EXCLUDED.name;
You need to have a primary key or UNIQUE constraint to perform the conflict check. Full details can be viewed in the INSERT Statement documentation, https://www.postgresql.org/docs/current/sql-insert.html
You can also choose to DO NOTHING on conflict which has the advantage of protecting against inserting duplicate records.
Related
I'm not sure if this was my mistake while I was importing CSV to my database or it was on CSV file itself but almost every record that should be null has now a 'null' string value in it.
The problematic columns have integer or float datatypes they are not string at all...
Is there a reasonably quick way to update the entire database and substitute each 'null' record with a real null?
If not the entire database, a single table would also work since I only got 3 tables. (but it would be nicer to learn anyways)
I'm trying to do something like:
SELECT ALL 'NULL' values in DATABASE and REPLACE with =NULL
***Question is for SQLite, I know there are solutions for SQL Server or other SQL types but couldn't find any for SQLite
if your sqlite has version 3.8.5 or higher, You can replace NULL string with NULL as following
UPDATE
table_name
SET
column_name = REPLACE(column_name,'NULL','');
But the problem: you should do this for each column in each table of your database.
REFERENCES
sqlite-replace-function (www.sqlitetutorial.net)
replace-update-all-rows-sqlite (dba.stackexchange.com)
Edit: Lengthy Approach
In this approach, we will get data out from sqlite file and use code editor to find and replace
Step 1
Take all data out from database and delete original data.
sqlite3> .mode insert
sqlite3> .output filename.sql
sqlite3> select * from table_name;
sqlite3> delete from table_name;
Step 2
Find and Replace data from this filename.sql
Replace NULL with nothing and also table with table_name
Step 3
Import all data back to table
sqlite3> .read filename.sql
After this close your terminal/cmd and again open sqlite database and check that table.
I have columnfamily testtable with data. Can I somehow export data to INSERT statements?
desc testtable will give me code to create it, but how can I export data? thanks.
If you just want to export from one table to another you can use the CQL COPY command:
COPY keyspace.table1(column1, column2) TO 'temp.csv';
COPY keyspace.table2(column1, column2) FROM 'temp.csv';
This will copy all of the data from keyspace.table1 to keyspace.table2
I have the following general structure repeated in a document I'm editing with VIM:
-- Dumping data for table `Blahblah1`
INSERT INTO `Blahblah` VALUES (....
multiple lines of insert statement
-- Table structure for table `Blahblah2`
DROP TABLE IF EXISTS `Blahblah2`;
CREATE TABLE `Blahblah2...
I'd like to use a g[lobal] search/execute to delete from the line beginning -- Dump to the blank line before the line beginning -- Table throughout the dump file. (about 25 tables)
What I've got is
:g/^-- Dumping/ ,/^-- Table/.-1d
Am I close or can anyone offer a better suggestion?
No I can't re-dump the tables. The database was MySQL and I'm converting it to SQLite manually and I don't have the MySQL db any more -- just the dump.
Essentially, I want this:
-- Table structure for table `Blahblah1`
DROP TABLE IF EXISTS `Blahblah1`;
CREATE TABLE `Blahblah1...
-- Dumping data for table `Blahblah1`
INSERT INTO `Blahblah` VALUES (....
multiple lines of insert statement
-- Table structure for table `Blahblah2`
DROP TABLE IF EXISTS `Blahblah2`;
CREATE TABLE `Blahblah2...
to turn into this:
-- Table structure for table `Blahblah1`
DROP TABLE IF EXISTS `Blahblah1`;
CREATE TABLE `Blahblah1...
-- Table structure for table `Blahblah2`
DROP TABLE IF EXISTS `Blahblah2`;
CREATE TABLE `Blahblah2...
It seems to work if you don't include the .:
:g/^-- Dumping/,/^-- Table/-1d
Also, as noted in the comments, the space before the comma is not necessary.
Does anybody face an issue when you drop a temporary table at the Sybase ASE 12 it still persists at a current session. So you encounter "Table already exists" when trying to select data into it again
Well, you need to read the manuals, at least the syntax for the commands you expect to use, before you write code. Otherwise you will face issues at every turn. It depends on what you are trying to do.
SELECT ... INTO #MyTable creates a table and succeeds because it does not exist. So a second SELECT ... INTO #MyTable will try to create #MyTable, find that it exists, and fail.
If you want to perform a second SELECT into the same table, TRUNCATE the table, then use SELECT ... INTO EXISTING TABLE #MyTable.
Or DROP TABLE and skip the EXISTING TABLE modifier.
If you want the table to contain the sum of several SELECTS, obviously, skip the TRUNCATE.
I normally do this:
1) CREATE TABLE #temptable (
....
)
INSERT INTO #temptable
SELECT .....
This will never give error.
This solves another possible error . If the WHERE clause accompanying the "select INTO " yields no rows, the temporary table will not have zero rows but the temporary table won't be created at all. This could make the stored proc blow up later.
I have source server and destination server, i have insert the data from source to destination, but before that i have to delete the records if any in destination - (DTS)
If you want to remove whole existing table than create sql task and write :
drop table (tablename)
If you want to remove only data inside existing table than create sql task and write :
truncate table (tablename)
Create an Execute SQL task in the correct sequence of your package and call the following code to delete the rows
TRUNCATE TABLE tablename
Refer MSDN link for more details