What’s You SQL IQ?• Level 1• Select * from RM00101
• Level 2• Update RM00101 set SLPRSNID=‘TOM’ where [STATE]=‘TX’
• Level 3• Update c set c.COMMENT1 = dtInvCount.Invoices from RM00101 c join
(Select CUSTNMBR, count(SOPNUMBE) as Invoices from SOP30200 group by CUSTNMBR) as dtInvCount on dtInvCount.CUSTNMBR = c.CUSTNMBR
Or are you….• Level 4
With cteCustTotals (CUSTNMBR, CUSTNAME, TotalAmount) as (Selectc.CUSTNMBR, c.CUSTNAME, sum(s.DOCAMNT) as TotalAmount fromRM00101 c join SOP30200 s on s.CUSTNMBR = c.CUSTNMBR ands.SOPTYPE = 3 group by c.CUSTNMBR, c.CUSTNAME)• Select CUSTNMBR, CUSTNAME, TotalAmount, case when TotalAmount <1000 then '1' when TotalAmount < 5000 THEN '2' when TotalAmount <10000 then '3‘ when TotalAmount < 20000 then '4' when TotalAmount< 50000 then '5' else '6' end as CustLevel from cteCustTotals order byTotalAmount
Safe Query Development
• Rogue SQL Queries• Minimize impact on production databases• TOP N Clause• WITH (NO LOCK)• WHERE clause
Joins and Table Aliases
•Table aliases• Provide plain language reference• Easier to understand, follow scripts• Simplifies joins• Can also use field aliases
Joins and Table Aliases
• Combine Data from multiple tables/views with JOIN• INNER• LEFT OUTER• RIGHT OUTER• FULL OUTER• CROSS JOIN
• Include components JOIN and ON
Handy SQL Keywords• Comments: -- OR /* */
• DISTINCT• SELECT DISTINCT CUSTNMBR FROM SOP30200
• RTRIM, for spaces• SELECT RTRIM(CUSTNAME)
• AS <fieldalias>:• SELECT RTRIM(CUSTNAME) AS CUSTNAME
• CASE WHEN ELSE• Example Later!
• LIKE• WHERE CUSTNMBR LIKE ‘AA%’
Handy SQL Keywords (2)• HAVING with GROUP BY
• GROUP BY CUSTNMBR HAVING COUNT (*) > 10
• ORDER BY• ORDER BY CUSTNMBER
• ISNULL• ISNULL(ACTIDX, ‘None’)
• IN• WHERE ACTIDX IN (3, 45, 56, 62)
• EXISTS• WHERE EXISTS (SELECT * FROM…WHERE =)
Handy SQL Keywords (3)• UNION
• Example Later!
• SUM, COUNT, MIN, MAX• Used with GROUP BY, SUM(CUSTBLNC)
• BETWEEN• WHERE CUSTBLNC BETWEEN 10000 AND 100000• Replaces >= and <=
• SELECT TOP• SELECT TOP (10) PERCENT• WITH TIES
Subqueries
• Query within a query or “nested” query• Can be embedded in…• Single Field• Where Clause
Common Table Expressions• Referred to as “CTE”s
• Allow you to setup complex subqueries separately
• Creates “tables” of information on the fly• Temporary result set• Nested SELECT statements
• Similar to a derived table but• Self-reference• Reference multiple times
More Demo Time!
UPDATE with JOINS• Multiple JOINS• Comparisons (IN, NOT IN, WHERE NOT EXISTS)• Information_Schema• CASE WHEN ELSE• Parameters• UNION/UNION ALL• PIVOT• Views and Stored Procedures
Partition with ROWNUM
• ROW NUMBER allows for sequential numbering of records, RANK can be used instead for ranking
• OVER () stipulates the window over which the row numbering or ranking occurs
• PARTITION BY applies grouping without summarizing, to “reset” ranking or row numbering• Can be based on a single field, or multiple fields
Dynamics GP Table Naming
Structured as:a 2 or 3 character module codeFollowed by a 5 digit number
Module code
Ex. RM00101
Dynamics GP Table Naming
More module code examples
Payables Management – PM
General Ledger – GL
Inventory Control – IV
Dynamics GP Table Naming
The first digit represents the table type
Master – 0 Work – 1Open – 2 TableHistory – 3
Ex. RM0010
Dynamics GP Table NamingThe second and third digits are sequences.
Sequence Number
Ex. RM00101
VariantsThe fourth and fifth digits are variants.
Table Naming
Module Module Code
Cash Management CM
General Ledger GL
Inventory Control IV
Invoicing IVC
Multicurrency Management MC
Payables Management PM
Purchase Order Processing POP
Receivables Management RM
Sales Order Processing SOP
System Manager SM
Table Naming
Table Type Value
Master 0
Work 1
Open 2
History 3
Setup 4
Temp 5
Relation 6
Report Options 7
Dynamics GP Field Naming
Earlier GP versions were limited to 8 character names
Ex. Customer Number – CUSTNMBR
Dynamics GP Views
A view is a virtual table
Cannot hold information, but will query tables and present data differently
Can join multiple tables:
Ex. Open and history
Finding Tables and Fields
Inside GP, click:Tools > Resource Descriptions
Support debugging tool:Tools > Support Debugging Tool –
Debugger Menu > Resource Information
SQL Management Studio
Data Table Resources
http://victoriayudin.com/gp-tables/Listing of commonly used tables and good explanations of fields where index numbers are used (ex. SOP Document Type: Quote = 1, Order = 2, invoice = 3)
Tools used to write Statements
• SSMS – SQL Server management studio
• Support debugging tool (Sql execute)
• Many other free tools• Need to create a connection to database
Changing Company Colours
• Found under administrator settings
• Debugger.xml file must be available to all workstations for this to work.
• Debugger location is set in Dex.ini setting. Under pathname location
Capture screenshots
• Sent a copy of dex.ini, set file and system file to system admin
• Can reduce support costs.
• All users have access to this
Q&A
• Thanks for Attending
http://www.prophet.ca/user-group-2014-presentations/
Paul [email protected]
Password: UserGroup2014