what does the error "ORA-00933:" mean in my situation - sql

I am just a student and I am trying to get a table working but have tried everything I could only to end up with an error.
ORA-00933: SQL command not properly ended
Could anyone please help me understand what I'm doing wrong?
CREATE TABLE student
(
idnumber VARCHAR2(8),
firstname VARCHAR2(20),
lastname VARCHAR2(20),
dateofbirth DATE,
address VARCHAR2(20),
email VARCHAR2(20),
programme VARCHAR2(5),
points number(3),
PRIMARY KEY (idnumber)
);
INSERT INTO student
VALUES ('D1234567', 'Student ONE', 'TWO THREE', DATE '2000-05-10',
'Thetown, the address', 'd1234567#mydit.ie', 'DT228', '380'),
('D2345678', 'Student TWO', 'FOUR FIVE', DATE '2000-04-10',
'Thetown, the address', 'd2345678#mydit.ie', 'DT228', '280');
SELECT
firstname, lastname
FROM
student
WHERE
points > 300;

That is not valid insert syntax. You can only insert one row at a time that way, separating by comma will not work. To do multi-row inserts, use the syntax:
INSERT ALL
INTO tbl (col1, col2) VALUES ('value', 'another value')
INTO tbl (col1, col2) VALUES ('value', 'another value')
INTO tbl (col1, col2) VALUES ('value', 'another value')
SELECT 1 FROM DUAL;
The select 1 from dual is required for the subquery needing a select statement. It's no different than doing multiple insert statements, so you might as well just break this into multiple statements unless you are doing thousands of inserts at once.

Run each of the statements one by one:
Create your table
CREATE TABLE student
(
idnumber VARCHAR2(8),
firstname VARCHAR2(20),
lastname VARCHAR2(20),
dateofbirth DATE,
address VARCHAR2(20),
email VARCHAR2(20),
programme VARCHAR2(5),
points number(3),
PRIMARY KEY (idnumber)
);
Insert the 1st row
INSERT INTO student VALUES
(
'D1234567',
'Student ONE',
'TWO THREE',
DATE '2000-05-10',
'Thetown,thedress',
'd1234567#mydit.ie',
'DT228',
'380'
);
Insert the 2nd row
INSERT INTO student VALUES
(
'D2345678',
'Student TWO',
'FOUR FIVE',
DATE '2000-04-10',
'Thetown,the adress',
'd2345678#mydit.ie',
'DT228',
'280'
);
Run the select
SELECT firstname, lastname
FROM student
WHERE points > 300;
Examples:
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=d01055a94dce5ed7919a3d26c8a9f73c
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=d01055a94dce5ed7919a3d26c8a9f73c

semicolon (;) is the problem, remove semicolon from last statement and you can get rid of this error, your code's last statement will become
SELECT firstname, lastname
FROM student
WHERE points > 300
Hope this will work

Related

SQL Server : default constraint doesn't work

I have a small problem with my code. I added a default constraint but for some reason it doesn't want to work. My code:
CREATE TABLE oc.students
(
stud_id INT PRIMARY KEY IDENTITY (1,1),
first_name VARCHAR(50) NOT NULL,
mid_name VARCHAR(50),
last_name VARCHAR(50) NOT NULL,
DOB DATE,
email VARCHAR (255) NOT NULL,
country VARCHAR(50) NOT NULL,
phone VARCHAR(20),
reg_date DATE NOT NULL
);
ALTER TABLE oc.students
ADD DEFAULT GETDATE() FOR [reg_date];
INSERT INTO oc.students (first_name, mid_name, last_name, DOB, email, country, phone, reg_date)
VALUES ('John', '', 'Smith', '1986-12-24', 'js#gmail.com', 'Malta', 123456789, '')
SELECT *
FROM oc.students
Result:
What could be the problem?
'' and NULL are not the same (well, in any database other than Oracle). The first is a string that happens to have no characters. The second is a SQL constant value that has the semantics of "unknown value" and is often used for missing values.
You are inserting '' which is interpreted as 0, which gets converted to the base date. Actually, the technical term for this is epoch, which is the date a uses to start counting date/time values.
If you want the default, you can use:
VALUES (
'John',
'',
'Smith',
'1986-12-24',
'js#gmail.com',
'Malta',
123456789,
DEFAULT
)
Or, more commonly, the column is just left out:
INSERT INTO oc.students (
first_name,
mid_name,
last_name,
DOB,
email,
country,
phone)
VALUES (
'John',
'',
'Smith',
'1986-12-24',
'js#gmail.com',
'Malta',
123456789
)

How to insert several different columns from source table to one column in destination table

I have two different tables in my SQL Server database, Table_1 and Table_2.
Table_1 has columns: FirstName, fName, First_Name
Table_2 has one column: FirstName.
I need to transfer these three columns from Table_1 to column FirstName in Table_2. Each row in Table_1 has date in only one of these columns and the other two are nulls. I need to copy only that column that contain data.
Does anyone know how to write stored procedure or query that can do this?
The screenshot is an example of Table_1
Second screenshot shows the output I expect
Use COALESCE(arg1, arg2, arg3, …), which returns the first non-null value among the arguments. Here's an example matching your sample data:
DECLARE #Table1 TABLE (FirstName VARCHAR(25), fName VARCHAR(25), First_Name VARCHAR(25))
DECLARE #Table2 TABLE (FirstName VARCHAR(25))
INSERT INTO #Table1 VALUES ('Sanja', null, null), ('Sanja', null, null),
('Sanja', null, null), (null, null, 'Nick'), (null, 'Bob', null)
INSERT INTO #Table2
SELECT COALESCE(FirstName, fName, First_Name) FROM #Table1
SELECT * FROM #Table2
FirstName
Sanja
Sanja
Sanja
Nick
Bob

"Invalid Column Name" even though the column is created?

I have created a table :
create table Praktikant(
Pr_id char not null Primary key,
Name varchar(100) not null,
Mbiemri varchar(100) not null,
Gender char(1) not null,
Degree varchar(100) not null,
age int,
check (age >18),
foreign key (Pr_id) references Doctor(Dr_id) on update cascade
)
Inserts:
insert into Praktikant values ('1', 'Kastriot', 'Tusha', 'M', 'Student', 19);
insert into Praktikant values ('2', 'Trim', 'Dalipi', 'M', 'Student', 23);
insert into Praktikant values ('3', 'Elira', 'Rrmoku', 'F', 'Studente', 24);
insert into Praktikant values ('4', 'Qendresa', 'Krasniqi', 'F', 'Studente',20);
Query:
select age
from Praktikant
where age > 23
I don't understand why I get this message:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'age'.
Use this query to get table info:
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'Praktikant'
then find your table schema because i suspect that you have two tables with the same name in another schema.
after find schema use this query for select info:
select age
from [schema_name].[Praktikant]
where age > 23
You can use this query to get table columns list to check:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Praktikant'

Importing into Gender Field in SQL/SSIS

How would you handle importing only male (m) or female (f) data into a gender field, so that it won't get populated with any potential extraneous source data, in SQL or SSIS?
You can create a column constraint:
CREATE TABLE Persons
(
Id int NOT NULL,
LastName varchar(255),
FirstName varchar(255),
Gender varchar(1),
CONSTRAINT chk_Gender CHECK (Gender ='m' or Gender='f')
)
Insert into Persons values(1,'smith', 'jane', 'f');
insert into Persons values(2,'smith', 'john', SUBSTRING('male', 1, 1));
/* this will fail
INSERT INTO Persons values (3,'foo', 'bar', 'u');
*/
select * FROM Persons;
Working Fiddle
It's trival to add a check constraint, something like:
CREATE TABLE #Test (
GENDER CHAR(1) CONSTRAINT Constraint_Gender CHECK (GENDER IN ('A','B','C'))
)
INSERT INTO #Test VALUES ('A')
INSERT INTO #Test VALUES ('D')
(1 row(s) affected)
Msg 547, Level 16, State 0, Line 9
The INSERT statement conflicted with the CHECK constraint "Constraint_Gender".
However, you should really cater for non-binary genders in a similar manner to facebook etc https://www.facebook.com/facebookdiversity/posts/774221582674346 (hence A, B, C in my example above)

Insert values of one table in a database to another table in another database

I would like to take some data from a table from DB1 and insert some of that data to a table in DB2.
How would one proceed to do this?
This is what I've got so far:
CREATE VIEW old_study AS
SELECT *
FROM dblink('dbname=mydb', 'select name,begins,ends from study')
AS t1(name varchar(50), register_start date, register_end date);
/*old_study now contains the data I wanna transfer*/
INSERT INTO studies VALUES (nextval('studiesSequence'),name, '',3, 0, register_start, register_end)
SELECT name, register_start, register_end from old_study;
This is how my table in DB2 looks:
CREATE TABLE studies(
id int8 PRIMARY KEY NOT NULL,
name_string VARCHAR(255) NOT NULL,
description VARCHAR(255),
field int8 REFERENCES options_table(id) NOT NULL,
is_active INTEGER NOT NULL,
register_start DATE NOT NULL,
register_end DATE NOT NULL
);
You should include the column names in both the insert and select:
insert into vip_employees(name, age, occupation)
select name, age, occupation
from employees;
However, your data structure is suspect. Either you should use a flag in employees to identify the "VIP employees". Or you should have a primary key in employees and use this primary key in vip_employees to refer to employees. Copying over the data fields is rarely the right thing to do, especially for columns such as age which are going to change over time. Speaking of that, you normally derive age from the date of birth, rather than storing it directly in a table.
INSERT INTO studies
(
id
,name_string
,description
,field
,is_active
,register_start
,register_end
)
SELECT nextval('studiesSequence')
,NAME
,''
,3
,0
,register_start
,register_end
FROM dblink('dbname=mydb', 'select name,begins,ends from study')
AS t1(NAME VARCHAR(50), register_start DATE, register_end DATE);
You can directly insert values that retured by dblink()(that means no need to create a view)
Loop and cursor are weapons of last resort. Try to avoid them. You probably want INSERT INTO ... SELECT:
INSERT INTO x(x, y, z)
SELECT x, y, z
FROM t;
SqlFiddleDemo
EDIT:
INSERT INTO vip_employees(name, age, occupation) -- your column list may vary
SELECT name, age, occupation
FROM employees;
Your syntax is wrong. You cannot have both, a values clause for constant values and a select clause for a query in your INSERT statement.
You'd have to select constant values in your query:
insert into studies
(
id,
name_string,
description,
field,
is_active,
register_start,
register_end
)
select
studiesSequence.nextval,
name,
'Test',
null,
0,
register_start,
register_end
from old_study;