Monday 9 July 2012

Lesson05: SQL Server - SQL Scripts


In the previous lesson, we added data to our database table using the "Edit Top 200 Rows" option. In this lesson, we will look at how to write SQL scripts to update and run queries against our database.

Create a New Query

Before we generate our SQL script, we need somewhere to enter it into. This part is easy. Just click the "New Query" button:

Screenshot of the New Query button

A blank, white sheet should now appear on the right pane.

Write/Run Your SQL Script

You are now ready to write SQL queries against your database. You can use this interface to create database objects (such as databases, tables, views etc), insert data into a database table, select data, update data, delete data.
To run an SQL query:
  1. Type your query into the workspace on the right pane
  2. Click "Execute" (you can also press F5)
The following screenshot shows an example of using a SQL 'select' statement to select data from a database:


Screenshot SQL query results



As you can see, the results of the query are displayed in the bottom pane.
As a shortcut, you can click on a table in the left pane and drag it to the right pane when building your query. This can save you time - especially if you have many tables to add to your query.

The above 'select' statement is an example of a SQL query. Apart from the occasional example, SQL queries are outside the scope of this tutorial. If you'd like to learn more about writing SQL queries, check out the SQL tutorial.

Database Administration Tasks

Most of the database administration tasks that can be performed in SSMS via the graphical user interface can be performed programatically via SQL scripts. This tutorial concentrates on using the graphical user interface, mainly because it's usually a lot easier for new users to get their head around. Once you become more familiar with SQL Server, you may find yourself using SQL scripts to perform many of the tasks that you started out doing via the graphical user interface.

No comments:

Post a Comment