If I have a PL SQL procedure like this:
Create Or Replace Procedure get_age (first_name varchar(40), last_name varchar(50))
Begin
Select age
From Person
Where first = first_name AND last = last_name;
End;
It is not guaranteed that the user will pass in a value for the first_name and last_name variable.
How do I account for this in the Procedure above since I do not want the first_name or last_name in the Where clause of my query if either one of those variables do not have a value.
Your query is good as it is now, you can accept null values in your WHERE clause:
Where (first = first_name OR first_name is NULL) AND (last = last_name OR last_name in NULL);
This way the user can enter first and last names, first or last names only or neither and results will be selected as expected.
Create Or Replace Procedure get_age (first_name varchar(40) default null, last_name varchar(50) default null)
Begin
Select age From Person Where first = nvl(first_name,first) AND last = nvl(last_name,last);
End;
Related
I have an employee table of following schema:
CREATE TABLE Employee
(
Id INT IDENTITY(1,1) PRIMARY KEY,
Email VARCHAR(35) UNIQUE,
Phone VARCHAR(50) UNIQUE
city FOREIGN KEY REFERENCES City(Id)
)
Before inserting data into the table, I want to validate my data using a stored procedure.
The stored procedure needs to accept many rows and return data along with errors. So, I created a user-defined table type tblTypeEmployee which I will pass to stored procedure.
CREATE TYPE tblTypeEmployee AS TABLE
(
Id INT,
Email VARCHAR(35),
Phone VARCHAR(50),
City VARCHAR(50)
)
My stored procedure:
CREATE PROCEDURE spBulkImportEmployee
(#tblEmployeeTableType [dbo].tblTypeEmployee READONLY)
AS
BEGIN
DECLARE #EmployeeErrors AS tblTypeEmployee;
INSERT INTO #EmployeeErrors (email, phone, city)
SELECT
CASE
WHEN (SELECT COUNT(id) FROM Employee
WHERE email = email) > 0
THEN 'Email already exist'
END AS email,
CASE
WHEN (SELECT COUNT(id) FROM Employee
WHERE phone = phone) > 0
THEN 'Phone no. already exist'
END as phone,
CASE
WHEN (SELECT COUNT(id) FROM City
WHERE cityName = city) < 1
THEN 'Invalid city name'
END as city,
FROM
#tblEmployeeTableType
SELECT * FROM #EmployeeErrors;
END
This procedure gives me all the validation errors. But I want data along with errors. So, that I can display that message in a table in the frontend. For example: If a user gives email as abc#gmail.com and phone no. as 123456789, then I should get abc#gmail.com - already exist, 123456789 - already exist.
How to approach this problem?
below is my requirement
i need to push second_name values to first_name column only when first_name columns are null.
for example,
i want drake and jim to be moved to first_name column and should not be present in second_name column.Rest of the names should be same as it is.
Looks like
update your_table set
first_name = second_name,
second_name = null
where first_name is null;
Trying to run a update script on a table, but getting an error:
ERROR: column "ok" does not exist
LINE 2: SET first_name="ok", last_name="pk", email="ooo", phone="...
CREATE TABLE employee (
employee_id SERIAL PRIMARY KEY,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL,
email varchar(255) NOT NULL,
phone varchar(255)
);
INSERT INTO employee(
first_name, last_name, email, phone)
VALUES ('Kyle', 'Belanger', 'kbelanger#ok.com', '(240) 298-4664');
UPDATE "employee"
SET first_name="ok", last_name="pk", email="ooo", phone="000"
WHERE employee_id = 1;
There is no need to wrap table name in double quote "employee", and use single quotes for column values
UPDATE employee
SET first_name='ok', last_name='pk', email='ooo', phone='000'
WHERE employee_id = 1;
See Working Example
Try below sql:
UPDATE employee
SET first_name='ok', last_name='pk', email='ooo', phone='000'
WHERE employee_id = 1;
Table name was wrapped in double quotes which is not allowed.
I'm in the process of migrating from MS SQL Server to Oracle and I'm struggling with a simple table-valued function:
CREATE TABLE Department (
Id NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
Name VARCHAR(64) NOT NULL,
PRIMARY KEY (Id)
);
/
CREATE TABLE Employee (
Id NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
PRIMARY KEY (Id),
DepartmentId INT NOT NULL REFERENCES Department(Id),
FirstName VARCHAR(64) NOT NULL,
LastName VARCHAR(64) NOT NULL,
Email VARCHAR(64) NULL
);
/
CREATE TYPE AType AS OBJECT (Department VARCHAR(64), Employee VARCHAR(64));
/
CREATE TYPE ATypeCol AS TABLE OF AType;
/
CREATE OR REPLACE FUNCTION fEmployee(dep IN VARCHAR(64))
RETURN ATypeCol PIPELINED IS
BEGIN
FOR i IN (SELECT Department.Name, Employee.FirstName || ' ' || Employee.LastName
FROM Employee JOIN Department ON DepartmentId = Department.Id
WHERE Department.Name = dep) LOOP
PIPE ROW(AType(i.Department, i.Employee));
END LOOP;
RETURN;
END;
/
SELECT * FROM TABLE(fEmployee('IT'));
/
However it fails with
FUNCTION FEMPLOYEE compiled
Errors: check compiler log
Error starting at line 94 in command:
SELECT * FROM TABLE(fEmployee('IT'))
Error at Command Line:94 Column:21
Error report:
SQL Error: ORA-06575: Package or function FEMPLOYEE is in an invalid state
06575. 00000 - "Package or function %s is in an invalid state"
*Cause: A SQL statement references a PL/SQL function that is in an
invalid state. Oracle attempted to compile the function, but
detected errors.
*Action: Check the SQL statement and the PL/SQL function for syntax
errors or incorrectly assigned, or missing, privileges for a
referenced object.
Any help is appreciated.
CREATE OR REPLACE FUNCTION fEmployee (
dep IN VARCHAR
)
RETURN ATypeCol PIPELINED IS
BEGIN
FOR i IN (SELECT Department.Name, Employee.FirstName || ' ' || Employee.LastName Employee
FROM Employee JOIN Department ON DepartmentId = Department.Id
WHERE Department.Name = dep) LOOP
PIPE ROW(AType(i.Name, i.Employee));
END LOOP;
RETURN;
END;
Formal parameter must not have scale/precision. Also, you should specify correct aliases in cursor.
Please use SELECT * FROM USER_ERRORS WHERE NAME = ''
or "show error" SQL*Plus command to get the information about compilliation errors.
Here is the base table layout:
create table employees (employeeid int not null IDENTITY,
firstname varchar(50), middlename varchar(50), lastname varchar(50),
assumedfirstname default(firstname), assumedname as concat(assumedfirstname,' ',lastname)
I understand that the assumedfirstname column is not being created correctly in the above statement; that default values must be scalar expressions and cannot be column names. That said, the above statement clearly illustrates my intent. That is, I wish for the assumedfirstname column to automatically be populated with the value found in firstname but allow explicit replacement with a separate string later. In this way, assumedname will always represent either a default of the person's first and last names or an explicitly entered assumedfirstname and their last name.
As such, a computed column will not work in this situation.
You can't set it as default, but you can simulate it with a trigger;
CREATE TABLE employees (
employeeid INT NOT NULL IDENTITY,
firstname VARCHAR(50),
middlename VARCHAR(50),
lastname VARCHAR(50),
assumedfirstname VARCHAR(50),
assumedname AS assumedfirstname + ' ' + lastname
);
CREATE TRIGGER MyTrigger on employees
FOR UPDATE, INSERT AS
UPDATE e
SET e.assumedfirstname = COALESCE(e.assumedfirstname, i.firstname)
FROM employees e
JOIN inserted i
ON i.employeeid=e.employeeid;
This trigger will update assumedfirstname to the value of firstname if it's set to null (ie is unset).
An SQLfiddle to test with.