How to format text data with Azure SQL - sql

I just began using Azure SQL Query Editor to help me learn.
I have problems formatting TEXT data when inserting data. I know I should be formatting data with "Quotations" around TEXT data, however, I continue to run into the following error: Incorrect syntax near '“'.
Example of table and dummy data:
CREATE TABLE EMPLOYEE ( eno INTEGER(11), ename TEXT(30), zip INTEGER(5) DEFAULT 47405, hdate DATE, dept TEXT (30), salary INTEGER(15,3), constraint EMPLOYEE_PK primary key (eno));
INSERT INTO EMPLOYEE VALUES (5476343153, “Stanley Keller”, , 06-Mar-99, “Sales”, 75689);
INSERT INTO EMPLOYEE VALUES (5286469147, “Sergio Murray”, , 13-Mar-92, “Marketing”, 148769);
INSERT INTO EMPLOYEE VALUES (2454152346, “Laurie Hawkins”, , 22-Mar-98, “Marketing”, 92474);
Why is this happening to me? Thank you.

Gordon is correct - single identifiers are the norm for ANSI SQL.
In SQL Server/SQL Azure, if you want to use a different quoted identifier, there is a session option to allow it if needed.
Here is the documentation on that feature which should make your original query work (non-standard SQL):
https://learn.microsoft.com/en-us/sql/t-sql/statements/set-quoted-identifier-transact-sql?view=sql-server-2017
SET QUOTED_IDENTIFIER ON

Please try the following. It worked for me.
DROP TABLE EMPLOYEE
GO
CREATE TABLE EMPLOYEE ( eno BigInt, ename NVARCHAR(30), zip INT DEFAULT 47405, hdate DATE, dept NVARCHAR(30), salary decimal(15,3), constraint EMPLOYEE_PK primary key (eno));
GO
INSERT INTO EMPLOYEE (eno, ename, hdate, dept, salary) VALUES (5476343153, 'Stanley Keller', '06-Mar-99', 'Sales', 75689);
INSERT INTO EMPLOYEE (eno, ename, hdate, dept, salary) VALUES (5286469147, 'Sergio Murray', '13-Mar-92', 'Marketing', 148769);
INSERT INTO EMPLOYEE (eno, ename, hdate, dept, salary) VALUES (2454152346, 'Laurie Hawkins', '22-Mar-98', 'Marketing', 92474);
Please take note the data types were wrong, You were treating integer like decimal and for integers the data type is int. Also you don't have to specify commas for the fields where you want the default value to take place. On the eno field I had to specify BigInt as data type since the values on the INSERT statements are to big for the integer (int) data type.
I changed double quotes for single quotes on those INSERT statements.

Related

Trigger ON Table which fire INSERT into another table which has NOT NULL constraint

CREATE TRIGGER logaction ON temployeelog
AFTER INSERT
AS
BEGIN
INSERT INTO TABLE temployee(ename, experience)
SELECT ename,experience FROM INSERTED
END
The structure of temployee
CREATE TABLE temployee
(
ename VARCHAR(20),
experience INT NOT NULL
)
ALTER TABLE temployeeADD DEFAULT (0) FOR experience
When I don't pass data in the experience column WHILE INSERT I get an error.
Cannot insert the value NULL into column 'experience', table
'temployee'; column does not allow nulls. INSERT fails. The statement
has been terminated.
I wanted to pass NULL Values temployeelog table AND wanted those situation to be handled by 'DEFAULT VALUES kept in temployee'
How can I achieve that?
The table default only comes into play if you don't insert it, so split the insert into one which handles a non-null experience and one which handles a null experience
INSERT INTO TABLE temployee (ename, experience)
SELECT ename, experience
FROM INSERTED
WHERE experience IS NOT NULL;
INSERT INTO TABLE temployee (ename)
SELECT ename
FROM INSERTED
WHERE experience IS NULL;

sql query ORA-01843

I have the following DDL and DML statements :
create table emp_details (
ID number(2) constraint t_pk primary key,
F_Name varchar(10) not null,
L_Name varchar(10) not null,
DOB date,
Mob_no number(10),
City varchar(10),
PIN number(5),
Gender char(1),
Designation varchar(15),
Join_Date date,
);
insert into emp_details values (01,'John','Wick','1990-07-05',9856482358,'Goa',403001,'M','SDE II', '2015-01-08');
then, I get the error of ORA-01843. So, what could be the problem?
The easiest thing to do here is to use ANSI date literals instead of strings for the dates (using strings will depend on the value of NLS_DATE_FORMAT and you don't want to play around with that if you don't have to):
INSERT INTO emp_details
VALUES
( 01, 'John', 'Wick', DATE'1990-07-05', 9856482358
, 'Goa', 403001, 'M', 'SDE II', DATE'2015-01-08');
I have to add that explicitly listing the columns into which you're inserting values is a good habit to have. Otherwise, your INSERT query will break if you or someone else adds a column from your table:
INSERT INTO emp_details
( id, f_name, l_name, dob, mob_no, city, pin, gender, designation, join_date )
VALUES
( 01, 'John', 'Wick', DATE'1990-07-05', 9856482358
, 'Goa', 403001, 'M', 'SDE II', DATE'2015-01-08');
Last, another good practice is to use VARCHAR2 instead of VARCHAR when you're working with Oracle. Currently they work the same, but Oracle "reserves the right" to change VARCHAR to meet with the ANSI standard under which NULL values and the empty string won't be the same (with VARCHAR2 they will always be the same). IOW, the behavior of VARCHAR values in Oracle can change.
It seems when you query with
select * from nls_session_parameters p where p.parameter = 'NLS_DATE_FORMAT';
you won't get YYYY-MM-DD or YYYY-DD-MM from your result of error ORA-01843.
This problem is due to inserting wrong-formatted value for date columns DOB and Join_date.
There may be two ways to prevent this error :
Assume you get DD/MM/YYYY from above query, then use 05-07-1990 for
DOB, and 08/01/2015 for Join_Date columns, respectively.
Format your values as to_date('1990-07-05','YYYY-MM-DD') for
DOB and to_date('2015-01-08','YYYY-MM-DD') for Join_Date

Inconsistent datatypes: expected REF got .. error when trying to insert data

Long story short what I'm trying to do is to insert some data into a table and I'm having some problems understanding what is wrong. I'll let the code talk for itself. Hope you can help me out. Thanks in advance!
CREATE OR REPLACE TYPE Departament IS OBJECT (
deptno NUMBER(2),
dname CHAR(14)
);
/
CREATE OR REPLACE TYPE Employee IS OBJECT (
empno NUMBER(4),
ename CHAR(10),
dept REF Departament,
sal NUMBER(7,2)
) NOT FINAL;
/
CREATE OR REPLACE TYPE Manager UNDER Employee (
nrEmp NUMBER(2)
);
/
CREATE TABLE departament_list AS (SELECT deptno, dname FROM dept);
/
CREATE TABLE manager_list OF Manager;
/
INSERT INTO manager_list VALUES(Manager(7782, 'JOHN', Departament(20, 'TEXAS'), 6000, 2));
Well here is the problem on the last line I get the following error
ORA-00932: inconsistent datatypes: expected REF SYS.DEPARTAMENT got
SYS.DEPARTAMENT.
Now don't get me wrong I have tried doing the whole select thingy with: REF(d) from departament_list d ... but I get another error saying incorrect number of arguments for default constructor.
Maybe I'm doing this the wrong way and you can help shed some light on where I'm mistaken. Thanks again!
This seems to be the error reason:
CREATE TABLE departament_list AS (SELECT deptno, dname FROM dept);
Steps which worked for me:
CREATE TABLE departament_list of Departament;
insert into departament_list values (20, 'Texas');
CREATE TABLE manager_list OF Manager;
INSERT INTO manager_list VALUES(Manager(7782, 'JOHN',
(select ref(d) from departament_list d where deptno=20), 6000, 2));
From documentation:
The OF clause lets you explicitly create an object table of type
object_type. The columns of an object table correspond to the
top-level attributes of type object_type. Each row will contain an
object instance, and each instance will be assigned a unique,
system-generated object identifier when a row is inserted.
datatypes are inconsistent, just modify Employee object definition as follows:
CREATE OR REPLACE TYPE Employee IS OBJECT (
empno NUMBER(4),
ename CHAR(10),
dept Departament,
sal NUMBER(7,2)
) NOT FINAL;
/
And then run insert statement:
SQL> INSERT INTO manager_list VALUES(Manager(7782, 'JOHN', Departament(20, 'TEXAS'), 6000, 2));
1 row created.
SQL>
Note: in order to update employee object you will need to drop "manager_list" (table) and drop "manager" (type), then update employee object definition and then create "manager_list" and "manager" again.

ORA-01858: a non-numeric character was found where a numeric was expected? Even when the values are numbers?

This is the table
CREATE TABLE Employee
(EmpID number(5) primary key,
SIN Number(9) Not null,
LastName Varchar2(25) Not null,
FirstName Varchar2(25),
Street Varchar2(30),
City Varchar2(25),
Province Char(2),
PostalCode Varchar2(7),
JobCode Number(4) Not null,
Foreign Key(JobCode) REFERENCES Job,
IncomeTax Char(1),
BirthDate Date,
HireDate Date,
JobCodeDate Date)
TABLESPACE users;
This is the line I am trying to insert, there is only three numeric values and all of them are numbers as far as I can see.
INSERT INTO Employee VALUES(97319,516303417,'Novak','Gerry','6803 Park Ave.','Moose Jaw','SK','S6H 1X7',3000,'N','24-Aug-86','07-Jul-03','07-Jul-03');
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
I believe the issue is with the date columns try using this syntax to_date('07-Jul-03','DD-MON-YY'):
INSERT INTO Employee
VALUES(97319,516303417,'Novak','Gerry','6803 Park Ave.','Moose Jaw','SK','S6H 1X7',3000,'N',to_date('24-Aug-86', 'DD-MON-YY'),to_date('07-Jul-03','DD-MON-YY'),to_date('07-Jul-03','DD-MON-YY'));
SQL-Fiddle: http://sqlfiddle.com/#!4/0e9df/2
alter SESSION set NLS_DATE_FORMAT = 'DD-Mon-YY';
I just had to type this in so that sql will execute the date format in my insert query's correctly
There's possibly a discrepancy between the order of fields as laid out in the INSERT statement, and the order that Oracle is expecting them. I suggest trying again using the full syntax of the INSERT (i.e. specify field names when doing the INSERT). That way, it's absolutely clear the value to field correlation being made.
So something like this:
INSERT
INTO Employee (EmpID, SIN, LastName, FirstName, Street, City, Province, PostalCode, JobCode, IncomeTax, BirthDate, HireDate, JobCodeDate)
VALUES(97319,516303417,'Novak','Gerry','6803 Park Ave.','Moose Jaw','SK','S6H 1X7',3000,'N','1986-08-24','2003-07-07','2003-07-07');

oracle error: not enough values

i have a table donor_master:
create table donor_master
(
donor_id number(10) primary key not null,
dob date not null,
age number(3) not null,
gender char(1) not null,
blood_group char(3),
contact_no number(10),
address varchar(50) not null,
city varchar(10) not null,
pin number(10) not null,
state varchar(10) not null,
branch_registration_id number(5) references branch_master(branch_id)
);
when i try to insert into the table in a procedure insert_donor_master, i get "not enough values" error on compilation.
this is the procedure:
create or replace procedure insert_donor_master(
vdob donor_master.dob%type,
vage donor_master.age%type,
vgender donor_master.gender%type,
vblood_group donor_master.blood_group%type,
vcontact_no donor_master.contact_no%type,
vaddress donor_master.address%type,
vcity donor_master.city%type,
vpin donor_master.pin%type,
vstate donor_master.state%type,
vbranch_registration_id donor_master.branch_registration_id%type
)
is
begin
insert into donor_master values (sq_donor_master.nextval, vdob, vage, vgender, vblood_group, vcontact_no, vaddress, vcity, vpin, vstate, vbranch_registration_id);
commit;
end;
What is the problem?
Thanks.
Oracle hurls ORA-00947 when we specify an INSERT statement which doesn't have a value for every column in the table.
Now, the CREATE TABLE statement you posted shows a table with eleven columns. And the stored procedure code you posted shows an insert statement with eleven values in the VALUES (...) clause.
So, the explanations are:
you have a configuration management issue, and you're running the wrong version of the stored procedure or the wrong version of the table
you have a configuration management issue, and the actual structure of the table isn't what you think it is (doesn't match your CREATE TABLE script)
you aren't really getting an ORA-00947 error
Note that if you don't want to populate every row you can specify a projection of the relevant columns before the VALUES clause. For instance, if you just wanted to populate the mandatory columns you would code this:
insert into donor_master
(donor_id, dob, age, gender, address, city, pin, state )
values (sq_donor_master.nextval, vdob, vage, vgender, vaddress, vcity, vpin, vstate)
All that matters is that the number of values matches the number of columns.
The complete syntax for INSERT statements is in the documentation. enter link description hereFind out more.