Application Library and Example
AC500 Microsoft SQL Library
Function Block Library for AC500
and Application Example
Content
1 Disclaimer................................................................................................................... 3
1.1 For customers domiciled outside Germany / Für Kunden mit Sitz außerhalb
Deutschlands .................................................................................................................................. 3
1.2 Nur für Kunden mit Sitz in Deutschland ........................................................................... 4
2 Introduction................................................................................................................ 5
2.1 Hardware and Software requirements ............................................................................. 5
2.2 Safety instructions............................................................................................................ 6
3 PLC Configuration ........................................................................................................ 7
3.1 Automation Builder .......................................................................................................... 7
3.2 AC500 CPU........................................................................................................................ 7
3.3 Firmware .......................................................................................................................... 8
4 Install Microsoft SQL database ..................................................................................... 9
· Download SQL Server 2012 Express Edition ................................................................................. 9
· Download SQL Server 2008 Express Edition ................................................................................. 9
· Download SQL Server 2012 Management Studio Express ............................................................ 9
· Download SQL Server 2008 Management Studio Express ............................................................ 9
· Install SQL Server 2012 ................................................................................................................. 9
· Install SQL Server 2008 ................................................................................................................. 9
5 Setup Microsoft SQL database.................................................................................... 10
5.1 Firewall settings ..............................................................................................................12
6 Configure the database.............................................................................................. 13
6.1 Add a new user................................................................................................................16
6.2 Create a database............................................................................................................18
11.2015 Application AC500 MSSQL Library and Example 1
6.3 Create a Table .................................................................................................................19
6.4 Insert into Table ..............................................................................................................21
7 MSSQL Function Block Library .................................................................................... 24
7.1 Overview .........................................................................................................................24
7.2 Usage ..............................................................................................................................25
7.3 Communication ...............................................................................................................26
7.3.1 Generally...............................................................................................................26
7.4 Function Blocks ...............................................................................................................27
7.4.1 MSSQL_Connect ....................................................................................................27
7.4.2 MSSQL_Execute ....................................................................................................30
7.4.3 MSSQL_Execute_Compact.....................................................................................34
7.4.4 MSSQL_GetValue ..................................................................................................38
7.4.5 MSSQL_Disconnect ...............................................................................................41
7.5 Example Program ............................................................................................................43
7.6 Supported data types ......................................................................................................45
8 Testing Environments ................................................................................................ 47
8.1 Microsoft SQL Server 2008 ..............................................................................................47
8.2 Microsoft SQL Server 2012 ..............................................................................................47
9 Modification ............................................................................................................. 48
11.2015 Application AC500 MSSQL Library and Example 2
1 Disclaimer
1.1 For customers domiciled outside Germany / Für Kunden mit Sitz
außerhalb Deutschlands
„Warranty, Liability:
The user shall be solely responsible for the use of this application example described within this file.
ABB shall be under no warranty whatsoever. ABB's liability in connection with this application
example or the files included within this file, irrespective of the legal ground, shall be excluded. The
exclusion of liability shall not apply in the case of intention or gross negligence. The present
declaration shall be governed by and construed in accordance with the laws of Switzerland under
exclusion of its conflict of laws rules and of the Vienna Convention on the International Sale of
Goods (CISG)."
„Gewährleistung und Haftung:
Der Nutzer ist allein für die Verwendung des in dieser Datei beschriebenen Anwendungsbeispiels
verantwortlich.
ABB unterliegt keiner Gewährleistung. Die Haftung von ABB im Zusammenhang mit diesem
Anwendungsbeispiel oder den in dieser Datei enthaltenen Dateien - gleich aus welchem
Rechtsgrund - ist ausgeschlossen. Dieser Ausschluß gilt nicht im Falle von Vorsatz oder grober
Fahrlässigkeit. Diese Erklärung unterliegt Schweizer Recht unter Ausschluß der
Verweisungsnormen und des UN-Kaufrechts (CISG)."
11.2015 Application AC500 MSSQL Library and Example 3
1.2 Nur für Kunden mit Sitz in Deutschland
"Gewährleistung und Haftung:
Die in diesem Anwendungsbeispiel enthaltenen Dateien beschreiben eine mögliche Anwendung
der Steuerung AC500 bzw. zeigen eine mögliche Einsatzart der Steuerung. Sie stellen nur
Beispiele für Programmierungen dar, sind aber keine fertigen Lösungen. Eine Gewähr kann nicht
übernommen werden.
Der Nutzer ist für die ordnungsgemäße, insbesondere vollständige und fehlerfreie Programmierung
der Steuerungen selbst verantwortlich. Im Falle der teilweisen oder ganzen Übernahme der
Programmierbeispiele können gegen ABB keine Ansprüche geltend gemacht werden.
Die Haftung von ABB, gleich aus welchem Rechtsgrund, im Zusammenhang mit den
Anwendungsbeispielen oder den in dieser Datei enthaltenen Dateien wird ausgeschlossen. Der
Haftungsausschluß gilt jedoch nicht in Fällen des Vorsatzes, der groben Fahrlässigkeit, bei
Ansprüchen nach dem Produkthaftungsgesetz, im Falle der Verletzung des Lebens, des Körpers
oder der Gesundheit oder bei schuldhafter Verletzung einer wesentlichen Vertragspflicht. Im Falle
der Verletzung einer wesentlichen Vertragspflicht ist die Haftung jedoch auf den vertragstypischen,
vorhersehbaren Schaden begrenzt, soweit nicht zugleich ein anderer der in Satz 2 dieses
Unterabsatzes erwähnten Fälle gegeben ist. Eine Änderung der Beweislast zum Nachteil des
Nutzers ist hiermit nicht verbunden.
Es gilt materielles deutsches Recht unter Ausschluß des UN-Kaufrechts."
Only for customers with seat in Germany
"Warranties and Liability:
The files contained in this application sample are only describing a possible use of the AC 500
control system showing a possible kind of operation of it. These are only examples of the
programming features and in no way working solutions. No warranty can be accepted.
Each user himself is exclusively responsible for the correct, i.e. complete and faultless
programming of the control system. In case of a partial or complete adoption of programming
samples no resulting claims may be asserted against ABB.
In connection with the application examples or the files contained therein any liability of ABB
regardless of the legal cause shall be excluded. The exclusion of liability does not apply in the case
of willful misconduct or gross negligence by ABB, claims under product liability law, in case of
damage to life, body or health or breach of an essential contractual obligation. In case of breach of
an essential contractual obligation the liability will, however, be limited to compensation for the
foreseeable damage, that is typical for this type of contract in as far as no other exceptions are
listed under sentence 2 of this subparagraph is present at the same time. An amendment of the
burden of proof to the disadvantage of the user shall not be associated with this.
German substantive law shall apply excluding the UN Convention on the International Sale of
Goods."
11.2015 Application AC500 MSSQL Library and Example 4
2 Introduction
This document describes an example to communicate with Microsoft SQL database (MSSQL),
where AC500 acts as Client. The advantage is to increase security by establishing communication
only from plant side (AC500). Only the IP address of the server is fixed, which benefit in the
mobile networks where standard IP addresses frequently change.
Ethernet
Client Microsoft SQL
2.1 Hardware and Software requirements
The following configuration is used in this document. The newer
version of hardware and software should be working. However the
functionality should be tested by the user himself.
NOTICE
· All instructions in this guide are based on default settings in the PLC
· Automation Builder version 1.1.0 or later
· The PLC required an Ethernet port
· The PLC required firmware version 2.4.2 or later
11.2015 Application AC500 MSSQL Library and Example 5
2.2 Safety instructions
· Follow all safety instructions delivered with the used components:
· Read all safety and installation instructions of the used PLC. See the online help in CoDeSys (Help
→ Contents → Target System → AC500 / S500 → Introduction → Overview → Regulations).
11.2015 Application AC500 MSSQL Library and Example 6
3 PLC Configuration
3.1 Automation Builder
The PLC program was written with the actual ABB Automation Builder version 1.1.0
However, should also run with newer versions.
3.2 AC500 CPU
Using a AC500 CPU with the actual version
11.2015 Application AC500 MSSQL Library and Example 7
3.3 Firmware
Using firmware V2.4.2 or later
11.2015 Application AC500 MSSQL Library and Example 8
4 Install Microsoft SQL database
This chapter will guide you on how to install a new SQL Server 2012. Please check the system requirements
at first. If you have already installed an SQL Server, you can skip this chapter.
1. Download your SQL Server Installation:
· Download SQL Server 2012 Express Edition
[Link]
· Download SQL Server 2008 Express Edition
[Link]
2. Download Management Studio:
· Download SQL Server 2012 Management Studio Express
[Link]
· Download SQL Server 2008 Management Studio Express
[Link]
3. Please follow the manual or videos in the internet how to install the different versions of SQL
Server. The same for Management Studio.
· Install SQL Server 2012
[Link]
· Install SQL Server 2008
[Link]
11.2015 Application AC500 MSSQL Library and Example 9
5 Setup Microsoft SQL database
This chapter will guide you on how to configure a new SQL Server 2012.
Please navigate to the “SQL Server Configuration Manager”. You can find them in:
Start->All Programs-> Microsoft SQL Server 20xx->Configuration Tools
1. In SQL Server Configuration Manager, click on SQL Server Network Configuration on the left panel,
then right click on each protocol and click on Enable.
2. In the console pane, click Protocols for <instance name>.
3. In the details pane, right-click the protocol you want to change, and then click Enable.
4. Select with right-click the TCP/IP protocol and choose Properties
5. Select the IP Addresses tab and scroll down.
11.2015 Application AC500 MSSQL Library and Example 10
6. Insert port “1433” in the TCP Port box. This is the default port MS SQL. You can also adapt this one.
7. Apply with OK
8. In the console pane, click SQL Server Services.
9. In the details pane, right-click SQL Server (<instance name>), and then click Restart, to stop and
restart the SQL Server service.
11.2015 Application AC500 MSSQL Library and Example 11
5.1 Firewall settings
Windows firewall blocks all incoming TDS requests. Please make sure, that you have
access to the port 1433
CAUTION!
There are two ways to have access to the MSSQL database.
I. It’s NOT recommended! You can disable your windows firewall at all.
II. The better way is to create a rule, which allow to access to the MSSQL database from an external
device. In our case a PLC: Please go to your windows firewall options, choose advanced settings
and create a new rule on the right side.
Choose port -> next, select “TCP” and select “specific port” and insert “1433 -> next, allow this
connection -> next, set a cross at all three options -> next, insert a name and click on finish button.
11.2015 Application AC500 MSSQL Library and Example 12
6 Configure the database
This chapter will guide you on how to configure the database.
Please navigate to the “SQL Server Management Studio”. You can find them in:
Start->All Programs-> Microsoft SQL Server 20xx
1. The startup screen looks like this:
2. If the Server name is empty, please select <Browse for more…>
3. A window will comes up, switch to “Network Servers” tab and select your instance.
4. Keep as Authentication method the: Windows Authentication and click Connect button
5. Now, you are logged in.
6. In the next step we want to check the Server authentication. For that right-click at Server Instance
and select Properties.
11.2015 Application AC500 MSSQL Library and Example 13
7. Select the Security register and check if the SQL Server and Windows Authentication mode is
selected.
11.2015 Application AC500 MSSQL Library and Example 14
8. Click OK and restart the SQL server.
11.2015 Application AC500 MSSQL Library and Example 15
6.1 Add a new user
1. Navigate in the Object Explorer to the Security folder and select Logins. Here is a list of all existing
Microsoft SQL users.
2. With a right-click on the Logins folder, we can select New Login…
11.2015 Application AC500 MSSQL Library and Example 16
3. A new window appears, where we can set a Login name and a password for the new user. In this
case the username: ac500 and password: mssql
Please set all other parameters as you can see in this screen.
11.2015 Application AC500 MSSQL Library and Example 17
6.2 Create a database
1. Navigate in the Object Explorer to the Databases folder
2. Select with a right-click New Database…
3. Insert a name for the new database, in this case “db”
4. Confirm with OK
5. Push F5 key on your keyboard to refresh the object explorer
11.2015 Application AC500 MSSQL Library and Example 18
6.3 Create a Table
The following SQL statement defines the "ID" column to be an auto-increment primary key field in
the "Persons" table:
CREATE TABLE Persons
(
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature.
In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new
record.
Tip: To specify that the "ID" column should start at value 10 and increment by 5, change it to
IDENTITY(10,5).
You can easily insert a new table with a few fields using the attached file: “[Link]”
Select your database you want to insert the table and select:
File->Open->File… and select the [Link]
11.2015 Application AC500 MSSQL Library and Example 19
If you have selected the file, it looks like this:
Push the Execute button to execute the SQL statement.
If the command is executed successfully, you will see a success message in the Message window.
Push F5 key on your keyboard to refresh the object explorer
11.2015 Application AC500 MSSQL Library and Example 20
6.4 Insert into Table
You can easily insert a new entry with a few fields using the attached file: “[Link]”
Select your table you want to insert the entry and select:
File->Open->File… and select the [Link]
11.2015 Application AC500 MSSQL Library and Example 21
If you have selected the file, it looks like this:
Push the Execute button to execute the SQL statement.
If the command is executed successfully, you will see a success message in the Message window.
Push F5 key on your keyboard to refresh the object explorer
Select the table with a right-click and select:
Select Top 1000 Rows
11.2015 Application AC500 MSSQL Library and Example 22
Now, you get the Results in the Message window.
11.2015 Application AC500 MSSQL Library and Example 23
7 MSSQL Function Block Library
7.1 Overview
The following function blocks are provided by the AC500 MSSQL library:
Function Blocks
· MSSQL_CONNECT(FB) Establishes a connection to the Microsoft SQL database
· MSSQL_EXECUTE(FB) Executes the SQL statements (SELECT,INSERT,UPDATE…) with
maximum of 250 columns for a SELECT request
· MSSQL_EXECUTE_COMP Executes the SQL statements (SELECT,INSERT,UPDATE…) with
ACT(FB) maximum of 50 columns for a SELECT request
· MSSQL_GETVALUE(FB) Extracted from the result set a cell value
· MSSQL_DISCONNECT(FB) Closed the connection
11.2015 Application AC500 MSSQL Library and Example 24
7.2 Usage
To connect to the Microsoft SQL server, the MSSQL_CONNECT (FB) required having the IP address,
port number of the server, the username and password. Once the function block has run without
an error, the connection at the output pin CON is returned.
MSSQL_EXECUTE (FB) will take this connection to perform the given SQL Statement. The
NonQueryStatements(INSERT, UPDATE, CREATE, ...) represent the response of the FB pin on
OKPACKET. This offer information of all affected rows. SELECT statements, however fill the
ResultSet pin.
The values from the ResultSet can then be read by MSSQL_GETVALUE (FB). Once the connection is
established, any number of MS SQL statements can be executed. If the connection is no longer
required, they should always close the connection by using MSSQL_DISCONNECT (FB).
Focus applications are large diagnosis networks, where e.g. hundreds of sensors have to send their
data to the database.
The following example shows the procedure for Microsoft SQL access:
11.2015 Application AC500 MSSQL Library and Example 25
7.3 Communication
7.3.1 Generally
A typical SQL session open a TCP/IP connection to the server where the database is stored.
Actually, it is more correct to say that clients and SQL Server use Tabular Data Stream (TDS), but
TDS actually sits on top of TCP.
MS SQL Server listens for incoming connections on a particular port. The default port for MS SQL
Server is 1433.
When the client establishes a TCP/IP connection, a three-way handshake takes place. The client
opens a source port and sends traffic to a destination port, which is 1433 by default. The client
source port in use is random, but is greater than 1024.
The server (in this case, MS SQL Server) then communicates to the client by sending traffic from
1433 back to the port that the client established.
After that, the client send a TDS Login request to the server. The server answers with the ACK OK
or an error code if something wrong.
TDS Request
TDS Response
Client MS SQL
11.2015 Application AC500 MSSQL Library and Example 26
7.4 Function Blocks
7.4.1 MSSQL_Connect
Establish a connection to the MSSQL server via TCP/IP. The communication is using
Tabulator-Data-Stream (TDS) protocol.
Block Data
Available as of PLC runtime system: V2.4.x Remark:
Included in library: MSSQL_AC500_V24.lib
Block Type
Function block with historical values
Parameter
EN Input BOOL Enable function block by FALSE->TRUE
edge
IP Input STRING(15) IP address from the MS SQL server
PORT Input WORD Port number from the MS SQL server, default
port is 1433
PROPS Input STRUCT Some properties from the server connection for
example: username, password
CON In/Out POINTER Connection handle to the MS SQL server
DONE Output BOOL Execution finished when output DONE =
TRUE
ERR Output BOOL Error occurred during execution when output
ERR = TRUE
ERNO Output WORD Error code
SQL_ERNO Output WORD SQL Error code. Please check here for more
information.
11.2015 Application AC500 MSSQL Library and Example 27
Description
EN BOOL
The block is activated (EN = TRUE) or reset (EN = FALSE) via input EN. If the block is active and the
values at its block inputs are valid.
IP STRING(15)
IP address of the MS SQL server.
PORT WORD
Port number of the MS SQL server. The default port is 1433.
PROPS MSSQL_PROPERTIES_TYPE
Some properties for the MS SQL connection. See table below for more details.
PROPS MSSQL_PROPERTIES_TYPE
sUser STRING(50) Username for the SQL connection
sPassword STRING(50) Password for the SQL connection
sDatabase STRING(50) Database name (optional)
sDatabase STRING(50) Application name (optional)
sLibName STRING(50) Library name (optional)
sClientName STRING(15) Client name (optional). Default value is the IP address from
the PLC
CON zMSSQL_CONNECTION_TYPE
Structure to the connection handle. Is required by all other function blocks.
DONE BOOL
Output DONE indicates the processing state of the block. If the MS SQL connection was established or an
error has occurred, DONE is set to TRUE for one cycle. This output always has to be considered together
with output ERR. If ERR is TRUE, an error occurred. In this case, the error number can be read at output
ERNO.
ERR BOOL
Output ERR indicates whether an error occurred during block processing. This output always has to be
considered together with output DONE. If DONE is TRUE and ERR is TRUE, an error occurred. In this
case, the error number can be read at output ERNO.
11.2015 Application AC500 MSSQL Library and Example 28
ERNO WORD
Output ERNO provides an error identifier if an invalid value has been applied to an input or if an error
occurred during block processing. ERNO always has to be considered together with the outputs DONE and
ERR. The value output at ERNO is only valid if DONE is TRUE and ERR is TRUE. Encoding of the error
messages output at ERNO is explained at the end of this chapter.
SQL_ERNO WORD
The SQL_ERNO code which has been returned by the MS SQL server as a response of the send
command. Please check here for more information.
ERNO numbers
0x 201A Socket creation failed
0x 3013 A function block took too long and was terminated
0x 3014 Received ERR_TOKEN with MS SQL error from server
0x 3017 Receive buffer for the MSSQL responses too small
0x 3027 Invalid RESPONSE from server
0x4020 Invalid Input - Invalid IP address
0x 4033 Invalid buffer
0x 4034 Invalid buffer size - No size given
0x 4040 Invalid Input - No username given
10001, 10035 - 10071 Windows Sockets Error Codes. Please see here.
11.2015 Application AC500 MSSQL Library and Example 29
7.4.2 MSSQL_Execute
Executes the given MSSQL statement and returns the ResultSet(Query) or the
acknowledgment packet (NonQuery). A Query could be SELECT; a NonQuery could be
INSERT, DELETE and UPDATE etc. The FB will set the content of the received response
to the address DATA and the corresponding length. We can receive a maximum of 250
columns.
Block Data
Available as of PLC runtime system: V2.4.x Remark:
Included in library: MSSQL_AC500_V24.lib
Block Type
Function block with historical values.
Parameter
EN Input BOOL Enable function block by FALSE->TRUE
edge
STATEMENT Input STRING(500) Executable MSSQL statement
NB Input WORD Number of data to be read
DATA Input DWORD Address of the array where the response data
are stored
CON In/Out POINTER Connection handle to the MS SQL server
DONE Output BOOL Execution finished when output DONE =
TRUE
ERR Output BOOL Error occurred during execution when output
ERR = TRUE
ERNO Output WORD Error code
SQL_ERNO Output WORD SQL Error code. Please check here for more
information.
OKPACKET Output MSSQL_OKPAC Response to a NonQuery statement (INSERT,
KET_TYPE UPDATE, ...)
RESULTSET Output MSSQL_RESUL Response to a SELECT statement. Can be
TSET_TYPE read by MSSQL_GETVALUE
11.2015 Application AC500 MSSQL Library and Example 30
Description
EN BOOL
The block is activated (EN = TRUE) or reset (EN = FALSE) via input EN. If the block is active and the
values at its block inputs are valid.
STATEMENT STRING(500)
MSSQL statement that will be executed on server side. For example:
INSERT INTO Persons (FirstName,LastName,Address,City) VALUES (ABB,PLC,'Eppelheimer street
123','Heidelberg')
NB WORD
At input NB, the size of the array will be used to receive the response.
DATA DWORD
Address of the array of byte where the received data will be stored.
CON zMSSQL_CONNECTION_TYPE
Structure to the connection handle. Is required by all other function blocks.
DONE BOOL
Output DONE indicates the processing state of the block. If the statement was executed correctly or an
error has occurred, DONE is set to TRUE for one cycle. This output always has to be considered together
with output ERR. If ERR is TRUE, an error occurred. In this case, the error number can be read at output
ERNO.
ERR BOOL
Output ERR indicates whether an error occurred during block processing. This output always has to be
considered together with output DONE. If DONE is TRUE and ERR is TRUE, an error occurred. In this
case, the error number can be read at output ERNO.
ERNO WORD
Output ERNO provides an error identifier if an invalid value has been applied to an input or if an error
occurred during block processing. ERNO always has to be considered together with the outputs DONE and
ERR. The value output at ERNO is only valid if DONE is TRUE and ERR is TRUE. Encoding of the error
messages output at ERNO is explained at the end of this chapter.
11.2015 Application AC500 MSSQL Library and Example 31
SQL_ERNO WORD
The SQL_ERNO code which has been returned by the MS SQL server as a response of the send
command. Please check here for more information.
OKPACKET MSSQL_OKPACKET_TYPE
This structure keeps all response information’s about a NonQuery statement.
OKPACKET MSSQL_OKPACKET_TYPE
dwStatusFlags WORD Current Status
dwCurCMD WORD The token of the current SQL statement
dwDoneRowCount DWORD The count of rows that were affected by the SQL statement
(INSERT, UPDATE, DELETE, …)
RESULTSET MSSQL_RESULTSET_TYPE
This structure keeps all response information’s about a Query statement. This information’s can be read by
the MSSQL_GETVALUE function block.
RESULTSET MSSQL_RESULTSET_TYPE
iColumnsCount INT The number of columns. Maximum allowed 250.
iRowCount INT The number of Rows. Maximum allowed 3.
11.2015 Application AC500 MSSQL Library and Example 32
Erno numbers
0x 201A Socket creation failed
0x3002 BATCH command is not supported
0x3012 Not supported data type of more than 4 bytes
0x 3013 A function block took too long and was terminated
0x 3014 Received ERR_TOKEN with MS SQL error from server
0x 3017 Receive buffer for the MSSQL responses too small
0x3022 Not supported data type of more than 4 bytes
0x 3027 Invalid RESPONSE from server
0x3032 Not supported data type of more than 8 bytes
0x 3042 Not supported data type of 64 bytes
0x3052 Not supported data type column
0x4020 Invalid Input - No statement given
0x4030 Invalid Input - Invalid Socket Descriptor
0x4033 Invalid buffer address
0x4034 Invalid buffer size
0x 5117 We received more columns then we can store
0x5217 We received more rows then we can store
10001, 10035 - 10071 Windows Sockets Error Codes. Please see here.
11.2015 Application AC500 MSSQL Library and Example 33
7.4.3 MSSQL_Execute_Compact
Executes the given MSSQL statement and returns the ResultSet(Query) or the
acknowledgment packet (NonQuery). A Query could be SELECT; a NonQuery could be
INSERT, DELETE and UPDATE etc. We can receive a maximum of 50 columns and a
RESPONSE size from up to 2000 bytes.
Block Data
Available as of PLC runtime system: V2.4.x Remark:
Included in library: MSSQL_AC500_V24.lib
Block Type
Function block with historical values.
Parameter
EN Input BOOL Enable function block by FALSE->TRUE
edge
STATEMENT Input STRING(500) Executable MSSQL statement
CON In/Out POINTER Connection handle to the MS SQL server
DONE Output BOOL Execution finished when output DONE =
TRUE
ERR Output BOOL Error occurred during execution when output
ERR = TRUE
ERNO Output WORD Error code
SQL_ERNO Output WORD SQL Error code. Please check here for more
information.
OKPACKET Output MSSQL_OKPAC Response to a NonQuery statement (INSERT,
KET_TYPE UPDATE, ...)
RESULTSET Output MSSQL_RESUL Response to a SELECT statement. Can be
TSET_TYPE read by MSSQL_GETVALUE
11.2015 Application AC500 MSSQL Library and Example 34
Description
EN BOOL
The block is activated (EN = TRUE) or reset (EN = FALSE) via input EN. If the block is active and the
values at its block inputs are valid.
STATEMENT STRING(500)
MSSQL statement that will be executed on server side. For example:
INSERT INTO Persons (FirstName,LastName,Address,City) VALUES (ABB,PLC,'Eppelheimer street
123','Heidelberg')
CON zMSSQL_CONNECTION_TYPE
Structure to the connection handle. Is required by all other function blocks.
DONE BOOL
Output DONE indicates the processing state of the block. If the statement was executed correctly or an
error has occurred, DONE is set to TRUE for one cycle. This output always has to be considered together
with output ERR. If ERR is TRUE, an error occurred. In this case, the error number can be read at output
ERNO.
ERR BOOL
Output ERR indicates whether an error occurred during block processing. This output always has to be
considered together with output DONE. If DONE is TRUE and ERR is TRUE, an error occurred. In this
case, the error number can be read at output ERNO.
ERNO WORD
Output ERNO provides an error identifier if an invalid value has been applied to an input or if an error
occurred during block processing. ERNO always has to be considered together with the outputs DONE and
ERR. The value output at ERNO is only valid if DONE is TRUE and ERR is TRUE. Encoding of the error
messages output at ERNO is explained at the end of this chapter.
SQL_ERNO WORD
The SQL_ERNO code which has been returned by the MS SQL server as a response of the send
command. Please check here for more information.
11.2015 Application AC500 MSSQL Library and Example 35
OKPACKET MSSQL_OKPACKET_TYPE
This structure keeps all response information’s about a NonQuery statement.
OKPACKET MSSQL_OKPACKET_TYPE
dwStatusFlags WORD Current Status
dwCurCMD WORD The token of the current SQL statement
dwDoneRowCount DWORD The count of rows that were affected by the SQL statement
(INSERT, UPDATE, DELETE, …)
RESULTSET MSSQL_RESULTSET_TYPE
This structure keeps all response information’s about a Query statement. This information’s can be read by
the MSSQL_GETVALUE function block.
RESULTSET MSSQL_RESULTSET_TYPE
iColumnsCount INT The number of columns. Maximum allowed 50.
iRowCount INT The number of Rows. Maximum allowed 3.
11.2015 Application AC500 MSSQL Library and Example 36
Erno numbers
0x 201A Socket creation failed
0x3002 BATCH command is not supported
0x3012 Not supported data type of more than 4 bytes
0x 3013 A function block took too long and was terminated
0x 3014 Received ERR_TOKEN with MS SQL error from server
0x 3017 Receive buffer for the MSSQL responses too small
0x3022 Not supported data type of more than 4 bytes
0x 3027 Invalid RESPONSE from server
0x3032 Not supported data type of more than 8 bytes
0x 3042 Not supported data type of 64 bytes
0x3052 Not supported data type column
0x4020 Invalid Input - No statement given
0x4030 Invalid Input - Invalid Socket Descriptor
0x4033 Invalid buffer address
0x4034 Invalid buffer size
0x 5117 We received more columns then we can store
0x5217 We received more rows then we can store
10001, 10035 - 10071 Windows Sockets Error Codes. Please see here.
11.2015 Application AC500 MSSQL Library and Example 37
7.4.4 MSSQL_GetValue
This function block returns the required cell value (COLUMN, ROW) from the
ResultSet. ROW = 0 affords the column headers. (1, 0) returns the first column
header. COLUMN = 0 is invalid.
Block Data
Available as of PLC runtime system: V2.4.x Remark:
Included in library: MSSQL_AC500_V24.lib
Block Type
Function block with historical values.
Parameter
EN Input BOOL Enable function block by FALSE->TRUE
edge
RESULTSET Input MSSQL_RESULT Response to a SELECT statement.
SET_TYPE
COLUMN Input DWORD Requested column. Always >= 1
ROW Input DWORD Requested line. 0 returns the column headers
DONE Output BOOL Execution finished when output DONE =
TRUE
ERR Output BOOL Error occurred during execution when output
ERR = TRUE
ERNO Output WORD Error code
VALUE Output STRING(255) Requested cell value
NULL Output BOOL Cell value = NULL
11.2015 Application AC500 MSSQL Library and Example 38
Description
EN BOOL
The block is activated (EN = TRUE) or reset (EN = FALSE) via input EN. If the block is active and the
values at its block inputs are valid, the function block separates a value from the RESULTSET.
RESULTSET MSSQL_RESULTSET_TYPE
This structure keeps all response information’s about a Query statement.
COLUMN DWORD
Column of the requested value.
ROW DWORD
Row of the requested value.
DONE BOOL
Output DONE indicates the processing state of the block. If the statement was executed correctly or an
error has occurred, DONE is set to TRUE for one cycle. This output always has to be considered together
with output ERR. If ERR is TRUE, an error occurred. In this case, the error number can be read at output
ERNO.
ERR BOOL
Output ERR indicates whether an error occurred during block processing. This output always has to be
considered together with output DONE. If DONE is TRUE and ERR is TRUE, an error occurred. In this
case, the error number can be read at output ERNO.
ERNO WORD
Output ERNO provides an error identifier if an invalid value has been applied to an input or if an error
occurred during block processing. ERNO always has to be considered together with the outputs DONE and
ERR. The value output at ERNO is only valid if DONE is TRUE and ERR is TRUE. Encoding of the output
error message of ERNO is explained at the end of this chapter.
VALUE DWORD
The requested cell value will be displayed.
11.2015 Application AC500 MSSQL Library and Example 39
NULL BOOL
If a cell value is “NULL” then this output will be TRUE.
Erno numbers
0x4020 Invalid Input - ResultSet is not available or 0
0x4030 Invalid Input - Column > MAX_ALLOWED
0x4040 Invalid Input - Row is out of bounds
11.2015 Application AC500 MSSQL Library and Example 40
7.4.5 MSSQL_Disconnect
This function block is used to close the connection to the connected MSSQL server.
Block Data
Available as of PLC runtime system: V2.4.x Remark:
Included in library: MSSQL_AC500_V24.lib
Block Type
Function block with historical values.
Parameter
EN Input BOOL Enable function block by FALSE->TRUE
edge
CON Input POINTER Connection handle to the MS SQL server
DONE Output BOOL Execution finished when output DONE =
TRUE
ERR Output BOOL Error occurred during execution when output
ERR = TRUE
ERNO Output WORD Error code
11.2015 Application AC500 MSSQL Library and Example 41
Description
EN BOOL
The block is activated (EN = TRUE) or reset (EN = FALSE) via input EN. If the block is active and the
values at its block inputs are valid.
CON zMSSQL_CONNECTION_TYPE
Structure to the connection handle. Is required by all other function blocks.
DONE BOOL
Output DONE indicates the processing state of the block. If the connection was completely closed or an
error has occurred, DONE is set to TRUE for one cycle. This output always has to be considered together
with output ERR. If ERR is TRUE, an error occurred. In this case, the error number can be read at output
ERNO.
ERR BOOL
Output ERR indicates whether an error occurred during block processing. This output always has to be
considered together with output DONE. If DONE is TRUE and ERR is TRUE, an error occurred. In this
case, the error number can be read at output ERNO.
ERNO WORD
Output ERNO provides an error identifier if an invalid value has been applied to an input or if an error
occurred during block processing. ERNO always has to be considered together with the outputs DONE and
ERR. The value output at ERNO is only valid if DONE is TRUE and ERR is TRUE. Encoding of the output
error message of ERNO is explained at the end of this chapter.
Erno numbers
0x4020 Invalid Input - CON has no connection handle
10001, 10035 - 10071 Windows Sockets Error Codes
Please see here.
11.2015 Application AC500 MSSQL Library and Example 42
7.5 Example Program
The project contains one program with a few function blocks. The easiest way to control these function is to
use a step chain.
The first network established a connection to the server.
The second network execute a MSSQL request at the server and return the ResultSet.
The third network extract from the received ResultSet a value.
The fourth network disconnect from the server.
11.2015 Application AC500 MSSQL Library and Example 43
Please choose your Communication parameters. Go to Online --> Communication Parameters…
When you connect your PLC over Ethernet then insert the following settings:
- Address (IP address from the PLC)
- Port (set Port to 1201)
- Motorola byteorder (Yes)
11.2015 Application AC500 MSSQL Library and Example 44
7.6 Supported data types
The following table shows the supported data types.
DATA TYPE SIZE in byte RANGE SUPPORT
Bigint 8 -2^63 - 2^63-1
Signed 64-bit integer
Int 4 -2^31 - 2^31-1
Signed 32-bit integer
Smallint 2 -2^15 - 2^15-1
Signed 16-bit integer
Tinyint 1 0 – 255
Bit 1 0–1
Decimal 5-17 -10^38 +1 - 10^38 –1 Precision
depending max. 0-9
on precision
Numeric 5-17 -10^38 +1 - 10^38 –1.
depending
on precision
money 8 -2^63 - 2^63-1
Smallmoney 4 -214,748.3648 - +214,748.3647
Float 4-8 Float(n)
1 <= n <= 24 à 4 Byte
-3.40^38 to -1.18^-38
25 <= n <= 53 à 8 Byte
- 1,79^308 to -2,23^-308, 0 and
2,23^-308 bis 1,79^308
Real 4 -3.40^38 - -1.18E^-38, 0 and 1.18^-
38 - 3.40^38.
Date 3
Datetime 8 Date and time data from January 1,
1753, through December 31, 9999
Smalldatetime 4 Date and time data from January 1,
1900, through June 6, 2079
11.2015 Application AC500 MSSQL Library and Example 45
Char(X) Number of Fixed-length non-Unicode
X chars character data with a maximum
length of 255 characters.
Varchar(X) Number of Variable-length non-Unicode data
X chars with a maximum of 255 characters.
Text 16 Variable-length non-Unicode data
with a maximum length of 255
characters.
Nchar Number of Fixed-length Unicode data with a
chars maximum length of 255 characters.
Nvarchar(50) Number of Variable-length Unicode data with
50 chars. a maximum length of 255
Size is 100. characters.
Ntext 16 Variable-length Unicode data with
a maximum length of 255
characters.
timestamp 8 A database-wide unique number
that gets updated every time a row
gets updated.
supported Not supported Party supported
11.2015 Application AC500 MSSQL Library and Example 46
8 Testing Environments
8.1 Microsoft SQL Server 2008
We have tested the function blocks with Microsoft SQL Server 2008.
8.2 Microsoft SQL Server 2012
We have tested the function blocks with Microsoft SQL Server 2012.
11.2015 Application AC500 MSSQL Library and Example 47
9 Modification
Version Chapter Name Modification Date
V1.0 All TR First version 2015-06-29
V1.0.1 All TR Some small improvements 2015-10-23
V1.0.3 All TR Added new fb “MSSQL_EXECUTE_COMPACT” 2015-11-11
Modified fb “MSSQL_EXECUTE”
Added chapter Supported data types
11.2015 Application AC500 MSSQL Library and Example 48
Doc. kind: No. o. p.:
Helpline document 50
Title: Lang.:
AC500 MSSQL Library Application [Link] EN
Manual No. 3ADR025225M0201
abb
ABB Automation Products GmbH
Wallstadter Str. 59
D-68526 Ladenburg
Tel. : +49 62 21 / 701-1444
Fax : +49 62 21 / 701-1382
E-Mail : [Link]@[Link]
[Link]/plc