Sie sind auf Seite 1von 44

Work with Linked Servers

Contents
1. Create a Linked Server .............................................................................................. 3
1.1 Create a library on your IBM i .................................................................................. 3
1.2 Define a linked server............................................................................................. 4
1.2.1 IBM i Relational Database Directory Entries ......................................................... 8
1.3 Change a Linked Server object ................................................................................ 9
1.3.1 Make changes using the Linked Server Properties dialog ........................................ 9
1.3.2 Make changes using the Script Linked Server options .......................................... 11
1.4 Work with Tables in the Linked Server .................................................................... 14
1.5 Create a new database in SQL Server ..................................................................... 17
1.6 Work with the IBM i database from SQL Server ........................................................ 20
1.6.1 Use a SELECT INTO statement to populate a table in the SQL400 database form a
table on your IBM i ..................................................................................................... 20
1.6.2 Query the data in the SQL400.dbo.QCUST table ................................................. 21
1.6.3 Attempt a SELECT INTO from SQL Server to the IBM i......................................... 22
1.6.4 Create the target IBM i table, use INSERT INTO ................................................. 24
1.6.5 Run an SQL UPDATE statement from SQL Server to the IBM i............................... 32
1.6.6 Run an SQL DELETE statement from SQL Server to the IBM i ............................... 33
1.7 Work with a batch of SQL statements ..................................................................... 34
1.7.1 Create a T-SQL batch script ............................................................................. 34
1.7.2 Save the T-SQL batch script ............................................................................ 36
1.7.3 Run the T-SQL batch file from a Windows Command Prompt ................................ 39
1.7.4 Summary ..................................................................................................... 43

1
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
2
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1. Create a Linked Server
In SQL Server, a linked server is a persistent object that describes a connection to a database
located on another server. Because the connection to the linked server is created using an OLE DB
Provider, the other server can potentially be any type of data source.
In this lab, you will define and work with a linked server to your IBM i, using the IBM i Access for
Windows OLE DB Provider. You'll see how you can use database files on the linked server in the
SQL Server Management Studio.

1.1 Create a library on your IBM i


_____ Enter the following command on your IBM i to create the library ADVWORKS:

CRTLIB LIB(ADVWORKS) TEXT('SQL Server course test library')

_____ Use the following CRTDUPOBJ command on your IBM i to copy file QIWS/QCUSTCDT to library
ADVWORKS:

CRTDUPOBJ OBJ(QCUSTCDT) FROMLIB(QIWS) OBJTYPE(*FILE) TOLIB(ADVWORKS) DATA(*YES)

Note: if you use the CRTLIB command shown above, you also need to create a journal receiver, a
journal, and start journaling the physical file that you copy. In some of the steps used later in this
lab, the file is required to be journaled.
As an alternative, you can use the IBM i Navigator and create a new schema (an SQL-enabled
library). When you create a schema, journaling is automatically started for tables that are in the
schema.

_____ If you did not use the IBM i Navigator to create a schema, use the following commands to
create a journal receiver and a journal. You will start journaling for the database file later:

CRTJRNRCV JRNRCV(ADVWORKS/ADVWORKS)

CRTJRN JRN(ADVWORKS/ADVWORKS) JRNRCV(ADVWORKS/ADVWORKS)

3
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.2 Define a linked server
_____ Open the SQL Server Management Studio, if it is not already opened.
_____ In the Object Explorer, expand the SQL Server that you are connected to and locate the
Server Objects item and expand it.
_____ You can right-click either the Server Objects item, or right-click the Linked Servers item
(Figure 1). Select the item to create a New Linked Server.

sql02002 sql02003

Figure 1: You can right-click either the Server Objects or the Linked Servers menu item.
_____ The New Linked Server dialog is displayed (Figure 2). Be sure that General is the selected
page and that Other data source is selected.

sql02004

Figure 2: On the General page, you select the provider to use for the connection and set the library list.

4
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ Enter the following information on the General page. DO NOT CLICK THE OK BUTTON
AFTER ENTERING THIS INFORMATION.
Figure 3 is an example of what the New Linked Server dialog looks like after making the
required entries.
Item Value

Linked server Enter a name to assign to the linked server. Suggested value: the
TCP/IP host name of the IBM i that you are connecting to.

Provider IF YOU ARE USING iSeries Access for Windows V5Rx


Select the IBM DB2 UDB for iSeries IBMDA400 OLE DB
Provider from the drop-down list.
Note: the IBMDA400 provider is the preferred provider. You may
be able to use the IBMDASQL provider, although you may need to
be at the most recent Service Level for it to work correctly. DO NOT
select the IBMDARLA provider.

IF YOU ARE USING System i Access for Windows V6Rx


or IBM i Access for Windows V7Rx or later
You can select either the IBM DB2 for i5/OS IBMDA400 OLE DB
Provider or the IBM DB2 for i5/OS IBMDASQL OLE DB
Provider from the drop-down list.

Product name You can enter any value here. Suggested value:
IBMDA400 if you are using IBMDA400 OLE DB Provider.
IBMDASQL if you are using the IBMDASQL OLE DB
Provider.

Data source Enter the TCP/IP host name of the IBM i system that you are
connecting to.

Provider string Enter the following blank-separated words, the = sign and a
comma-separated list of libraries, followed by a closing semicolon:
Catalog Library List

Example:
Catalog Library List=ADVWORKS,QIWS;

Include the ADVWORKS library that you created earlier in


this lab. You can enter any other libraries on your IBM i that
you want. However, for your initial tests, you should limit the
number of libraries that you use.
Note: if you mistype a library name (or enter the name of a library
that does not yet exist on your IBM i), no error is indicated. The
linked server will be created with the library list for libraries that do
exist in the list that you specify.
Note: if you include a library that you are not authorized to, the
library appears in the linked server list of libraries. However, when
you try to access any file objects in that library through the linked
server, an error message is returned to SQL Server.

Catalog Leave this entry blank unless you created the test library
ADVWORKS in the non-default system Auxiliary Storage Pool (ASP).
If you created library ADVWORKS in a non-default ASP, enter the
name of the ASP as the value for Catalog.

5
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
sql020041

Figure 3: This is what the New Linked Server dialog looks like after you make the entries.
_____ After entering the information on the General page, click the Security item in the upper-
left corner of the New Linked Server dialog (Figure 4).

sql020042

Figure 4: On the Security page, you set the IBM i User ID and Password for the connection.

6
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ On the Security page (Figure 4), select the Be made using this security context option.
Enter your IBM i user ID for Remote login and enter your IBM i password for With
password.
_____ Click the OK button on the Security page. The linked server is created.
_____ You should now see the linked server that you defined in the list of Linked Servers, as
shown in Figure 5.
_____ Expand the linked server, then expand the Catalogs item. You should see an entry for
System Catalogs and another catalog name, which is the name of the local database entry
on your IBM i. The catalog name that you see will not be the same as the catalog
entry shown in the figure (IDEVCLD012). A description of the local database entry follows
this step.
When you expand the catalog name, you will see entries for Tables and Views. You can
expand both of those items to see the lists of database file objects that are accessible on
your IBM i via the linked server definition.
• The Tables list includes physical files.
• The Views list includes logical files and SQL views.

Note: the Tables and Views lists may include objects that you are not authorized to. When
you attempt to access data in an unauthorized object, an error message is sent from the
IBM i to the database provider that you used to create the linked server. You are not
allowed to access data in IBM i objects that you are not authorized to, even if the object
appears in the list of objects for the linked server.

sql02005

Figure 5: The linked server now appears in the list. Expand it to see the catalog and the tables that are in the
catalog. Note: the list of tables that you see on your system will be different from the list shown here.

7
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.2.1 IBM i Relational Database Directory Entries
By default, your IBM i is configured to support one database, using DB2 for IBM i. The default
database is referred to as the *LOCAL database. You can see the database configuration on your
IBM i by entering the following command on a 5250 command entry line:

WRKRDBDIRE

Figure 6 shows two relational database directory entries on an IBM i system. The *LOCAL entry is for
the database that is on the IBM i where you run the command. The database with remote location
value 10.1.1.4 points to a database on another IBM i.
There might be more than one database on your IBM i. Your system might be configured with more
than one auxiliary storage pool (ASP), in which case each ASP has its own database.
When you define a linked server, you specify the database to connect to using the Catalog field on
the General page (Figure 2). If you leave the Catalog field blank, the linked server connects to
the *LOCAL database. If you need to connect to a different database, enter the name in the Catalog
field.

sql020051

Figure 6: This is the WRKRDBDIRE display, showing the *LOCAL database and a remote database definition.

8
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.3 Change a Linked Server object
You may need to change one or more properties of a linked server object after you create it. Use
the steps shown in this section to change an existing linked server object.
1.3.1 Make changes using the Linked Server Properties dialog
_____ In the Object Explorer, right-click the linked server name and select the Properties item
from the pop-up menu, as shown in Figure 7.

sql02401

Figure 7: Right-click the linked server and select the Properties item.
_____ The Linked Server Properties dialog is displayed, as shown in Figure 8. You cannot make
any changes on the General panel. If you need to change any of the properties on this
panel, see the instructions later in this section.

sql02402

Figure 8: You cannot change any of the properties on the General panel.

9
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ In the left column, click the Security item. The Security panel shown in Figure 9 is
displayed.
_____ You can change properties on the Security panel for an existing linked server object.

sql02403

Figure 9: You can change properties on the Security panel for an existing linked server.
_____ In the left column, click the Server Options items. The Server Options panel shown in
Figure 10 is displayed.
_____ You can change properties on the Server Options panel for an existing linked server object.

sql02404

Figure 10: You can change properties on the Server Options panel for an existing linked server.

10
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.3.2 Make changes using the Script Linked Server options
When you created the linked server using the New Linked Sever dialog (see Section 0 starting on
page 3), behind the scenes, a CREATE command was run to create the linked server. You can use
the same command to create a new version of an existing linked server.
You can always look up the syntax of the CREATE command in the SQL Server Books Online. But
SSMS includes a shortcut that you can use: it generates the required CREATE statement, containing
the property values that define the current linked server.
_____ In SSMS, right-click the linked server that you create earlier.
_____ On the pop-up menu, select the Script Linked Server as, CREATE To menu items, as
shown in Figure 11. Do not click the New Query Editor Window item.

sql02411

Figure 11: You can use the Script Linked Server as, CREATE To, New Query Editor Windows items to create a
new linked server object based on an existing definition.
_____ On the same pop-up menus, another option is Script Linked Server as, DROP and
CREATE To, as shown in Figure 12.

sql02412

Figure 12: Use the Script Linked Serer as, DROP and CREATE To, New Query Editor Window to change the
definition of a linked server object.

11
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
CREATE To or DROP and CREATE To
SSMS will generate the script code required to create a linked server.
If you want to create a new linked server object that uses most of the same property
values as an existing linked server, use the CREATE To option. You can change the name
of the new linked server in the generated script.
If you want to change an existing linked server object, use the DROP and CREATE To
option. SSMS generates a DROP statement in the script to drop (delete) the existing linked
server object, followed by the CREATE statement used to create the new (revised) version
of the linked server.

_____ Select the Script Linked Server as, DROP and CREATE To, New Query Editor Window
menu items, as shown in Figure 12.
_____ SSMS opens a query editor window. It generates the script code required to drop and create
the linked server object, as shown in Figure 13.

sql02413

Figure 13: This shows the script code that is generated to drop and create the linked server object.

_____ You can change any of the property values, including the property values that are displayed
on the General tab and are not updateable in the Linked Sever Properties dialog (see Figure
8 on page 9).
For example, you might need to change the library list. You can make your changes to the
Catalog Library List property directly in the editor.

12
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
NOTE
When the script is generated, the password that was assigned to the linked sever object is
not generated into the script. Instead, it is replaced with a series of "x" characters, as
shown in the @rmtpassword property in Figure 13. The password is obscured so that users
who can access the SQL Server will not be able to determine the password used with the
linked server object.
If you plan on running the script to modify the existing linked server object, you need to
change the password value to the value for the user ID specified in the @rmtuser property.

_____ When done making your changes, you can click the Execute button as shown in Figure 14
or press the F5 key to run the script.
_____ If you do not want to make any changes, you can simply close the editor panel by clicking
the X close button on the right side of the tab (the circled item in Figure 14).

sql02414

Figure 14: When done, you can click the Execute button or press F5 to run the script.

13
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.4 Work with Tables in the Linked Server
_____ In the Tables list for your linked server, locate the ADVWORKS.QCUSTCDT item.
_____ Right-click the ADVWORKS.QCUSTCDT item and select the Script Table as, SELECT To,
New Query Editor Window menu items, as shown in Figure 15.

sql02006

Figure 15: Use the Script Table as feature to generate a SELECT statement for a table in the linked server.

_____ A SELECT statement for the table is generated in the SSMS Query window, as shown in
Figure 16. Note that the SELECT statement that is generated includes only the columns in the
table that contain character data. Also note that if you hover the mouse over the 4-part
table name, the "invalid" message shown in the figure is displayed.
Note: the tab headers that are displayed on your PC (SQLQuery5.sql...) will be different.
It does not matter what the tab header names are; the headers are automatically generated
when a new tab opens.

sql02007

Figure 16: A SELECT statement for the table is generated in the Query window. The table name is flagged as
being invalid.

14
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ If you hover the mouse over any of the column names, the column is also flagged being
invalid (Figure 17).

sql020071

Figure 17: The column names will also be flagged as invalid.

_____ Click the Execute button to run the SELECT statement. The results are displayed in the
Results panel, as shown in Figure 18.

sql02008

Figure 18: Click the Execute button to run the SELECT query. The results are displayed in the Results panel.

15
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ In the Query tab, change the SELECT statement. Replace the explicit column name list with a
simple SELECT * (leave the FROM clause as it is).
_____ Now click the Execute button to run the revised query. You should see data for all of the
columns in the table, as shown in Figure 19.

sql02009

Figure 19: When you run a SELECT * query, you see data for all of the columns in the table.

Observation: although SQL Server initially limits the Linked Server query to select only data in
character fields, you can access all of the columns in a Linked Server table by using a SELECT *
query.

However, you will almost always want to explicitly specify the columns that you want to retrieve in
a SELECT statement, rather than use the SELECT * query. Using explicit column names in the SELECT
is one of the first things you should do to optimize a query. Keep in mind that all of the data that
you select will be sent over a network connection from your IBM i to SQL Server. Anything that you
can do to limit the amount of data that is transmitted will improve the response time.

16
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.5 Create a new database in SQL Server
In the next series of tests, you will create a new database in SQL Server. You will then run SQL
statements through the linked server to your IBM i. The SQL statements will create a table in the
SQL Server database and fill it with data from your IBM i.
_____ In the SQL Server Management Studio, right-click the Databases item and select the New
Database item, as shown in Figure 20.

sql02010

Figure 20: Select the New Database item to create a new SQL Server database.

_____ On the New Database dialog, General page (Figure 21), enter SQL400 as the Database
name value. Leave all of the other options set to their defaults. DO NOT CLICK THE OK
BUTTON.

sql02011

Figure 21: Enter the value SQL400 as the database name.

17
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ Click the Options item on the New Database dialog to display the options page, as shown
in Figure 22.
_____ If you are working in a mixed environment of SQL Server 2012 and earlier version of SQL
Server (SQL Server 2008 or SQL Server 2005), you may want to set the Compatibility
level of the database to allow you to save and restore the new database to prior versions of
SQL Server. You should review the information about compatibility level in the SQL
Server Books Online before changing the setting.
_____ Leave all of the other options set to their default values. Click the OK button to create the
database.

sql02012

Figure 22: If you work with SQL Server 2008 or SQL Server 2005 databases, you can set the compatibility
level to allow the new database to work with the prior versions.

18
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ You should see the new SQL400 database in the list of databases, as shown in Figure 23. If
you do not see the database, review the previous steps to create the database. You can also
click the Databases item, then press the F5 key to refresh the list of databases, or use the
View, Refresh menu items to refresh the list.

sql02013

Figure 23: You can now see the SQL400 database in the list of databases.

19
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.6 Work with the IBM i database from SQL Server
In this section, you'll work with several techniques using the linked server. You will run SQL
commands in SQL Server that use data from your IBM i and affect data on your IBM i.
1.6.1 Use a SELECT INTO statement to populate a table in the SQL400 database form a
table on your IBM i
_____ In the SQL Server Management Studio, click the New Query button to open a new query
window, as shown in Figure 24 (at ).
_____ Enter the following statements into the new query window (at )

use SQL400;
go

select cusnum, lstnam, init, street, city, state, zipcod


into SQL400.dbo.QCUST
from linked_server_name.catalog_name.ADVWORKS.QCUSTCDT;
go

where linked_server_name is the name of your linked server and catalog_name is the name
of the catalog that is displayed under the linked server.

For example, in Figure 5 on page 7 the linked_server_name is IDEV and the catalog_name is
IDEVCLD012. Enter the corresponding values that are displayed in your SSMS for your linked
server.

Note: when you enter SQL400.dbo.QCUST, SQL400 is the name of the SQL Server database
where the table is created. dbo.QCUST is the schema-qualified name of the table, where dbo
is the default schema and QCUST is the table name. In versions of SQL Server prior to SQL
Server 2005, dbo was the "owner name" (it stands for "database owner").



sql02014

Figure 24: Click the New Query button to open a new query panel and enter the SELECT INTO statement.

20
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ After entering the statements, click the Execute button (at ) in SSMS to run the query.
• If the query runs successfully, you will see a message stating how many rows were
affected (copied from your IBM i to the new SQL Server table).
• If the query does not run successfully, you will see one or more error messages. Make
the required corrections to the SELECT statement and run the query again.
_____ Now expand the SQL400 database and expand Tables. You should see the new table
dbo.QCUST, as shown in Figure 24 (at ).

_____ Expand the dbo.QCUST table and expand Columns. You should see the list of columns in the
table, as shown in Figure 24.
1.6.2 Query the data in the SQL400.dbo.QCUST table
_____ Right-click the dbo.QCUST table and select the Select Top 1000 Rows item, as shown in
Figure 25.
_____ SQL Server generates the SELECT statement shown in the query panel and runs the query,
displaying the results in the Results panel.

sql02015

Figure 25: You can now run a query against the data in the SQL400.dbo.QCUST table. You will see the data
that was copied from the IBM i to the SQL Server table.

21
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.6.3 Attempt a SELECT INTO from SQL Server to the IBM i
In the previous section, you saw that you could use a SELECT INTO SQL statement to create and
populate a table in the SQL Server database, based on a table in the IBM i database. In this
section, you'll attempt to perform the reverse: create a table in the IBM i database based on a
SELECT INTO statement that runs against the SQL Server database.

Based on the title of this section, you’ll find that this technique does not work. However, it is useful
to follow through with the steps shown in this section so that you will see the errors that are
generated. In the following sections, you'll use techniques that provide an equivalent capability
(the ability to create a table on the IBM i from SQL Server, then populate that table with data from
SQL Server).
_____ Open a new query window (click the New Query button).
_____ Enter the following statements into the new query window. The statement is a mirror-image
of the statement that you used in the previous section to create the table in SQL Server:

select cusnum, lstnam, init, street, city, state, zipcod


into linked_server_name.catalog_name.ADVWORKS.QCUST
from SQL400.dbo.QCUST;
go

where linked_server_name is the name of your linked server and catalog_name is the name
of the catalog that is displayed under the linked server.
_____ Click the Parse button as shown in Figure 26. The SQL statement is parsed and the error
message shown in the figure is displayed.

sql02101

Figure 26: When you click the Parse button, the SQL statement(s) in the query window are parsed and any
errors are displayed in the Results panel.

22
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ The error message indicates that the linked server database name contains more than the
maximum number of prefixes.
_____ You might think that you could omit the server_name part of the linked server database
name, and use a statement similar to that shown in Figure 27. In this case, when you click
the Parse button, the statement will parse successfully. However, when you click the
Execute button, you get the error message shown in the figure.

sql02102

Figure 27: If you use a 3-part name, you get the "database does not exist" error.

23
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.6.4 Create the target IBM i table, use INSERT INTO
It appears that the SELECT INTO technique (which creates the target table, then inserts data into it)
will not work for a new table on a linked server. Instead of trying to combine the table create and
insert into one operation, you'll see how you can split the task into two steps that will work:
1. Create the table.
2. Use an SQL INSERT INTO statement to get data from the SQL Server table into the IBM i
table.
_____ To get started, you need the Data Definition Language (DDL) statement to create the table.
You can use SQL Server to generate the DDL statement based on the existing table in the
SQL Server database.
_____ Locate the SQL400.dbo.QCUST table in and right-click it. Select the Script Table as, CREATE
To, New Query Editor Window menu items, as shown in Figure 28.

sql02104

Figure 28: Use the Script Table as, CREATE to, New Query Editor Window item to generate the CREATE TABLE
DDL.

24
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ SQL Server generates the statements shown in Figure 29. The CREATE TABLE statement is a
valid statement that will create the table in the SQL Server database.

sql02105

Figure 29: SQL Server generates a valid CREATE TABLE statement that will create the table in a SQL Server
database.

25
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ Because you want to create the table on your IBM i, you might think that you can enter the
4-part linked server identifier as shown in Figure 30 to create the table. However, when you
parse the statement, you get the error about the maximum number of prefixes again.

sql02106

Figure 30: If you attempt to create the table on the linked server, you get an error message.

26
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ Although you can't run the CREATE TABLE statement that was generated by SQL Server, you
can use that statement as the basis for another statement that will run on your IBM i.
_____ In the query window shown in Figure 30, use your mouse to select all of the code starting
with CREATE TABLE down through the closing right parenthesis character (on the ON
[PRIMARY] clause). When the statement is selected, use the Edit, Copy menu item (or press
Ctrl-C) to copy the statement.

_____ Click the New Query button to open a new query window.
_____ Open a new query window and paste the CREATE TABLE statement that you copied into it
(Edit, Paste or Ctrl-V).
_____ Change the statement so that it looks like the following, as shown in Figure 31:

EXEC ('CREATE TABLE ADVWORKS.QCUST (


cusnum numeric (6, 0) NOT NULL,
lstnam varchar (8) NOT NULL,
init varchar (3) NOT NULL,
street varchar (13) NOT NULL,
city varchar (6) NOT NULL,
state varchar (2) NOT NULL,
zipcod numeric (5, 0) NOT NULL)'
) at linked_server_name;

For linked_server_name, substitute the name of the linked server to your IBM i (only the first
part of the name, as shown in the figure).

Note: the EXEC statement uses opening and closing parentheses characters, and everything
inside those parentheses is embedded within opening and closing single-quote characters:

EXEC ('something_to_do') at linked_server_name;

Note: if library ADVWORKS is in a non-default ASP on your IBM i (in SQL Server terms, in a
non-default database), enter a 3-part identifier for the table name:
EXEC (‘CREATE TABLE catalog_name.ADVWORKS.QCUST (

CHECK YOUR SYNTAX!

sql02107

Figure 31: Use the EXEC statement to perform a pass-through operation to the linked server.

27
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ Click the Parse button to parse the EXEC statement. You should see the Command(s)
complete successfully message, as shown in Figure 31.
_____ If the parse reports any errors, correct the statement until you can get it to parse
successfully.
_____ If the parse is successful, click the Execute button. The results panel displays the error
message shown in Figure 32 (Server 'linked_server_name' is not configured for RPC.). RPC
stands for Remote Procedure Call. It needs to be enabled to support the EXEC statement that
you are trying to run in SQL Server.

sql021071

Figure 32: When you click the Execute button, you get the error message shown here.

_____ If the ADVWORKS library was not created in IBM i Navigator as a schema, you need to start
journaling for the database file:

STRJRNPF FILE(ADVWORKS/QCUST) JRN(ADVWORKS/ADVWORKS)

28
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ To enable RPC for the linked server, right-click the linked server name and select the
Properties item, as shown in Figure 33.

sql021072

Figure 33: Right-click the linked server name and select the Properties item.
_____ On the Linked Server Properties dialog (Figure 34), click the Server Options item.
_____ Click the RPC Out property. A drop-down list selector will appear. Click the drop-down
selector and select the value True from the list that appears. Be sure you select the RPC
Out property, not the RPC property.
_____ Click the OK button to close the Linked Server Properties dialog.

sql021073

Figure 34: In the Server Options page, set the RPC Out value to True.

29
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ Back in SSMS, be sure the query window with the EXEC statement is the current window.
Click the Execute button. The results panel will show the following message if the
command runs successfully:

(0 row(s) affected)

_____ Go to a 5250 command entry line and run a DSPLIB command to display the contents of the
ADVWORKS library on your IBM i. You should see the new file QCUST in the library.

_____ If you do not see file ADVWORKS/QCUST, review the steps shown above and rerun them as
necessary.
_____ If you see file ADVWORKS/QCUST on your IBM i, use the Display File Field Description (DSPFFD)
command to view the field definitions for the file. You should see the field names, data
types and lengths that correlate with the CREATE TABLE statement.

DSPFFD ADVWORKS/QCUST

_____ Open a new query window and enter the following INSERT INTO statement, as shown in
Figure 35:

INSERT INTO linked_server_name.catalog_name.ADVWORKS.QCUST


(cusnum, lstnam, init, street, city, state, zipcod)
SELECT cusnum, lstnam, init, street, city, state, zipcod
FROM SQL400.dbo.QCUST

where linked_server_name is the name of your linked server and catalog_name is the name
of the catalog that is displayed under the linked server.

sql02108

Figure 35: Enter the INSERT INTO statement to select data from SQL Server and insert it into the table on
your IBM i.

30
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ Click the Parse button to parse the INSERT INTO statement. Correct any errors that are
reported.
_____ Click the Execute button to run the INSERT INTO statement. You should see a message
reporting the number of row affected, as shown in Figure 35.
_____ On a 5250 command, enter the following Display Physical File Member command (DSPPFM):

DSPPFM ADVWORKS/QCUST

_____ Press F10 to display hexadecimal data, then F11 to display the over-and-under display. You
will see the data displayed as shown in Figure 36.

sql02109

Figure 36: The QCUST table, displayed using the hexadecimal over-and-under view of the DSPPFM command.
Note the 2-byte length fields that precede each of the character fields.

_____ As you can see in Figure 36, each of the character fields is preceded by a 2-byte "length"
field that indicates the number of bytes in the field. This is because each of the character
fields was defined as a varchar field type (see Figure 31, where you use the CREATE TABLE
statement that was generated by SQL Server).

31
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ If you do not want to work with varchar fields on your IBM i, delete file ADVWORKS/QC UST.
Return to the SQL CREATE TABLE statement (Figure 31) and change each of the varchar data
types to the char data type. You can leave all of the remaining code in the CREATE TABLE
statement as it is.
_____ If you change the data type, run the EXEC statement again to recreate file ADVWORKS/QCUST.
After you recreate table, run the INSERT INTO statement again, then review the new version
of ADVWORKS/QCUST with the DSPPFM command.

1.6.5 Run an SQL UPDATE statement from SQL Server to the IBM i
The INSERT INTO statement that you ran in the preceding section shows a pattern that you can use
to run the two other SQL Data Manipulation Language (DML) statements: UPDATE and DELETE.
DDL statements (such as CREATE TABLE) run within an EXEC statement.
In this section, you'll run an UPDATE statement in SQL Server that affects the ADVWORKS/QCUST table
on your IBM i.
_____ Refer to the DSPPFM display of the ADVWORK/QCUST database file (see Figure 36). The value of
the CITY field for the first row (CUSNUM 938472) is Dallas. You will change the city value of
that row.
_____ Now go back to the SQL Server Management Console and open a new query window.
_____ Enter the following SQL UPDATE statement into the new query window:

UPDATE linked_server_name.catalog_name.ADVWORKS.QCUST
set city='PARIS'
where cusnum=938472

where linked_server_name is the name of your linked server and catalog_name is the name
of the catalog that is displayed under the linked server.
_____ Click the Parse button to parse the UPDATE statement. Correct any errors that are reported.
_____ Click the Execute button to run the statement. You should get a message reporting that 1
row was affected.

32
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ Run the DSPPFM command again for the ADVWORKS/QCUST database file. You should see that
the CITY value was changed for the CUSNUM value that you specified, as shown in Figure 37.

sql02019

Figure 37: Verify that the CITY value was changed for the selected customer.

1.6.6 Run an SQL DELETE statement from SQL Server to the IBM i
To finish out your tests of using DML statements from SQL Server to your IBM i, you will run an
SQL DELETE statement.
_____ Go back to the SQL Server Management Console and open a new query window.
_____ Enter the following SQL DELETE statement into the new query window:

DELETE FROM linked_server_name.catalog_name.ADVWORKS.QCUST


where cusnum=938472

where linked_server_name is the name of your linked server and catalog_name is the name
of the catalog that is displayed under the linked server.
_____ Click the Parse button to parse the DELETE statement. Correct any errors that are reported.
_____ Click the Execute button to run the statement. You should get a message reporting that 1
row was affected.
_____ Run the DSPPFM command again for the ADVWORKS/QCUST database file. You should see that
the row that you specified for deletion is no longer in the table.

33
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.7 Work with a batch of SQL statements
Although it is useful to be able to enter and run statements in the SQL Server Management Studio,
it is unlikely that you will always want to enter and run statements manually. It is more likely that
you will want to save previously written SQL statements so that you can easily run them again.
1.7.1 Create a T-SQL batch script
In this section, you'll create a batch of Transact SQL (T-SQL) statements. T-SQL is the name
Microsoft uses for their SQL language environment. "Batch" in this sense simply means one or
more T-SQL statements that are run, it does not imply batch processing as in the IBM i
environment.

_____ In the SQL Server Management Studio, open a new query window.
_____ Enter the following sequence of T-SQL statements into the query window. Pay particular
attention to the use of semicolons at the end of each T-SQL statement. Do not
enter a semicolon after the "go" commands.

DELETE FROM linked_server_name.catalog_name.ADVWORKS.QCUST;


go

INSERT INTO linked_server_name.catalog_name.ADVWORKS.QCUST


(cusnum, lstnam, init, street, city, state, zipcod)
SELECT cusnum, lstnam, init, street, city, state, zipcod
FROM SQL400.dbo.QCUST;

go

where linked_server_name is the name of your linked server and catalog_name is the name
of the catalog that is displayed under the linked server.
_____ Click the Parse button to validate the batch. If there are any errors in the batch, correct
them and run the Parse again.
_____ Now click the Execute button. You should get two messages, indicating that each step was
successfully completed, as shown in Figure 38.

34
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
sql02121

Figure 38: This is the result of running the "batch" of statements.


_____ Run the DSPPFM command for the ADVWORKS/QCUST database file on your IBM i. It should
contain the data from your SQL400.dbo.QCUST table on SQL Server.

35
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.7.2 Save the T-SQL batch script
_____ Now that you've coded and tested the T-SQL batch, you can save it.
_____ Create a directory on your PC where you can save T-SQL batches ("scripts") that you
create. The directory that is shown in the examples in this course is c:\SQLScripts. You can
use that directory name or any other name of your choosing.
_____ In SQL Server Management Studio, be sure that the query window containing your T-SQL
batch is the currently selected query window.
_____ Use the File, Save As... menu item as shown in Figure 39 to save the contents of the
current query window. Note: the name shown in the Save As menu item will vary from
that shown in the figure, depending upon the name of the query window (shown in the tab).
It does not matter what the name of the tab is.

sql02122

Figure 39: Use the File, Save As... menu item to save your T-SQL script file.

36
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ A conventional Save File As dialog is displayed (Figure 40). Navigate to the directory you
created for saved scripts. Enter a file name for the saved script. The name used in the
example is Load_ADVWORKS_QCUST.sql.
There is no particular benefit to using a short, cryptic name for a script. The idea is
that you want to be able to recognize the function of a script file based upon the name you
assign to it. The conventional file extension for T-SQL batch files is .sql. Use that extension.

sql02123

Figure 40: Enter a name for your script. Use the file name extension .sql.
_____ Click the Save button to save the T-SQL batch file. When you return to the SQL Server
Management Studio, you will see that the tab on the query window now displays the name
that you assigned to the saved script file.

37
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ In the SQL Server Management Studio, be sure that the current query window contains the
batch that you just saved.
_____ On the right side of the tab for the query window, locate and click the close icon (the "X"),
as shown in Figure 41.

sql02124

Figure 41: Click the Close button on the right side of the query window to close the window for the script.

_____ You can also close the other open query windows if you want to. When you are prompted to
save the contents of those windows, you can accept or reject the offer to save.
_____ In the SQL Server Management Studio, use the File, Open, File menu item as shown in
Figure 42.

sql02125

Figure 42: Use the File, Open, File... menu item to open your saved .sql script file.

_____ Navigate to the directory where you saved your T-SQL script file and open it. You should see
it loaded into a query window. Once it is in the query window, you can click the Execute
button to run it again.

38
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.7.3 Run the T-SQL batch file from a Windows Command Prompt
You now have a T-SQL batch file that is saved to a directory on your PC. If you used the suggested
file name extension .sql for the batch file, you can double-click on the file in Windows Explorer.
The file opens in SQL Server Management Studio. Although you can always run the batch file from
within the Management Studio, it might be more useful to be able to run the T-SQL batch file
independently of the Management Studio.
SQL Server provides the sqlcmd command to interact with SQL Server from the Windows Command
Prompt environment. When you use sqlcmd, you can run commands and T-SQL batches without
opening the Management Studio.

_____ On your PC, open a Windows Command Prompt window (it is in the Accessories program
group).
_____ Enter the following command in the Command Prompt window:

sqlcmd

_____ You should see the 1> command prompt as shown in Figure 43.

_____ If you get the 1> command prompt, enter the word exit and press Enter to end the sqlcmd
command.

sql02131

Figure 43: If the SQLCMD command runs, you will get the prompt as shown in this figure.

_____ If you did not get the command prompt when you entered the sqlcmd command, use the
steps shown on the next page.

39
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ To run the sqlcmd command you may need to specify the name of your SQL Server and its
instance name. To view those names, go to the Management Studio and click the New
Query button to open a new query editing panel.
_____ In the query panel, enter the following statements, then click the Execute button:

select @@SERVERNAME;
select SERVERPROPERTY('InstanceName');

_____ You should see two results panels as shown in Figure 44. The top panel contains the name
of the SQL Server, specified as server_name\instance_name. The bottom panel contains the
value SQLEXPRESS which is the name of the SQL Server instance.

sql02132

Figure 44: You can view the Server Name and Instance Name in the Management Studio.

40
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ (SQL Server Express) Now that you know the server name and instance name, try running
the SQLCMD command as shown in Figure 45.
Note: use a lower-case "s" for the -s parameter.

sqlcmd –s server_name\instance_name

sqlcmd –s \instance_name

sql021321 sql021322

Figure 45: Use the -s parameter to specify the server_name\instance_name or just the \instance_name.

_____ (SQL Server Enterprise Evaluation Edition) If the value for the Instance Name panel shown
in Figure 44 is NULL, enter the server name as the value following the –s parameter, as
shown in Figure 46.

sql0213221

Figure 46: Use the -s parameter and specify the server_name.

_____ The sqlcmd command can be used to run the T-SQL batch file that you created. The syntax
for the command is:

sqlcmd -s server_name\instance_name -i c:\script_file_name.sql

where

-S identifies the following values as the server name and instance name

server_name is the name of the SQL Server where the T-SQL statements
are to be run

instance_name is the instance of the SQL Server where the T-SQL statements
are to be run.

-i identifies the following value as the name of the input file (the T-SQL batch file).
The -i is case-sensitive, you must enter it as -i

c:\script_file_name.sql is the complete path and file name of the T-SQL batch file

41
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
_____ Enter the sqlcmd command to run the T-SQL batch file.
Figure 47 and Figure 48 are examples of running the command in SQL Server Express.
Figure 49 is an example of running the command in SQL Server Evaluation Edition.
Note: if your SQL Server instance name is NULL, do not specify it for the server name
parameter. Just enter the –s parameter and the server name, without a leading or trailing \
character, as shown in Figure 49.

sql021323

Figure 47: This is an example of running the SQLCMD command in SQL Server Express, using just the
instance name.

sql021324

Figure 48: This is an example of running the SQLCMD command in SQL Server Express, using the server
name and the instance name.

sql021325

Figure 49: This is an example of running the SQLCMD command in SQL Server Enterprise Evaluation Edition,
using just the server name.

42
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
1.7.4 Summary
You've now seen how you can develop and test a batch of T-SQL statements using the
Management Studio. You've also seen that you can save the T-SQL batch file to your PC and run it
using the sqlcmd command.
You can use the sqlcmd command with other programs that are available to you. For example, you
can use the Windows Scheduled Tasks program (in the Control Panel program group) to run the
T-SQL batch file using the sqlcmd command on a schedule.
If you are using the SQL Server Express edition, you are now done with this lab. If you are using
another edition of SQL Server, you can continue with the next section, where the SQL Server Agent
is used to schedule and run the statements that are in the T-SQL batch file.

43
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED
44
Work with Linked Servers
Copyright © 2014, Craig Pelkie, ALL RIGHTS RESERVED

Das könnte Ihnen auch gefallen