SlideShare uma empresa 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

Mais conteúdo relacionado

Mais procurados

Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)
harman kaur
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
paulguerin
 

Mais procurados (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
 

Destaque

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
ayman diab
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
Pyadav010186
 
Cnc Programming Basics
Cnc Programming BasicsCnc Programming Basics
Cnc Programming Basics
shlxtn
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
vijaybusu
 

Destaque (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)
 

Semelhante a SQL Programming

Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
Syed Asrarali
 
ASP.Net Presentation Part2
ASP.Net Presentation Part2ASP.Net Presentation Part2
ASP.Net Presentation Part2
Neeraj Mathur
 
SQL Reporting Services
SQL Reporting ServicesSQL Reporting Services
SQL Reporting Services
neha mittal
 

Semelhante 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
 

Mais de Randy Riness @ South Puget Sound Community College

Mais 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

Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Último (20)

Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
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
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.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
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

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)