SQL Server Administration Class 4 of 4 Activant Prophet 21 Basic Data Manipulation
This class is designed for Beginner SQL/Prophet21 users who are responsible for SQL Administration as it relates to Prophet 21
Objectives System stored procedures System functions SQL statements Retrieving data through the SELECT statement Inserting data through the INSERT statement Updating data through the UPDATE statement
This course will NOT cover Windows Navigation Installation of SQL Server SQL Server Data Storage Performance Tuning
Using System Stored Procedures System stored procedure overview Common system stored procedures Using system stored procedures to retrieve information
System Stored Procedure Compiled set of Transact-SQL statements used for data retrieval or manipulation The name of the procedure usually begins with sp You can run them from any database using the EXEC statement in a query window They are useful for retrieving information about objects in the database
Common System Stored Procedures Syntax Sp_help {object name} Sp_helptext {object_name} Result List of all objects in the current database with their object types Actual Transact-SQL code that is executed when a stored proc,trigger or view is run
Using System Stored Procedures to Retrieve Information Command EXEC sp_help oe_line EXEC sp_help EXEC sp_helptext myproc Results stored procedure information about the table called oe_line list of all the objects in the current database code that is in a stored procedure called myproc
Understanding System Functions System function overview System functions vs. system stored procedures
System Function Compiled procedures provided by SQL Server to perform complex mathematical calculations, or to manipulate data Rowset Functions Aggregate Functions Scalar Functions
Aggregate Functions SUM AVG MIN MAX COUNT Sums a column of values and returns the result Averages a set of values and returns the results Returns the minimum value from a list of values Returns the maximum value from a list of values Return the count of values in a list
Aggregate Functions To return the average value of the extended price column in the oe_line table SELECT AVG(extended_price) FROM oe_line To return the number of invoices in the inv_hdr table which have an invoice_amount above $1000.00 SELECT count(invoice_no) FROM invoice_hdr WHERE total_amount > 1000
Scalar Functions Getdate() Substring Convert Returns the current system date Used to take only a certain part of a longer string Used to convert data from one data type to another SELECT substring(customer_name, 1, 5) FROM customer
System Functions vs. System Stored Procedures A system function is a compiled function Resides in the SQL Server program itself Does not reside in any database but rather in the shared dynamic link library files that are installed with SQL Server Used in queries to: retrieve summary data from a list of values, perform complex mathematics, or convert data
System Functions vs. System Stored Procedures A system stored procedure is a collection of Transact-SQL statements Returns information about database objects or the current environment Reside in the master database and be used in any database on the server The most common system stored procedures are sp_help and sp_helptext
SELECT Statement Overview Choosing columns Choosing rows Sorting results Grouping Results Implementing Joins
Basic Syntax SELECT column_list Column_list is a commaseparated list of the columns you want in your result set FROM table WHERE where_clause Table which the columns in the select list or the where clause reside. Additional tables can be joined to the FROM Where_clause specifies the criteria for the rows you want to see
Choosing Columns SELECT * FROM oe_hdr SELECT order_no, ship2_name FROM oe_hdr SELECT order_no, 'My Literal', ship2_name FROM oe_hdr SELECT 'Order Number' = order_no, 'Ship To Name' = ship2_name FROM oe_hdr
Choosing Rows Comparison Operators Ranges Lists String Matches =, >, <, >=, <=, <>,!=,!<,!> BETWEEN, NOT BETWEEN IN, NOT IN LIKE, NOT LIKE Unknown Values IS NULL, IS NOT NULL Combinations Negations AND, OR NOT
Choosing Rows SELECT * from oe_hdr where order_no = 10000 SELECT * from oe_hdr where ship2_add1 = 1234 Any Street Put a single quote mark around all literal constants for the columns of char, varchar, text, datetime, and smalldatetime data types
Choosing Rows SELECT * from oe_hdr where order_no in (100000, 100010, 100020) SELECT * from oe_hdr where ship2_add1 LIKE '1234 Market%'
Wildcards % Any string of zero or more characters _ Any single character [] Any single character within a range/set [^] Single character not with a range or set
Wildcards SELECT * from oe_hdr where ship2_add1 LIKE '%Ave% SELECT * from oe_hdr where order_no in (100000, 100005, 100006) OR ship2_add1 IS NULL
Sorting Results ORDER BY clause sorts the results of your query in a specified order SELECT * from oe_hdr where order_no > 150000 ORDER BY ship2_name SELECT * from oe_hdr where order_no > 150000 ORDER BY ship2_name DESC
Grouping Results Commonly used in conjunction with aggregate functions to produce summary data SELECT order_no, AVG(extended_price) from oe_line GROUP BY order_no SELECT order_no, SUM(qty_ordered) from oe_line GROUP BY order_no
HAVING SELECT order_no, AVG(extended_price) FROM oe_line GROUP BY order_no HAVING AVG(extended_price) >= 70
Implementing Joins In a relational database environment, the information you need often resides in more than one table In order to retrieve this data, you must join to tables
Joins INNER JOIN CROSS JOIN OUTER JOIN Includes only rows that satisfy the join condition Includes every combination of all of the rows between the two tables Includes the rows that satisfy the join condition, plus the remaining rows in one of the tables
Joins SELECT column_list FROM table join_operator ON table_name.column_name = table_name.column_name WHERE table_name.column_name
Allowed Join Operators = Equal to > Greater than < Less than >= Greater or equal to <= Less or equal to <> Not equal to
Inner Joins Connect two tables to create a third table that includes only the rows satisfying the join condition
INNER JOIN Example SELECT oe_hdr.customer_id, oe_line.qty_invoiced, oe_line.commission_cost FROM oe_hdr INNER JOIN oe_line ON oe_hdr.order_no = oe_line.order_no WHERE oe_hdr.customer_id = 100000
Cross Joins Result set containing all the combinations of the rows between tables in the join 5 rows, 9 cols = 45 rows in result set Cross Join Example SELECT customer_id, qty_invoiced FROM oe_hdr CROSS JOIN oe_line
Outer Joins - ANSI Left Outer Join Right Outer Join Full Outer Join Includes all rows from the table on the left, while only including related records from the table on the right Includes all rows from the table on the right, while only including related records from the table on the left Includes all non-matching rows for both the left side table and the right side table
Outer Joins SELECT inv_mast.item_id, sum(oe_line.qty_ordered) FROM inv_mast LEFT OUTER JOIN oe_line ON inv_mast.inv_mast_uid = oe_line.inv_mast_uid GROUP BY inv_mast.item_id
Outer Joins Outer joins include all rows in the outer table and only those related rows from the inner table Outer joins can be done between only two tables
INSERT Statement Overview Basic syntax Inserting rows with SELECT
INSERT Statement Data modification statement that inserts rows into a table while still observing constraints on the columns of the table Basic Syntax INSERT INTO table_name (column_list) VALUES (value_list)
INSERT statement INSERT INTO oe_hdr (order_id, customer_id, ship_to_address) VALUES ( 100050, 107543, 1234 Any Street)
Inserting with SELECT Insert more than one row at a time into a table SELECT * INTO oe_line_copy FROM oe_line (table does not exist) INSERT INTO oe_line_copy SELECT * FROM oe_line (table already exists)
UPDATE Statement Overview Basic Syntax Updating data based on other tables
UPDATE Statement Updates data that already exists in tables Observes column and table constraints that reside on the table UPDATE table SET Specifies the columns and the new values WHERE Specifies the exact rows to update
UPDATE syntax UPDATE table_name SET column_name = expression, column_name2 = expression WHERE where_clause
UPDATE statement UPDATE inv_mast SET price1 = price1 + (price1 *.10) UPDATE inv_mast SET description = Discontinued Item WHERE inactive = Y
Summary System stored procedures System functions Select statements SELECT statement INSERT statement UPDATE statement
Visit Activant on the web: distribution.activant.com View Educational Services training course offerings Microsoft SQL Server: three-day classroom training Browse Documentation Search the Solutions database Submit a web case Email Professional Services Sales: pss@activant.com