Introduction to SQL for SAP Business One - SAP B1 Tips...Introduction to SQL and SAP Business One...
Transcript of Introduction to SQL for SAP Business One - SAP B1 Tips...Introduction to SQL and SAP Business One...
Introduction to SQL and SAP Business One Queries
Contents ABOUT THE AUTHOR .................................................................................................................................... 2
INTRODUCTION ............................................................................................................................................ 3
SQL ............................................................................................................................................................ 3
What is SQL? ......................................................................................................................................... 3
Databases ............................................................................................................................................. 3
Queries .................................................................................................................................................. 4
SAP BUSINESS ONE ................................................................................................................................... 4
QUERIES AND SAP BUSINESS ONE ............................................................................................................... 4
QUERY GENERATOR ................................................................................................................................. 4
QUERY MANAGER .................................................................................................................................. 10
QUERY WIZARD ...................................................................................................................................... 11
MSSQL MANAGEMENT STUDIO ............................................................................................................. 16
EXAMPLES AND EXERCISES ........................................................................................................................ 16
BUSINESS PARTNERS .............................................................................................................................. 16
EXAMPLE 1 .......................................................................................................................................... 16
INVENTORY ............................................................................................................................................. 17
EXAMPLE 1 .......................................................................................................................................... 17
SALES ....................................................................................................................................................... 17
EXAMPLE 1 .......................................................................................................................................... 17
Purchasing .............................................................................................................................................. 17
EXAMPLE 1 .......................................................................................................................................... 17
WHAT IS NEXT? ........................................................................................................................................... 17
ABOUT THE AUTHOR
My name is Rafael Santos Oliveira. I was born in Brazil in 1983. I have a degree in Computer Science
from the Universidade Federal de Itajuba.
I have been working with SAP Business One since 2006. I started working as a SDK developer for
solutions like WMS, BI and Field Sales. Currently I work as a project manager.
I’ve been in projects for companies like Jaguar Land Rover Brazil and BMW.
Introduction to SQL and SAP Business One Queries
I hope that with this e-book I’ll be able to share some of my knowledge about SAP Business One and
MSSQL.
INTRODUCTION
This eBook is an introduction to SQL for SAP Business One. If you have no experience writing SQL
queries, this material is for you. If you have no experience with SAP Business One, this material is NOT
for you.
Throughout this eBook, you will learn what SQL is and how to write queries specific for the ERP
SAP Business One.
The intention of this material is to provide the knowledge on how to be self-sufficient and write
your own queries/reports. All end-users with experience of any module of SAP Business One are
welcome. Modules such as:
Sales
Purchasing
Business Partners
Inventory
Finance
Production
Item Master Data
SQL
What is SQL?
SQL (Structure Query Language) is the language used to retrieve information from a relational
database.
Databases
Database is an organized collection of data. It is the collection of schemes, tables, objects, views,
stored procedures.
Introduction to SQL and SAP Business One Queries
SAP Business One uses Microsoft SQL Database. For the purpose of this course, I will be using
the 2012 version.
Queries
Query language use statements to retrieve, update or delete the data from a database. This
eBook will show you how to retrieve data. In addition, you will see that the statement always starts with
a SELECT statement.
SAP BUSINESS ONE
SAP Business One is an affordable, easy-to-implement business management solution designed
specifically to meet the needs of emerging and dynamically growing small companies. (Source:
http://help.sap.com/businessone)
QUERIES AND SAP BUSINESS ONE
Here you will find the different methods and places where queries can be written to retrieve
data from SAP Business One.
QUERY GENERATOR
You can use the query generator to familiarize yourself with the tables/fields from SAP Business
One. This option is located here:
Introduction to SQL and SAP Business One Queries
Note 1: You will notice that the main tables in SAP Business One always starts with the letter O.
(Example: OCRD for Business Partners). The linked tables will follow this format CRD1 – Business
Partners Addresses, CRD2 – Business Partners Payment Methods and so on.)
Introduction to SQL and SAP Business One Queries
If you want to add conditions to your query (Example: Retrieve data only from Customers), here is
where you can do it:
In this case, you may ask: How do I know the possible values from the CardType field from the
OCRD table?
To answer this question you will need to have the Database Help file from SAP Business One.
This file is located here after you have the SDK installed:
Introduction to SQL and SAP Business One Queries
Another option is to activate the option View -> System Information in SAP Business One. Then just go
to the CardType field to see the option available:
Introduction to SQL and SAP Business One Queries
After you click ‘Execute’ in the Query Generator screen you will be able to see a preview of your
query:
You can save it by click on the Save button and give a name to your query:
Introduction to SQL and SAP Business One Queries
QUERY MANAGER
In the query manager, you will be able to see and modify all the queries you created:
Introduction to SQL and SAP Business One Queries
QUERY WIZARD
The Query Wizard is another option for you to write your queries. The idea of this tool is to
guide you through every step. It will not allow you build very complex statements, but it is definitely a
good place to start.
You can find the Query Wizard here:
Introduction to SQL and SAP Business One Queries MSSQL MANAGEMENT STUDIO
Once you reach an intermediate/advanced level, I recommend that you start using the MSSQL
Management Studio to write your queries. MSSQLMS is a management tool that allow you to execute
many different tasks. However, the focus here is the great interface to write and preview queries.
You will see that the queries look much nicer in the MSSQLMS:
Once you feel comfortable to write your own queries from scratch, ask your dba or system
administrator to provide you access to the MSSQLMS.
EXAMPLES AND EXERCISES
BUSINESS PARTNERS
EXAMPLE 1
Bring a list of business partners with the BP Type, Customer Code, Customer Name and Customer
Group:
SELECT Case When T0.CardType = 'C' then 'Customer' When T0.CardType = 'S' then 'Vendor' else 'Lead' end as [Customer Type], T0.CardCode, T0.CardName, T1.GroupName FROM OCRD T0 INNER JOIN OCRG T1 ON T0.GroupCode = T1.GroupCode
Introduction to SQL and SAP Business One Queries INVENTORY
EXAMPLE 1
Bring the Item No, Item Description, Item Group,Warehouse, Quantity On Hand, Quantity Committed
and Quantity on order. But only for items in stock:
SELECT T0.ItemCode, T0.ItemName, T2.ItmsGrpNam, T1.WhsCode, T1.OnHand,T1.IsCommited, T1.OnOrder FROM OITM T0 INNER JOIN OITW T1 ON T0.ItemCode = T1.ItemCode INNER JOIN OITB T2 ON T0.ItmsGrpCod = T2.ItmsGrpCod WHERE T1.OnHand > 0
SALES
EXAMPLE 1
Bring the list of all open Sales Orders with: Order No, Customer Code, Customer Name, Customer Ref.
No, Order Date and Order Total:
SELECT T0.DocNum, T0.CardCode, T0.CardName, T0.NumAtCard, T0.DocDate, T0.DocTotal FROM ORDR T0 WHERE T0.DocStatus = 'O'
Purchasing
EXAMPLE 1
Bring the list of all open Purchase Orders with: PO No, Vendor Code, Vendor Name, Vendor Ref. No, PO
Date, PO Delivery Date and PO Total:
SELECT T0.DocNum, T0.CardCode, T0.CardName, T0.NumAtCard, T0.DocDate,T0.DocDueDate, T0.DocTotal FROM OPOR T0 WHERE T0.DocStatus = 'O'
WHAT IS NEXT?
Introduction to SQL and SAP Business One Queries
I recommend starting by familiarizing yourself with SAP B1 tables first. Use the wizard and see the SQL
statement generated. Once you feel comfortable enough, start writing your own statements from
scratch.
In this case practice will help you a lot.
If you have any questions, or need help to build a specific query, feel free to send me a message here:
http://sapb1tips.com/about/
Or email me at:
Enjoy!