0% found this document useful (0 votes)
7 views6 pages

Procedures in MySql

A stored procedure is a reusable SQL code that can accept parameters and be executed multiple times. It allows for efficient querying by saving frequently used SQL statements. Additionally, the document explains the syntax for creating stored procedures and functions, along with examples of their usage in SQL queries.

Uploaded by

verginjose12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Procedures in MySql

A stored procedure is a reusable SQL code that can accept parameters and be executed multiple times. It allows for efficient querying by saving frequently used SQL statements. Additionally, the document explains the syntax for creating stored procedures and functions, along with examples of their usage in SQL queries.

Uploaded by

verginjose12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

What is a Stored Procedure?

A stored procedure is a prepared SQL code that you can save, so


the code can be reused over and over again.

So if you have an SQL query that you write over and over again, save it as
a stored procedure, and then just call it to execute it.

You can also pass parameters to a stored procedure, so that the


stored procedure can act based on the parameter value(s) that is
passed.

Stored Procedure Syntax


CREATE PROCEDURE procedure_name
AS
sql_statement
GO;
Execute a Stored Procedure
EXEC procedure_name;

Example -1
CREATE PROCEDURE SelectAllCustomers
AS
SELECT * FROM Customers
GO;

Execute the stored procedure above as follows:

Example
EXEC SelectAllCustomers;

Stored Procedure With One Parameter

The following SQL statement creates a stored procedure that selects


Customers from a particular City from the "Customers" table:
Example -2
CREATE PROCEDURE SelectAllCustomers @City nvarchar(30)
AS
SELECT * FROM Customers WHERE City = @City
GO;

Execute the stored procedure above as follows:

Example
EXEC SelectAllCustomers @City = 'Trichy';
Stored Procedure With Multiple Parameters

Setting up multiple parameters is very easy. Just list each parameter and
the data type separated by a comma as shown below.

The following SQL statement creates a stored procedure that selects


Customers from a particular City with a particular PostalCode from the
"Customers" table:

Example- 3
CREATE PROCEDURE SelectAllCustomers @City nvarchar(30),
@PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode =
@PostalCode
GO;

Execute the stored procedure above as follows:

Example
EXEC SelectAllCustomers @City = 'Trichy', @PostalCode = 620015';

FUNCTION
The CREATE FUNCTION statement is used for creating a stored function
and user-defined functions. A stored function is a set of SQL statements
that perform some operation and return a single value.

CREATE FUNCTION function_name(func_parameter1, func_parameter2, ..)


RETURN datatype [characteristics]
func_body
Parameters used:

1. function_name:
It is the name by which stored function is called. The name should not
be same as native(built_in) function. In order to associate routine
explicitly with a specific database function name should be given
as database_name.func_name.

2. func_parameter:
It is the argument whose value is used by the function inside its body.
You can’t specify to these parameters IN, OUT, INOUT. The
parameter declaration inside parenthesis is provided
as func_parameter type. Here, type represents a valid Mysql
datatype.

3. datatype:
It is datatype of value returned by function.

4. characteristics:
The CREATE FUNCTION statement is accepted only if at least one of
the characterisitics { DETERMINISTIC, NO SQL, or READS SQL DATA }
is specified in its declaration.

func_body is the set of Mysql statements that perform operation.


It’s structure is as follows:
BEGIN

Mysql Statements

RETURN expression;
END
The function body must contain one RETURN statement.

Example:
Consider following Employee Table-

EMP_ID FNAME LNAME START_DATE

1 Michae Smith 2001-06-22


EMP_ID FNAME LNAME START_DATE

2 Susan Barker 2002-09-12

3 Robert Tvler 2000-02-09

Hawthorn

4 Susan e 2002-04-24

We have to find the number of years the employee has been in the
company-
DELIMITER //

CREATE FUNCTION no_of_years(date1 date) RETURNS int DETERMINISTIC


BEGIN
DECLARE date2 DATE;
Select current_date()into date2;
RETURN year(date2)-year(date1);
END

//

DELIMITER ;
Calling of above function:
Select emp_id, fname, lname, no_of_years(start_date) as 'years' from
employee;
Output:
EMP_ID FNAME LNAME YEARS

1 Michael Smith 18
EMP_ID FNAME LNAME YEARS

2 Susan Barker 17

3 Robert Tvler 19

4 Susan Hawthorne 17

All modern operating systems and development platforms use Unicode


internally. By using nvarchar rather than varchar, you can avoid doing encoding
conversions every time you read from or write to the database. Conversions take
time, and are prone to errors. And recovery from conversion errors is a non-
trivial problem.

CREATE PROCEDURE ChangeCity @Name nvarchar(30) @NewCity nvarchar(30)


AS
UPDATE Customers
SET City = @NewCity
WHERE Name = @Name
Go;
Exporting Data with the SELECT ... INTO OUTFILE Statement
The syntax for this statement combines a regular SELECT command
with INTO OUTFILE filename at the end. The default output format is the
same as it is for the LOAD DATA command. So, the following statement
exports the tutorials_tbl table into /tmp/tutorials.txt as a tab-delimited,
linefeed-terminated file.
mysql> SELECT * FROM tutorials_tbl
-> INTO OUTFILE '/tmp/tutorials.txt';
You can change the output format using various options to indicate how to
quote and delimit columns and records. To export the tutorial_tbl table in a
CSV format with CRLF-terminated lines, use the following code.
mysql> SELECT * FROM passwd INTO OUTFILE '/tmp/tutorials.txt'
-> FIELDS TERMINATED BY ',' ENCLOSED BY '"'
-> LINES TERMINATED BY '\r\n';

You might also like