SQL and Support Debugging Tool Paul Johnson and Graham O’Bray
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 (Select c.CUSTNMBR, c.CUSTNAME, sum(s.DOCAMNT) as TotalAmount from RM00101 c join SOP30200 s on s.CUSTNMBR = c.CUSTNMBR and s.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 < then '3‘ when TotalAmount < then '4' when TotalAmount < then '5' else '6' end as CustLevel from cteCustTotals order by TotalAmount
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 : 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 AND 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
Finding Data
Dynamics GP Table Naming Structured as: a 2 or 3 character module code Followed 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 – 1 Open – 2 Table History – 3 Ex. RM0010
Dynamics GP Table Naming The second and third digits are sequences. Sequence Number Ex. RM00101 Variants The fourth and fifth digits are variants.
Table Naming ModuleModule Code Cash ManagementCM General LedgerGL Inventory ControlIV InvoicingIVC Multicurrency ManagementMC Payables ManagementPM Purchase Order ProcessingPOP Receivables ManagementRM Sales Order ProcessingSOP System ManagerSM
Table Naming Table TypeValue Master0 Work1 Open2 History3 Setup4 Temp5 Relation6 Report Options7
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 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
Finding GP data using SDTL Use the Resource information menu in the support debugging tool
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
Audit security Security Profiler Security Information
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 Paul Johnson Password: UserGroup2014