Download You can also connect to SQL Server using MS Access.

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Database wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

Microsoft Access wikipedia , lookup

SQL wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
IS475/675
Creating Readable Output from SQL Server 2012 via MSAccess 2010
The SQL Server environment is used to write and test efficient and effective queries. It is a DBMS
that is designed to access data quickly and effectively; it is not a front-end data access tool. A good
way to create a nicely formatted report is to direct the output from SQL Server into a format that is
readable by another software package capable of producing nicely structured reports, such as
Microsoft’s Access database management system. This handout describes creating a simple stored
procedure and using an MS Access project to connect to SQL Server to view the stored procedure’s
output. The handout assumes familiarity with both SQL Server 2012 and Microsoft Access 2010.
Stored Procedures
A stored procedure is a type of query that is pre-compiled and saved on the database server. Stored
procedures have many capabilities not found in simple queries, such as the ability to create explicit
programming loops and detailed conditional logic through a programming language called
“Transact-SQL” in the MS SQL Server environment. In addition, they run very fast and are stored
in your database for central access and re-use. Stored procedures include both standard SQL and
custom code specific to a given DBMS vendor, such as Microsoft.
Creating a Stored Procedure
Sign on to SQL Server Management Studio as you normally do and open your database.
1. Click on “New Query” and create a stored procedure using the following code.
CREATE PROCEDURE PutProcedureNameHere
AS
SELECT * FROM tblEmployee
GO
In the place above where it says – PutProcedureNameHere – name your stored procedure. Do not
use spaces in the name of the stored procedure. Where it says – SELECT * FROM tblEmployee,
place the code that generates a result table you want to view in MS Access.
2. Press the Execute tool bar button. You will receive the message, ‘Command(s) completed
successfully.’ if your syntax is correct, or an error message if there are problems. The
‘success’ message signifies your stored procedure has been successfully created and saved.
3. To validate that your stored procedure was created, press the F5 key to “refresh” your
Object Explorer window. Under your database name, expand the Programmability set of
database objects. Now expand the Stored Procedures set of objects. You should see a
database object (dbo) called the name you put into the area “PutProcedureNameHere.”
Since SQL Server has saved your stored procedure, you do not have to do or save anything else.
Displaying Your Stored Procedure Output in MS Access
This part of the handout describes setting up a Project which is an MS Access database container
that gets its data and queries from a SQL Server database. Once you have set up this project file,
you can re-use it as often as necessary whenever you want to run and view the output from your
stored procedures.
Launch MS Access and create a new Access project using the file table and New option. No matter
where you create your new project you have to use the Browse icon in order to
specify that is a Microsoft Office Access Project. Don’t hit the “create” button directly – browse
for a file so that you are able to change the type of container (database vs. project) created by MS
Access. The Browse icon is located on the right side of the screen where it says “Blank Database”.
Click on the file folder next to the File_Name input box. (See below)
You should now see the screen displayed below:
Name the project anything you like and locate it in a directory where you have read/write access.
Save it as a Microsoft Office Access Projects file type. There will be a .adp extension placed on your
filename. This is really important – you must save it as a project, not a database. You must have
the .adp extension.
Click on the Create button after naming the project. When you Create the project, you will then be
presented with a dialog box asking, “Do you want to connect to an existing SQL Server database?”
Answer Yes. A dialog box similar to the one below will be displayed. (Your dialog box will have
empty information.)
Fill out the dialog as shown, except use your own database name in step 3 shown below. (currently
it says “YourDatabaseName” and you must replace that with the name of your database on SQL
Server (the name of your database
is your NetID). You can use the
Test Connection button to make
sure all your parameters are
correct.
After pressing the OK button, the
database container will appear as
shown at the bottom of this page.
Go to the Queries section in the
Objects area of the database and
find your stored procedure (we
called it ConnectToAccess in
class). Double click the stored
procedure to run it and it will
display a grid you can copy and
paste into Word or Excel. The
grid will be fairly well formatted
and contain column headers.
Data formatted as currency will
include dollar signs, commas and
decimal points.
Altering and Re-running Stored Procedures
You can change stored procedures that have been saved in your database and re-run them at will.
To alter a stored procedure:
1. Expand the Treeview control in SQL Management Studio starting from your database to
Programmability|Stored Procedures.
2. Right click your stored procedure and select Modify.
3. The saved stored procedure should look something like this:
SQL Server inserts a number of statements and changes the ‘CREATE PROCEDURE’ statement
to an ‘ALTER PROCEDURE’ statement. You do not have to worry about the statements that
SQL Server inserted or changed and you only have to change the actual SQL query that you wrote
earlier. When you Execute this query, SQL Server will replace the prior stored procedure with the
new one you configured.
Re-opening Your MS Access File
The MS Access project file you created earlier can be used indefinitely whenever you want to run
your stored procedures. You DO NOT have to create another project on MS Access. MS Access
2010 by default displays two security warnings when you open an existing file. To re-open the file
you previously created:
1. When asked to block unsafe expressions, answer No.
2. When asked whether you want to open a file that may contain harmful code, respond with
Open.
Please Note:
When you create an object like a stored procedure, it won’t display in the SQL Server Object
Explorer window or in the MS Access object area until you refresh the data using the F5 button.
Sometimes you have to back out of the area where the object should be displayed and if worse
comes to worse, you may have to quit the application and restart. You have to Refresh (F5)
stored procedures in both SQL Server and MS Access after you’ve made changes to them.