insert values of table in another empty table - sql

Can someone help me to understand where is the probleme ?
I have two tables the first one is :
CREATE TABLE "election-csv" (
"Code du département" DECIMAL NOT NULL,
.
.
...
);
the second one is empty:
CREATE TABLE departement (
code_dpt integer,
DPT varchar(30) NOT NULL,
PRIMARY KEY( code_dpt )
);
-- error when I want to insert the values from the first table --
insert into departement(code_dpt) values (select"Code du département" FROM "election-csv");
I get this error :
SQL Error [42601]: ERROR: syntax error at or near "select"¶ Position : 9

Assuming you're using MSSQL the INSERT statement should look like this:
INSERT INTO departement(code_dpt)
SELECT [Code du département]
FROM election-csv

Related

Need guide on managing sqlite files

I have to do the following:
In 1.sql, write a SQL query to list the titles of all movies released in 2008.
Your query should output a table with a single column for the title of each movie.
my sql file is this:
TABLE movies (
id INTEGER,
title TEXT NOT NULL,
year NUMERIC,
PRIMARY KEY(id)
);
And my code is:
sqlite> CREATE TABLE TITLES(titel)
...> INSERT INTO TITLES(titel)
...> VALUES (SELECT title FROM movies WHERE year = 2008);
Apparently it throws me this error:
Error: near "INSERT": syntax error
How can I solve this? thanks in advance
CREATE TABLE movies (
id INTEGER,
title TEXT NOT NULL,
year NUMERIC,
PRIMARY KEY(id)
);
INSERT INTO MOVIES VALUES (1, 'The Dark Knight' , 2008);
CREATE TABLE TITLES(titel TEXT) ;
INSERT INTO TITLES SELECT title from movies where year =2008;
SQL Fiddle

Why am I getting "Cannot insert the value NULL into column ...." here?

Getting the error
Cannot insert the value NULL into column 'id', table
'master.dbo.Problems'; column does not allow nulls. INSERT fails.
on my table created with
CREATE TABLE Problems (
id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
prompt_code VARCHAR(5000) NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP
);
after I try to run the basic command
INSERT INTO Problems (prompt_code) VALUES ( '<span>Blah blah blah</span>' );
What gives?

Postgres returns SQL state: 22001 when copying data to another table

Searching on google the SQL state: 22001 is because:
the sql statement specifies a
string that is too long
but both tables have the same definition of the column.
this is the table i want to have the data copied:
CREATE TABLE wise_estado
(
id_estado serial NOT NULL,
>> cvgeo_estado character varying(2),<<
nombre_estado character varying
)
this the table with data i want to copy:
CREATE TABLE estados
(
gid serial NOT NULL,
>> "CVE_ENT" character varying(2),<<
"NOM_ENT" character varying(80),
geom geometry(MultiPolygon,4326),
CONSTRAINT estados_pkey PRIMARY KEY (gid)
)
my SQL statement:
INSERT INTO wise_estado ( cvgeo_estado, nombre_estado)
SELECT 'CVE_ENT', 'NOM_ENT'
FROM estados
What i'm missing in my SQL statement?
You are sending string literals instead of field names in your SQL statement. Instead:
INSERT INTO wise_estado ( cvgeo_estado, nombre_estado)
SELECT "CVE_ENT", "NOM_ENT"
FROM estados;
Or, more succintly:
INSERT INTO wise_estado ( cvgeo_estado, nombre_estado)
SELECT CVE_ENT, NOM_ENT
FROM estados

SQL Error about syntax error(POSTGRES)

I always get the error on postgres sql can anyone help me
ERROR: Syntaxerror in „(“ LINE 4: Liga_Nr int(1),
^
********** ERROR **********
ERROR: Syntaxerror in „(“ SQL Status:42601 Symbol:79
Here is my code
DROP TABLE IF EXISTS Liga;
Create Table Liga(
Verband varchar(90),
Liga_Nr int(1),
PRIMARY KEY(Liga_Nr)
);
DROP TABLE IF EXISTS Spiel;
CREATE Table Spiel(
);
DROP TABLE IF EXISTS Verein;
CREATE Table Verein(
);
DROP TABLE IF EXISTS Spieler;
CREATE Table Spieler(
PRIMARY KEY(Spieler_ID)
);
Integer types don't accept a parameter. The correct code is:
Create Table Liga(
Verband varchar(90),
Liga_Nr int,
PRIMARY KEY(Liga_Nr)
);
If you want to store a small number, use smallint. You can read about numeric types here.

Create table script syntax error "table does not exist"

This is my first time producing a script which I can run through the psql terminal using the -f option.
My script is as follows:
DROP TABLE if EXISTS piste;
DROP TABLE if EXISTS lift;
DROP TABLE if EXISTS lift_location;
CREATE TABLE piste (
piste_name varchar(30) PRIMARY KEY NOT NULL,
grade varchar(10) NOT NULL,
length decimal NOT NULL,
fall smallint NOT NULL,
open boolean NOT NULL,
);
INSERT INTO piste (name, grade, length, fall, open) VALUES
('test name', 'easy', 3.2, 400, true);
This produces the following error:
psql:create_tables.sql:22: NOTICE: table "piste" does not exist, skipping
DROP TABLE
psql:create_tables.sql:23: NOTICE: table "lift" does not exist, skipping
DROP TABLE
psql:create_tables.sql:24: NOTICE: table "lift_location" does not exist, skipping
DROP TABLE
psql:create_tables.sql:33: ERROR: syntax error at or near ")"
LINE 8: );
^
psql:create_tables.sql:36: ERROR: relation "piste" does not exist
LINE 1: INSERT INTO piste (name, grade, length, fall, open) VALUES
Does anybody know what is causing this? From what I can see, the table "piste" is created before I try to insert so how can it say it does not exist?
Thanks,
Chris.
This error:
psql:create_tables.sql:33: ERROR: syntax error at or near ")"
LINE 8: );
Tells you that the table is not created.
You have a dangling comma , inside your create table (after the last column). Remove that and you should be fine.
The comma at the end generates the error message:
open boolean NOT NULL,
The other messages are NOTICEs not errors. They just tell you that they didn't drop the table as they did not exist