prophet 21 world wide users group - db revealed programming webinar.pdf · 2014. 9. 18. ·...

40
Prophet 21 World Wide User Group Webinars SQL Queries & Views (Basic Skill Level) Barry Hallman

Upload: others

Post on 23-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Prophet 21 World Wide User Group Webinars

SQL Queries & Views (Basic Skill Level)

Barry Hallman

Page 2: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Disclaimer

• This webinar is an attempt by P21WWUG members to assist each other by demonstrating ways that we utilize the Prophet21 system and other related products.

• The P21WWUG and the individuals conducting these webinars take no responsibility for potential issues that arise as a result of taking the advice given during the session.

• The P21WWUG does not recommend using any SQL statements to update your database without having those statements first reviewed by Activant or other experienced SQL professionals.

• Using SQL statements to update your database may result in corrupting your database

6

Page 3: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Barry Hallman

• Purchasing agent (1973-1981) • Sales manager for wholesale distributor (1981-1983) • Regional manager for national computer retailer (1983-1986) • Director of computer consulting for an accounting firm (1986-1992) • Senior implementation consultant for local consulting firm (1992-1994) • Accounting software and network support (1992-present) • Database manager (1992-present) • Favorite color (blue)

7

Page 4: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Project Description SITUATION: We have been “asked” by a major customer to print large labels for every product we ship to

them. The labels (4”x6”) are to include: • Our company name and address • Customer’s shipping address • Customer’s part number (with barcode) • Purchase order number (with barcode) • Quantity shipped (with barcode)

We previously have produced these labels using Label Matrix software and a Zebra printer, but all the data was manually entered for every label , except for our address. SOLUTION: Create a query that can be accessed by the label software that will populate ALL the data by selecting data from the pick ticket.

8

Page 5: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

The Finished Product

9

Looks Simple

Enough!

Page 6: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Agenda

1. Anatomy of a database 2. Using Prophet 21 to point us to specific data fields 3. Navigating SQL Management Studio (2005) 4. Basic query structure 5. Simple SQL syntax 6. Accessing data from multiple tables 7. Filtering data to be returned 8. Saving a query 9. Converting a query to a VIEW

10

Page 7: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Goals

Understand how: – To use the Prophet21 for identifying data fields – To use SQL Management Studio (2005) – A database is structured – To retrieve data from a table – To retrieve data from multiple related tables – To save a query – To convert a query to a re-usable view

11

Page 8: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

The Finished Product – Data Types Identified

12

From Prophet 21

Label Template Info

Page 9: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

The Finished Product – Data Fields Identified

13

Customer Ship-to Name Ship-to Addresses 1 and 2 Ship-to City Ship-to State Ship-to Zip

Customer Part Number

Customer PO Number

Quantity Shipped

Page 10: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Step 1:

The Anatomy of a Database

•Database name – Database Diagrams – Tables – house the actual data

Arranged as COLUMNS and ROWS – rather like an Excel spreadsheet – Views – provide quick access to the tables (like an Excel “print preview”) – Synonyms – Programmability – Service Broker – Storage – Security

(Highlighted items are beyond the scope of this webinar)

14

Page 11: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Step 2:

Using Prophet 21 to point us to specific data fields

• Navigate in Prophet 21 to a screen that shows the target data • RMB (right click) on the field • Choose (left click) “HELP” from the drop-down menu • Choose “SQL Information” – may show the data location • Choose the “More” button • Choose the “SQL” tab – shows the entire P21 query

(NOTE: The query shown may be difficult to decipher!)

(Examples provided on next 2 slides)

15

Page 12: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Example 1 – Access the SQL Help:

Using Prophet 21 to point us to specific data fields

16

1. RMB (right click) ContactId

2. Left click Help

3. Left click on SQL Information

Page 13: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Example 1 – View the Desired Information:

Using Prophet 21 to point us to specific data fields

17

4. Here’s the Field Info.

Page 14: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Example 1 – View the Desired Information:

Using Prophet 21 to point us to specific data fields

18

Data is specified in SQL as:

{table name} . {column name}

Page 15: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Example 2 – Access the SQL Help:

Using Prophet 21 to point us to specific data fields

19

1. RMB (right click) Pick Ticket Id

2. Left click Help

3. Left click on SQL Information

Page 16: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Example 2 – Access SQL Help:

Using Prophet 21 to point us to specific data fields

20

(Not very useful Information)

4. Left click on More>>

Page 17: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Example 2 – Access the SQL Tab:

Using Prophet 21 to point us to specific data fields

21

5. Left click the SQL tab

Page 18: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Example 2 – View the Desired Information:

Using Prophet 21 to point us to specific data fields

22

6. Needed Field Information (if you read SQL!!!)

Page 19: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

The Finished Product – Data Fields Identified

23

oe_line.customer_part_number

oe_hdr.po_no

oe_pick_ticket_detail.print_quantity

oe_hdr.ship2_name oe_hdr.ship2_addr1

oe_hdr.ship1_city oe_hdr.ship1_state oe_hdr.ship2_zip

oe_hdr.ship2_addr2

Page 20: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Step 3:

“Finding” the Data in SQL

• Open Microsoft SQL Management Studio • Connect to the SQL server – i.e. log on/connect • Expand “Databases” in the Object Explorer • Locate the Prophet 21 (Play) database and expand it • Tables - 1,556 tables in version 12.0 • Views - 1,886 views in version 12.0

(Total column references : 69,990 in all tables, views and triggers) (This will be demonstrated during the “LIVE” portion of the presentation)

24

Page 21: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Step 4:

Basic SQL Query Syntax

“Live” SQL Query Demonstration

(Please refer to refer to separately published

Supplements 1 & 2 for printed details)

25

Page 22: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Step 4:

Basic SQL Query Syntax

• Choose the database that you will be querying – Chosen from the user interface or the “USE” statement

• Begins with a SELECT statement to specify the action • Followed by a list of columns (separated by commas)

– Use of the * wildcard character – Specify named columns

• Ends with a FROM statement to identify the data table (See live demonstration)

26

Page 23: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Step 5:

Selecting Data from Multiple Databases

•Choose the “primary” database •Choose the first supplemental database •“JOIN” the two databases

– INNER Join – LEFT OUTER Join – RIGHT OUTER Join – FULL Join – CROSS Join

(Highlighted items are beyond the scope of this presentation) (See SUPPLEMENT for explanations)

27

Page 24: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Step 6:

Filtering Data

• Using the “WHERE” clause – Specifying the rows to be returned from a table (based on data in the row) – Using multiple filter criterion

(See SUPPLEMENT for explanations)

28

Page 25: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Step 7:

Finishing the Query

• Sorting the data using the “ORDER BY” clause

• Clean up the “look” of the query – Using proper capitalization for readability – Using proper indentation for readability – Adding COMMENTS to the query

• Saving the query

(See live demonstration)

29

Page 26: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Step 8:

Converting the Query to a VIEW

• Comment or Remove the “USE” statement • Comment or Remove the “ORDER BY” statement • Using the “CREATE QUERY” clause • Using the “ALTER QUERY” clause

(See live demonstration)

30

Page 27: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

P21 WWUG Webinar Basic SQL Queries & Views

Questions and

Answers

31

Page 28: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Disclaimer

• This webinar is an attempt by P21WWUG members to assist each other by demonstrating ways that we utilize the Prophet21 system and other related products.

• The P21WWUG and the individuals conducting these webinars take no responsibility for potential issues that arise as a result of taking the advice given during the session.

• The P21WWUG does not recommend using any SQL statements to update your database without having those statements first reviewed by Activant or other experienced SQL professionals.

• Using SQL statements to update your database may result in corrupting your database

32

Page 29: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Contact Information

Barry Hallman President

Hallman Consulting & Services, Ltd. (513) 561-4300

[email protected]

33

Page 30: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Page 1 of 11

Query Development - Supplement 1 Step 1

Code Sample /* Query: Pick_Ticket_Label_Data_view.sql Purpose: To retrieve data for printing package labels from pick tickets with label matrix Created: 03-20-2009 - gbh Modified: 08-09-2010 - gbh - Removed unnecessary content for demonstration */ -- The next line specifies which SQL database we will use USE VBH /* Characters marking the beginning of a comment block

Query: Pick_Ticket_Label_Data_view.sql Purpose: To retrieve data for printing package labels from pick tickets with label matrix Created: 03-20-2009 - gbh Modified: 08-09-2010 - gbh - Removed unnecessary content for demonstration

*/ Characters marking the ending of a comment block

-- Characters beginning a comment for ONE LINE ONLY. This is especially useful annotating the code for enhanced understandability and readability, or to DISABLE a code line.

USE The SQL action that specifies the default database to be used by subsequent SQL actions. VBH -- the name of the selected database for this exercise

! Execute

or F5 The menu bar choice or function key to cause the SQL code to EXECUTE

What these statements say is: “Here is some information (COMMENT) that is relevant to this query, but this text is ONLY here for the reader, and will be ignored when the query is executed. “

“I want to retrieve data from this (USE) database“

Page 31: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Page 2 of 11

Query Development - Supplement 1 Step 2

Code Sample SELECT pick_ticket_no ,order_no FROM oe_pick_ticket

SELECT The SQL action that specifies you want to retrieve data from the database

{columns list} pick_ticket_no ,order_no

FROM The SQL action that specifies you want to retrieve data from the database {table name} oe_pick_ticket

* The “ * “ (asterisk) serves as a wildcard character to denote “all-columns”

What these statements say is: “Show me (SELECT) the data in the columns pick_ticket_no and order_no from (FROM) the

table oe_pick_ticket”

OR (if using the wildcard character)

“Show me (SELECT) the data in ALL the columns from (FROM) the table oe_pick_ticket”

Page 32: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Page 3 of 11

Query Development - Supplement 1 Step 3

Code Sample SELECT OE_PT.pick_ticket_no AS PickTicketNo ,OE_PT.order_no AS VBH_OrderNo ,OE_PTDtl.line_number AS PickTickLineNo ,OE_PTDtl.print_quantity AS PickTicketQty ,OE_PTDtl.unit_of_measure AS UOM FROM oe_pick_ticket AS OE_PT INNER JOIN oe_pick_ticket_detail AS OE_PTDtl ON

OE_PT.pick_ticket_no = OE_PTDtl.pick_ticket_no

AS Notes that the following “name” is an alias for a specific column or table name

{columns list}

OE_PT.pick_ticket_no AS PickTicketNo ,OE_PT.order_no AS VBH_OrderNo ,OE_PTDtl.line_number AS PickTickLineNo ,OE_PTDtl.print_quantity AS PickTicketQty ,OE_PTDtl.unit_of_measure AS UOM

INNER JOIN

The SQL action that specifies you want to retrieve data from the database (see Supplement 2 for additional information on table joins) INNER JOIN oe_pick_ticket_detail AS OE_PTDtl (NOLOCK) ON

OE_PT.pick_ticket_no = OE_PTDtl.pick_ticket_no

ON Specifies that the expression that follows states the condition for connecting the data in two tables

{expression} Specifies the columns in each of the chosen tables that equal each other – creates the “link” between the two tables

What these statements say is: “Show me the data in the columns pick_ticket_no and order_no from the table oe_pick_ticket, and the columns line_number, print_quantity, and unit_of_measure from the table pick_ticket_detail using an alias (AS) for each column name and table, AND match the data up (INNER JOIN) by choosing the columns from the pick_ticket_detail table where the value in the pick_ticket_no column of the oe_pick_ticket table EQUALS the value in the pick_ticket_no column of the oe_picket_ticket_detail table.”

Page 33: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Page 4 of 11

Query Development - Supplement 1 Step 4

Code Sample SELECT DISTINCT OE_PT.pick_ticket_no AS PickTicketNo ,OE_PT.order_no AS VBH_OrderNo ,OE_PTDtl.line_number AS PickTickLineNo ,OE_PTDtl.print_quantity AS PickTicketQty ,OE_PTDtl.unit_of_measure AS UOM ,Inv_Mast.item_id AS ItemID ,Inv_Mast.item_desc AS ItemDesc FROM oe_pick_ticket AS OE_PT INNER JOIN oe_pick_ticket_detail AS OE_PTDtl (NOLOCK) ON

OE_PT.pick_ticket_no = OE_PTDtl.pick_ticket_no INNER JOIN inv_mast AS Inv_Mast (NOLOCK) ON OE_PTDtl.inv_mast_uid = I Inv_Mast.inv_mast_uid

DISTINCT Instructs SQL to only retrieve rows of data where EVERY COLUMN of data is unique from other rows. In other words, EXCLUDE all duplicate rows.

(NOLOCK) Specifies to the SQL database engine to NOT lock, or prevent access/update (to the records in this table) by other users or processes while this table is being queried by this query

{columns list}

OE_PT.pick_ticket_no AS PickTicketNo ,OE_PT.order_no AS VBH_OrderNo ,OE_PTDtl.line_number AS PickTickLineNo ,OE_PTDtl.print_quantity AS PickTicketQty ,OE_PTDtl.unit_of_measure AS UOM

What these statements say is: “Show me the data from Step 3 and also include the item_id and item_desc columns from the inv_mast table. Also, while retrieving this data, do not prevent (NOLOCK) any other users or processes from seeing or updating this data. AND while you are at it, exclude any duplicate rows (DISTINCT) from the retrieved data.”

Page 34: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Page 5 of 11

Query Development - Supplement 1 Step 5

Code Sample SELECT DISTINCT OE_PT.pick_ticket_no AS PickTicketNo ,OE_PT.order_no AS VBH_OrderNo ,OE_PTDtl.line_number AS PickTickLineNo ,OE_PTDtl.print_quantity AS PickTicketQty ,OE_PTDtl.unit_of_measure AS UOM ,Inv_Mast.item_id AS ItemID ,Inv_Mast.item_desc AS ItemDesc ,OE_Ln.customer_part_number AS Cust_PartNo FROM oe_pick_ticket AS OE_PT INNER JOIN oe_pick_ticket_detail AS OE_PTDtl (NOLOCK) ON

OE_PT.pick_ticket_no = OE_PTDtl.pick_ticket_no INNER JOIN inv_mast AS Inv_Mast (NOLOCK) ON OE_PTDtl.inv_mast_uid = I Inv_Mast.inv_mast_uid INNER JOIN oe_line AS OE_Ln (NOLOCK) ON (OE_PT.order_no = OE_Ln.order_no AND OE_Ln.line_no = OE_PTDtl.oe_line_no)

AND This operator indicates that BOTH conditions MUST be TRUE, or stated differently, both of the conditions (~equations) MUST equate to TRUE. Note that this “compound condition” is enclosed in parentheses.

What these statements say is: “Show me the data from Step 4 and also include the customer_part_number column from the oe_line table. This time, to “line up” the right customer part number with the right line from the detail section of the order, match the order_no column from the oe_pick_ticket table with the order_no column in the oe_line table, AND (AND) match the line_no column from the oe_line table to the line_no column from the oe_pick_ticket_detail table.”

Page 35: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Page 6 of 11

Query Development - Supplement 1 Step 6

Code Sample SELECT DISTINCT OE_PT.pick_ticket_no AS PickTicketNo ,OE_PT.order_no AS VBH_OrderNo ,OE_PTDtl.line_number AS PickTickLineNo ,OE_PTDtl.print_quantity AS PickTicketQty ,OE_PTDtl.unit_of_measure AS UOM ,Inv_Mast.item_id AS ItemID ,Inv_Mast.item_desc AS ItemDesc ,OE_Ln.customer_part_number AS Cust_PartNo ,OE_Hdr.po_no AS Cust_PONo ,OE_Hdr.ship2_name AS Ship2_Name ,OE_Hdr.customer_id AS Ship2_Addr1 ,OE_Hdr.ship2_add2 AS Ship2_Addr2 ,OE_Hdr.ship2_city AS Ship2_City ,OE_Hdr.ship2_state AS Ship2_State ,OE_Hdr.ship2_zip AS Ship2_Zip

,OE_Hdr.ship2_city + ', ' + OE_Hdr.ship2_state + ' ' + OE_Hdr.ship2_zip AS Ship2_CityStateZip

FROM oe_pick_ticket AS OE_PT INNER JOIN oe_pick_ticket_detail AS OE_PTDtl (NOLOCK) ON

OE_PT.pick_ticket_no = OE_PTDtl.pick_ticket_no INNER JOIN inv_mast AS Inv_Mast (NOLOCK) ON

OE_PTDtl.inv_mast_uid = Inv_Mast.inv_mast_uid INNER JOIN oe_line AS OE_Ln (NOLOCK) ON

(OE_PT.order_no = OE_Ln.order_no AND OE_Ln.line_no = OE_PTDtl.oe_line_no) INNER JOIN oe_hdr AS OE_Hdr (NOLOCK) ON OE_PT.order_no = OE_Hdr.order_no

{string math}

Various pieces of data can be “attached” to each other (or concatenated) by “adding” them together. In this example, the city, state, and zip code columns have been “added” together to create one string “CITY, STATE, ZIP”. (This was done to simplify our label layout. REMEMBER, THAT’S WHY WE STARTED ALL THIS!!!)

What these statements say is: “Take the contents of the ship2city column, “add” ({string math}) a comma and a space to the end of it, then add the contents of the ship2_state column to that, and then add two spaces, and finally the contents of the ship2_zip column to get a properly formatted city, state zip column”

Page 36: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Page 7 of 11

Query Development - Supplement 1 Step 7

Code Sample SELECT DISTINCT … {column listing}

FROM oe_pick_ticket AS OE_PT

INNER JOIN oe_pick_ticket_detail AS OE_PTDtl (NOLOCK) ON OE_PT.pick_ticket_no = OE_PTDtl.pick_ticket_no

INNER JOIN inv_mast AS Inv_Mast (NOLOCK) ON OE_PTDtl.inv_mast_uid = Inv_Mast.inv_mast_uid

INNER JOIN oe_line AS OE_Ln (NOLOCK) ON (OE_PT.order_no = OE_Ln.order_no AND

OE_Ln.line_no = OE_PTDtl.oe_line_no) INNER JOIN oe_hdr AS OE_Hdr (NOLOCK) ON OE_PT.order_no = OE_Hdr.order_no WHERE OE_PT.printed_flag = 'Y' AND OE_PT.delete_flag = 'N' AND OE_PTDtl.print_quantity > 0 AND OE_Hdr.cancel_flag = 'N' AND OE_Hdr.approved = 'Y' AND OE_Hdr.projected_order = 'N' AND OE_Ln.detail_type = 0 -- "Master" or "Top-Level" item AND OE_Ln.cancel_flag = 'N' AND OE_Ln.delete_flag = 'N'

WHERE This action specifies conditions at EVERY ROW must satisfy in order to be included in the data rows that are retrieved.

What these statements say is: “Include in our data ONLY rows where (WHERE) the pick ticket has been printed (printed_flag = ‘Y’), and where the pick ticket has not been deleted (delete_flag = ‘N’), and where the quantity printed on the pick ticket was greater than zero (print_quantity > 0), and where the order was NOT cancelled (cancel_flag = ‘N’), and where the order was approved (approved = ‘Y’), and where this is NOT a quote (projected_order = ‘N’), and where this is a master item and not components of another item (detail_type = 0), and where the line item was not cancelled from the order (cancel_flag) = ‘N’), and where the line item was not deleted from the order (delete_flag) = ‘N’).”

Page 37: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Page 8 of 11

Query Development - Supplement 1 Step 8

Code Sample SELECT DISTINCT … {column listing}

FROM oe_pick_ticket AS OE_PT

INNER JOIN oe_pick_ticket_detail AS OE_PTDtl (NOLOCK) ON OE_PT.pick_ticket_no = OE_PTDtl.pick_ticket_no

INNER JOIN inv_mast AS Inv_Mast (NOLOCK) ON OE_PTDtl.inv_mast_uid = Inv_Mast.inv_mast_uid

INNER JOIN oe_line AS OE_Ln (NOLOCK) ON (OE_PT.order_no = OE_Ln.order_no AND

OE_Ln.line_no = OE_PTDtl.oe_line_no) INNER JOIN oe_hdr AS OE_Hdr (NOLOCK) ON OE_PT.order_no = OE_Hdr.order_no WHERE OE_PT.printed_flag = 'Y' AND OE_PT.delete_flag = 'N' AND OE_PTDtl.print_quantity > 0 AND OE_Hdr.cancel_flag = 'N' AND OE_Hdr.approved = 'Y' AND OE_Hdr.projected_order = 'N' AND OE_Ln.detail_type = 0 -- "Master" or "Top-Level" item AND OE_Ln.cancel_flag = 'N' AND OE_Ln.delete_flag = 'N' ORDER BY OE_PT.pick_ticket_no, OE_PTDtl.line_number

ORDER BY This action specifies how the data is to be sorted as it is retrieved. Mulitiple levels of sorting is accomplished by specifying multiple columns to sort by, separated by commas.

ASC This signifies that the data is to be sorted in ascending order. This is the DEFAULT order that is assumed, so it need NOT be specified explicitly

DESC This signifies that the data is to be sorted in descending order. This sort order MUST be stated explicitly in the statement immediately following the column name – i.e. order_date DESC

What these statements say is: “Sort (ORDER BY) my output by the pick ticket number (pick_ticket_no), and then within a pick ticket, sort the detail lines by their line numbers (line_number). Do both of these sorts in ascending order (ASC) as I have not specified otherwise (DESC).”

Page 38: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Page 9 of 11

Query Development - Supplement 1 Step 9

Code Sample CREATE VIEW gbh_view_pick_ticket_lables AS --USE VBH SELECT DISTINCT … {column listing}

FROM oe_pick_ticket AS OE_PT INNER JOIN oe_pick_ticket_detail AS OE_PTDtl (NOLOCK) ON OE_PT.pick_ticket_no = OE_PTDtl.pick_ticket_no INNER JOIN oe_hdr AS OE_Hdr (NOLOCK) ON OE_PT.order_no = OE_Hdr.order_no INNER JOIN inv_mast AS Inv_Mast (NOLOCK) ON OE_PTDtl.inv_mast_uid = Inv_Mast.inv_mast_uid INNER JOIN oe_line AS OE_Ln (NOLOCK) ON (OE_PT.order_no = OE_Ln.order_no AND OE_Ln.line_no = OE_PTDtl.oe_line_no) WHERE OE_PT.printed_flag = 'Y' AND OE_PT.delete_flag = 'N' AND OE_PTDtl.print_quantity > 0 AND OE_Hdr.cancel_flag = 'N' AND OE_Hdr.approved = 'Y' AND OE_Hdr.projected_order = 'N' AND OE_Ln.detail_type = 0 -- "Master" or "Top-Level" item AND OE_Ln.cancel_flag = 'N' AND OE_Ln.delete_flag = 'N' --ORDER BY OE_PT.pick_ticket_no, OE_PTDtl.line_number

CREATE VIEW

This action directs SQL to use the query language that follows to create a new VIEW in the current database. (This action also some code to the query.

NOTE The USE and ORDER BY actions were commented out here as these actions cannot be included in a CREATE VIEW query.

What these statements say is: “This addition instructs SQL to use the contents of the query and create (and save) a view (CREATE VIEW) in my current database, that can be reused to access the data retrieved by the query as though it were a data table.”

Page 39: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Page 10 of 11

Query Development - Supplement 1 Step 10

Code Sample ALTER VIEW gbh_view_pick_ticket_lables AS -- The next line specifies which SQL database we will use --USE VBH SELECT DISTINCT … {column listing}

FROM oe_pick_ticket AS OE_PT INNER JOIN oe_pick_ticket_detail AS OE_PTDtl (NOLOCK) ON OE_PT.pick_ticket_no = OE_PTDtl.pick_ticket_no INNER JOIN oe_hdr AS OE_Hdr (NOLOCK) ON OE_PT.order_no = OE_Hdr.order_no INNER JOIN inv_mast AS Inv_Mast (NOLOCK) ON OE_PTDtl.inv_mast_uid = Inv_Mast.inv_mast_uid INNER JOIN oe_line AS OE_Ln (NOLOCK) ON (OE_PT.order_no = OE_Ln.order_no AND OE_Ln.line_no = OE_PTDtl.oe_line_no) WHERE OE_PT.printed_flag = 'Y' AND OE_PT.delete_flag = 'N' AND OE_PTDtl.print_quantity > 0 AND OE_Hdr.cancel_flag = 'N' AND OE_Hdr.approved = 'Y' AND OE_Hdr.projected_order = 'N' AND OE_Ln.detail_type = 0 -- "Master" or "Top-Level" item AND OE_Ln.cancel_flag = 'N' AND OE_Ln.delete_flag = 'N' --ORDER BY OE_PT.pick_ticket_no, OE_PTDtl.line_number

ALTER VIEW

This action directs SQL to use the query language that follows to CHANGE and existing VIEW in the current database.

NOTE This Step is included here ONLY to raise awareness of this action

What these statements say is: “By changing the code line from CREATE VIEW to ALTER VIEW, we are instructing SQL to modify an existing view (ALTER VIEW) and save this revised view in place of the previous view.”

NOTE: By commenting out the “ALTER VIEW” line of code, this becomes a query that can be executed from the query window – an especially helpful tool when “debugging” changes to your view. Remember to remove the comment notation and execute the query to save your changes to the view.

Page 40: Prophet 21 World Wide Users Group - db REVEALED Programming Webinar.pdf · 2014. 9. 18. · Customer’s shipping address • Customer’s part number ... Step 2: Using Prophet 21

Page 11 of 11

Query Development - Supplement 1 Completed Code

/* Query: Pick_Ticket_Label_Data_view.sql Purpose: To retrieve data for printing package labels from pick tickets with label matrix Created: 03-20-2009 - gbh Modified: 08-09-2010 - gbh - Removed unnecessary content for demonstration */ ALTER VIEW gbh_view_pick_ticket_lables AS -- The next line specifies which SQL database we will use --USE VBH SELECT DISTINCT OE_PT.pick_ticket_no AS PickTicketNo ,OE_PT.order_no AS VBH_OrderNo ,OE_PTDtl.line_number AS PickTickLineNo ,OE_PTDtl.print_quantity AS PickTicketQty ,OE_PTDtl.unit_of_measure AS UOM ,Inv_Mast.item_id AS ItemID ,Inv_Mast.item_desc AS ItemDesc ,OE_Ln.customer_part_number AS Cust_PartNo ,OE_Hdr.po_no AS Cust_PONo ,OE_Hdr.ship2_name AS Ship2_Name ,OE_Hdr.customer_id AS Ship2_Addr1 ,OE_Hdr.ship2_add2 AS Ship2_Addr2 ,OE_Hdr.ship2_city AS Ship2_City ,OE_Hdr.ship2_state AS Ship2_State ,OE_Hdr.ship2_zip AS Ship2_Zip

,OE_Hdr.ship2_city + ', ' + OE_Hdr.ship2_state + ' ' + OE_Hdr.ship2_zip AS Ship2_CityStateZip

FROM oe_pick_ticket AS OE_PT INNER JOIN oe_pick_ticket_detail AS OE_PTDtl (NOLOCK) ON OE_PT.pick_ticket_no = OE_PTDtl.pick_ticket_no INNER JOIN oe_hdr AS OE_Hdr (NOLOCK) ON OE_PT.order_no = OE_Hdr.order_no INNER JOIN inv_mast AS Inv_Mast (NOLOCK) ON OE_PTDtl.inv_mast_uid = Inv_Mast.inv_mast_uid INNER JOIN oe_line AS OE_Ln (NOLOCK) ON (OE_PT.order_no = OE_Ln.order_no AND OE_Ln.line_no = OE_PTDtl.oe_line_no) WHERE OE_PT.printed_flag = 'Y' AND OE_PT.delete_flag = 'N' AND OE_PTDtl.print_quantity > 0 AND OE_Hdr.cancel_flag = 'N' AND OE_Hdr.approved = 'Y' AND OE_Hdr.projected_order = 'N' AND OE_Ln.detail_type = 0 -- "Master" or "Top-Level" item AND OE_Ln.cancel_flag = 'N' AND OE_Ln.delete_flag = 'N' --ORDER BY OE_PT.pick_ticket_no, OE_PTDtl.line_number