SlideShare una empresa de Scribd logo
1 de 20
Progamming in SQL CIS-182
Overview Storing SQL Code can be done in text files or objects Scripts are text files independent of any particular database or server .sql files are scripts Objects are stored as part of a database Tend to be part of application “back end” to manage and make use of data
SQL Programming Objects Views are predefined SELECT statements Stored Procedures are instructions about completing a task Triggers are special stored procedures executing automatically if data or an object changes User-defined Functions are instructions used as part of another statements DDL is used to create each object
Views A view is a predefined selected statement Also referred to as a “virtual table” Allows easier access to data Don’t need to remember all of the joins required to put data together Allows data hiding Can limit the rows or columns returned Allows structure hiding Can hide how data is actually stored
Creating a View Define the SELECT statement Add the Data Definition Language statement before the SELECT CREATE VIEW v_TitleWithPublisher AS SELECT t.*, pub_name Publisher FROM titles t JOIN publishers p ON t.pub_id=p.pub_id
Using a View A view is used in place of a table Can apply criteria, grouping, sorting SELECT publisher, title, price FROM v_TitleWithPublisher WHERE price>20 ORDER BY title
View Code Sample
Stored Procedures Stored procedures are similar to procedures in other languages Designed to complete a particular task Add a row Return a value or table Also referred to as “sproc” Use CREATE, ALTER to code  Use Execute (or EXEC) to run
Simple Stored Procedure To get a list of all authors, code a SELECT statement  Add a CREATE statement prior to the SELECT CREATE PROC up_GetAuthors AS SELECT * FROM authors
Sample SPROC Code
Parameters Parameters are used to send data to a procedure  Referred to as arguments in many languages Define a name and data type Name starts with ‘@’ Parameters can be INPUT or OUTPUT Input parameters are values going into the procedure Output parameters are values being used outside of the procedure
Input Parameters Can use value coming into sproc As part of WHERE clause WHERE price>@SearchPrice For value to store in a table UPDATE titles SET price=@NewPrice WHERE title_id= @title_id
Input Parameters -1
Input Parameters - 2 Show data before run sproc Show data after run sproc
Output Parameters Output parameters are used to return a value Direction must be specified when define parameter When use sproc, must also provide a variable to capture value If want average price of all books can use an output parameter
Output Parameter Example
Functions Also known as user-defined functions, or “udf” Functions return a value for use as part of another statement Also referred to as ‘In-Line’ Sprocs execute as single statements Can use input parameters if need to pass in a value Parameters enclosed in parentheses UDF’s don’t have output parameters
Function Values Functions may return different types of results Scalar functions return a single value (string, date, integer Table functions return a table (rows and columns) May be single or multiple statements to generate a table When use, need to qualify with where function is (schema)
Scalar Function
Table Function

Más contenido relacionado

La actualidad más candente

Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, proceduresVaibhav Kathuria
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functionsNitesh Singh
 
Mimsy XG Resource Session
Mimsy XG Resource SessionMimsy XG Resource Session
Mimsy XG Resource SessionAxiell ALM
 
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822
The Ultimate Guide to Oracle solaris 11 advanced system administration  1 z0 822The Ultimate Guide to Oracle solaris 11 advanced system administration  1 z0 822
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822SoniaSrivastva
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)harman kaur
 
Oracle basic queries
Oracle basic queriesOracle basic queries
Oracle basic queriesPRAKHAR JHA
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007paulguerin
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSalman Memon
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle SqlAhmed Yaseen
 
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133
 The Ultimate Guide to Oracle web logic server 12c  administration i  1z0 133 The Ultimate Guide to Oracle web logic server 12c  administration i  1z0 133
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133SoniaSrivastva
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding GuidelinesChris Adkin
 
5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow Basics5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow BasicsPramod Singla
 

La actualidad más candente (20)

SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
CIS 282 Final Review
CIS 282 Final ReviewCIS 282 Final Review
CIS 282 Final Review
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
Sql DML
Sql DMLSql DML
Sql DML
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
Mimsy XG Resource Session
Mimsy XG Resource SessionMimsy XG Resource Session
Mimsy XG Resource Session
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822
The Ultimate Guide to Oracle solaris 11 advanced system administration  1 z0 822The Ultimate Guide to Oracle solaris 11 advanced system administration  1 z0 822
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)
 
Oracle basic queries
Oracle basic queriesOracle basic queries
Oracle basic queries
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data base
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
 
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133
 The Ultimate Guide to Oracle web logic server 12c  administration i  1z0 133 The Ultimate Guide to Oracle web logic server 12c  administration i  1z0 133
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding Guidelines
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow Basics5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow Basics
 
Babitha2.mysql
Babitha2.mysqlBabitha2.mysql
Babitha2.mysql
 

Destacado

CNC Programming with G Code for Beginners
CNC Programming with G Code for BeginnersCNC Programming with G Code for Beginners
CNC Programming with G Code for Beginnersayman diab
 
Cnc 4-g code language -hiast
Cnc 4-g code language -hiastCnc 4-g code language -hiast
Cnc 4-g code language -hiastahmad almaleh
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questionsPyadav010186
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands1keydata
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginnersRam Sagar Mourya
 
Introduction to cnc machines (1)
Introduction to cnc machines (1)Introduction to cnc machines (1)
Introduction to cnc machines (1)someshking
 
Cnc Programming Basics
Cnc Programming BasicsCnc Programming Basics
Cnc Programming Basicsshlxtn
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Beat Signer
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answersvijaybusu
 
CNC PROGRAMMING FOR BEGAINER Part 1
CNC PROGRAMMING FOR BEGAINER Part 1CNC PROGRAMMING FOR BEGAINER Part 1
CNC PROGRAMMING FOR BEGAINER Part 1Parveen Kumar
 
CNC Machines
CNC MachinesCNC Machines
CNC Machinespratik207
 

Destacado (19)

CNC Programming with G Code for Beginners
CNC Programming with G Code for BeginnersCNC Programming with G Code for Beginners
CNC Programming with G Code for Beginners
 
Sql queries
Sql queriesSql queries
Sql queries
 
Cnc 4-g code language -hiast
Cnc 4-g code language -hiastCnc 4-g code language -hiast
Cnc 4-g code language -hiast
 
Dbms viva questions
Dbms viva questionsDbms viva questions
Dbms viva questions
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
 
Dbms lab questions
Dbms lab questionsDbms lab questions
Dbms lab questions
 
DBMS Practical File
DBMS Practical FileDBMS Practical File
DBMS Practical File
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
Introduction to cnc machines (1)
Introduction to cnc machines (1)Introduction to cnc machines (1)
Introduction to cnc machines (1)
 
Cnc Programming Basics
Cnc Programming BasicsCnc Programming Basics
Cnc Programming Basics
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
 
CNC PROGRAMMING FOR BEGAINER Part 1
CNC PROGRAMMING FOR BEGAINER Part 1CNC PROGRAMMING FOR BEGAINER Part 1
CNC PROGRAMMING FOR BEGAINER Part 1
 
CNC Machines
CNC MachinesCNC Machines
CNC Machines
 
part programming (cnc)
part programming (cnc)part programming (cnc)
part programming (cnc)
 

Similar a SQL Programming (20)

Db Triggers05ch
Db Triggers05chDb Triggers05ch
Db Triggers05ch
 
Chapter09
Chapter09Chapter09
Chapter09
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
 
Intro to tsql
Intro to tsqlIntro to tsql
Intro to tsql
 
ASP.Net Presentation Part2
ASP.Net Presentation Part2ASP.Net Presentation Part2
ASP.Net Presentation Part2
 
Dynamic Publishing with Arbortext Data Merge
Dynamic Publishing with Arbortext Data MergeDynamic Publishing with Arbortext Data Merge
Dynamic Publishing with Arbortext Data Merge
 
Module04
Module04Module04
Module04
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).ppt
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Chap 7
Chap 7Chap 7
Chap 7
 
Tech Days09 Sqldev
Tech Days09 SqldevTech Days09 Sqldev
Tech Days09 Sqldev
 
SQL Server 2008 for Developers
SQL Server 2008 for DevelopersSQL Server 2008 for Developers
SQL Server 2008 for Developers
 
SQL Server 2008 for .NET Developers
SQL Server 2008 for .NET DevelopersSQL Server 2008 for .NET Developers
SQL Server 2008 for .NET Developers
 
OBIEE publisher with Report creation - Tutorial
OBIEE publisher with Report creation - TutorialOBIEE publisher with Report creation - Tutorial
OBIEE publisher with Report creation - Tutorial
 
SQL Reporting Services
SQL Reporting ServicesSQL Reporting Services
SQL Reporting Services
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 

Más de Randy Riness @ South Puget Sound Community College

Más de Randy Riness @ South Puget Sound Community College (20)

Stored procedures
Stored proceduresStored procedures
Stored procedures
 
3 sql overview
3 sql overview3 sql overview
3 sql overview
 
Normalization
NormalizationNormalization
Normalization
 
CIS160 final review
CIS160 final reviewCIS160 final review
CIS160 final review
 
SQL Constraints
SQL ConstraintsSQL Constraints
SQL Constraints
 
CIS 245 Final Review
CIS 245 Final ReviewCIS 245 Final Review
CIS 245 Final Review
 
CIS145 Final Review
CIS145 Final ReviewCIS145 Final Review
CIS145 Final Review
 
Cis166 Final Review C#
Cis166 Final Review C#Cis166 Final Review C#
Cis166 Final Review C#
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objects
 
CIS245 sql
CIS245 sqlCIS245 sql
CIS245 sql
 
Cis245 Midterm Review
Cis245 Midterm ReviewCis245 Midterm Review
Cis245 Midterm Review
 
CSS
CSSCSS
CSS
 
XPath
XPathXPath
XPath
 
XSLT Overview
XSLT OverviewXSLT Overview
XSLT Overview
 
Views
ViewsViews
Views
 
CIS282 Midterm review
CIS282 Midterm reviewCIS282 Midterm review
CIS282 Midterm review
 
Schemas 2 - Restricting Values
Schemas 2 - Restricting ValuesSchemas 2 - Restricting Values
Schemas 2 - Restricting Values
 
CIS 145 test 1 review
CIS 145 test 1 reviewCIS 145 test 1 review
CIS 145 test 1 review
 
XML schemas
XML schemasXML schemas
XML schemas
 
Document type definitions part 2
Document type definitions part 2Document type definitions part 2
Document type definitions part 2
 

Último

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 

Último (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

SQL Programming

  • 2. Overview Storing SQL Code can be done in text files or objects Scripts are text files independent of any particular database or server .sql files are scripts Objects are stored as part of a database Tend to be part of application “back end” to manage and make use of data
  • 3. SQL Programming Objects Views are predefined SELECT statements Stored Procedures are instructions about completing a task Triggers are special stored procedures executing automatically if data or an object changes User-defined Functions are instructions used as part of another statements DDL is used to create each object
  • 4. Views A view is a predefined selected statement Also referred to as a “virtual table” Allows easier access to data Don’t need to remember all of the joins required to put data together Allows data hiding Can limit the rows or columns returned Allows structure hiding Can hide how data is actually stored
  • 5. Creating a View Define the SELECT statement Add the Data Definition Language statement before the SELECT CREATE VIEW v_TitleWithPublisher AS SELECT t.*, pub_name Publisher FROM titles t JOIN publishers p ON t.pub_id=p.pub_id
  • 6. Using a View A view is used in place of a table Can apply criteria, grouping, sorting SELECT publisher, title, price FROM v_TitleWithPublisher WHERE price>20 ORDER BY title
  • 8. Stored Procedures Stored procedures are similar to procedures in other languages Designed to complete a particular task Add a row Return a value or table Also referred to as “sproc” Use CREATE, ALTER to code Use Execute (or EXEC) to run
  • 9. Simple Stored Procedure To get a list of all authors, code a SELECT statement Add a CREATE statement prior to the SELECT CREATE PROC up_GetAuthors AS SELECT * FROM authors
  • 11. Parameters Parameters are used to send data to a procedure Referred to as arguments in many languages Define a name and data type Name starts with ‘@’ Parameters can be INPUT or OUTPUT Input parameters are values going into the procedure Output parameters are values being used outside of the procedure
  • 12. Input Parameters Can use value coming into sproc As part of WHERE clause WHERE price>@SearchPrice For value to store in a table UPDATE titles SET price=@NewPrice WHERE title_id= @title_id
  • 14. Input Parameters - 2 Show data before run sproc Show data after run sproc
  • 15. Output Parameters Output parameters are used to return a value Direction must be specified when define parameter When use sproc, must also provide a variable to capture value If want average price of all books can use an output parameter
  • 17. Functions Also known as user-defined functions, or “udf” Functions return a value for use as part of another statement Also referred to as ‘In-Line’ Sprocs execute as single statements Can use input parameters if need to pass in a value Parameters enclosed in parentheses UDF’s don’t have output parameters
  • 18. Function Values Functions may return different types of results Scalar functions return a single value (string, date, integer Table functions return a table (rows and columns) May be single or multiple statements to generate a table When use, need to qualify with where function is (schema)