You are on page 1of 10

Lab: Using SQL Server Management Studio and SQLCMD

L1-1

Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008

Lab: Using SQL Server Management Studio and SQLCMD


Exercise 1: Explore the components and execute queries in SQL Server Management Studio
Task 1: Launch the SQL Server Management Studio
1. 2. 3. 4. 5. In the Lab Launcher, next to 2778A-NY-SQL-01, click Launch. Log on to NYC-SQL-01 as Student using the password Pa$$w0rd. On the Start menu, click All Programs, click Microsoft SQL Server 2008, and then click SQL Server Management Studio. The Microsoft SQL Server Management Studio window opens, and then the Connect to Server dialog box appears. In the Connect to Server dialog box, verify that the Server type, Server name, and Authentication fields have been populated correctly, and then click Connect to accept the default settings.

Task 2: Navigate through online help


1. 2. 3. On the Help menu, click Contents. The Server 2008 Combined Help Collection - Microsoft Document Explorer window appears. In the Contents pane, navigate to SQL Server Books Online | Getting Started | Initial Installation | Overview of SQL Server Installation | Features and Tools Overview |SQL Server Studios Overview | Introducing SQL Server Management Studio | Tutorial: SQL Server Management Studio | Lesson 1: Basic Navigation in SQL Server Management Studio.

L1-2

Lab: Using SQL Server Management Studio and SQLCMD

4. 5.

The Lesson 1: Basic Navigation in SQL Server Management Studio page opens in the right pane. In the Contents pane, expand Lesson 1: Basic Navigation in SQL Server Management Studio, and then click the topic Connecting with Registered Servers and Object Explorer. The content of the page opens in the right pane.

6.

Task 3: Resize, hide, and close Object Explorer and Solution Explorer
1. 2. 3. 4. 5. 6. Reduce the size of the Contents pane by resizing it. Scroll down the Connecting with Registered Servers and Object Explorer page to view its contents. Click the Close button to close the Help window. On the View menu, click Solution Explorer. In Solution Explorer, click Auto Hide to hide the pane. In Object Explorer, click Auto Hide to hide the pane.

Task 4: Create a new solution and explore the solution objects in Object Explorer
1. 2. 3. 4. 5. 6. 7. 8. 9. Open Object Explorer by clicking the Object Explorer tab. In Object Explorer, click Auto Hide to dock the pane. In the Object Explorer, double-click the Databases folder, double-click AdventureWorks2008, and then double-click Tables. You can scroll through the pane to view the list of tables. On the toolbar, click New Query. The SQLQuery1.sql - NY-SQL01.AdventureWorks2008 window opens. Open Solution Explorer by clicking the Solution Explorer tab. In Solution Explorer, click Auto Hide to dock the pane. In Solution Explorer, right-click Solution 'Solution1' (1 project), point to Add, and then click New Project. The Add New Project dialog box appears.

Lab: Using SQL Server Management Studio and SQLCMD

L1-3

10. In the Templates box, verify that the SQL Server Scripts template is highlighted. 11. In the Name field, select SQL Server Scripts1, type PersonAddress, and then click OK. 12. The PersonAddress project will be created under Solution Solution1 (1 project). 13. In Solution Explorer, in the PersonAddress project, right-click Connections, and then click New Connection. 14. The Connect to Server dialog box appears. Verify the connection information, and then click OK.

Task 5: Add projects to an existing solution and create queries in the projects
1. 2. 3. 4. 5. 6. 7. 8. 9. In the Connections folder of the PersonAddress project, right-click NY-SQL01:NY-SQL-01\Student, and then click New Query. The SQLQuery1.sql object appears in the Queries folder. In the query window, type USE AdventureWorks2008, and then press ENTER. On the toolbar, click Execute. The active database is changed to AdventureWorks2008. In the query window, type SELECT DISTINCT CITY FROM Person.Address. On the toolbar, click Execute. The list of cities in the Person.Address table is displayed without repetition in the Results pane. In the Queries folder of PersonAddress project, right-click SQLQuery1.sql, and then click Rename.

10. Rename the query as Address.sql. 11. The query has been renamed to Address.sql. 12. On the File menu, click Save Address.sql. 13. In Solution Explorer, right-click PersonAddress, and then click Save PersonAddress.ssmssqlproj.

L1-4

Lab: Using SQL Server Management Studio and SQLCMD

14. Right-click Solution Solution1 (1 project), point to Add, and then click New Project. 15. The Add New Project dialog box appears. 16. In the Templates box, verify that the SQL Server Scripts template is highlighted. 17. In the Name field, select SQL Server Scripts1, type HumanResourcesDepartment, and then click OK. 18. The HumanResourcesDepartment project is created under Solution Solution1 (2 projects). 19. In the HumanResourceDepartments project, right-click Connections, and then click New Connection. 20. The Connect to Server dialog box appears. 21. Verify the connection information, and then click OK. 22. In the Connections folder, under HumanResourceDepartments, right-click NY-SQL-01:NY-SQL-01\Student and then click New Query. 23. The SQLQuery1.sql folder appears. 24. In the Queries folder of HumanResourcesDepartment project, right-click SQLQuery1.sql, and then click Rename. 25. Rename the query as Department.sql. 26. The query has been renamed to Department.sql. 27. In the query window, type USE AdventureWorks2008, and then press ENTER. 28. On the toolbar, click Execute. 29. The active database is changed to AdventureWorks2008. 30. In the query window, type SELECT Name, GroupName FROM HumanResources.Department. 31. On the toolbar, click Execute. 32. The Name and GroupName columns are displayed in the Results pane. 33. On the File menu, and click Save Department.sql. 34. In Solution Explorer, right-click the HumanResourcesDepartment project, and then click Save HumanResourcesDepartment.ssmssqlproj.

Lab: Using SQL Server Management Studio and SQLCMD

L1-5

35. Click Solution Solution1 (2 projects). 36. On the File menu, click Save Solution1 As. 37. The Save File As dialog box appears. Verify that the Projects folder is selected in the Save in: list. 38. In the File name field, type AdventureWorks2008.ssmssln, and then click Save. 39. Click the Close button to close SQL Server Management Studio.

Task 6: Connect to SQL Server and execute a query


1. 2. 3. On the Start menu, click All Programs, click Microsoft SQL Server 2008, and then click SQL Server Management Studio. The Microsoft SQL Server Management Studio window opens, and then the Connect to Server dialog box appears. In the Connect to Server dialog box, verify that the Server type, Server name, and Authentication fields have been populated correctly, and then click Connect to accept the default settings. On the File menu, point to Open, and then click File. The Open File dialog box appears. In the Open File window, navigate to the PersonAddress folder, click Address.sql, and then click Open. If the Connect to Database Engine application window appears, click Connect. On the toolbar, click Execute. The results are displayed in the Results pane. It should be similar to the results in the earlier task.

4. 5. 6. 7. 8.

L1-6

Lab: Using SQL Server Management Studio and SQLCMD

Task 7: Use Visual Query Builder to return rows from a table


1. 2. 3. 4. 5. 6. 7. 8. 9. In Object Explorer, expand the Databases folder. Right-click AdventureWorks2008 database, and then click New Query. On the Query menu, click Design Query in Editor. The Query Designer dialog box appears, and the Add Table dialog box appears. In the Add Table dialog box, verify that the Address (Person) table is selected, and then click Add. The Address (Person) dialog box appears. The columns of the Address table are listed in the Address (Person) dialog box. In the Add Table dialog box, click Close. In the Address (Person) dialog box, select the * (All Columns) check box.

10. In the Query Designer dialog box, click OK. 11. Notice the query in the SQLQuery1.sql - NY-SQL-01.AdventureWorks2008 query pane. 12. On the toolbar, click Execute. 13. All the columns in the Person.Address table are displayed in the Results pane. 14. Click the Close button to close SQL Server Management Studio. 15. The Microsoft SQL Server Management Studio dialog box appears. 16. Click No to close the solution without saving changes.
Results: After this exercise, you should have explored the components and executed queries in the SQL Server Management Studio.

Lab: Using SQL Server Management Studio and SQLCMD

L1-7

Exercise 2: Start and use sqlcmd


Task 1: Start the sqlcmd utility and connect to a default instance of SQL Server
1. 2. 3. 4. On the Start menu click All Programs, click Accessories, and then click Command Prompt. The Command Prompt window opens. At the command prompt, type sqlcmd, and then press ENTER. You now have a trusted connection to the default instance of SQL Server that is running on your computer. 1> is the sqlcmd prompt that specifies the line number. Each time you press ENTER, the number increases by one. At the sqlcmd prompt, type exit, and then press ENTER, to end the sqlcmd session.

5.

Task 2: Run a Transact-SQL script file by using sqlcmd


1. 2. 3. In the Command Prompt window, type: sqlcmd -S NY-SQL-01 -i E:\MOD01\Labfiles\Starter\Department.sql. Press ENTER. Notice that the results are similar to the results returned in the earlier task.

Task 3: Run a Transact-SQL script file and save the output to a text file
1. In the Command Prompt window, type: sqlcmd -S NY-SQL-01 -i E:\MOD01\Labfiles\Starter\Department.sql -o E:\MOD01\Labfiles\Solution\DeptList.txt. Press ENTER. Notice that there is no output in the Command Prompt window.

2. 3.

L1-8

Lab: Using SQL Server Management Studio and SQLCMD

Task 4: Review the output file


1. On the Start menu, click Computer. 2. The Windows Explorer window opens. 3. Navigate to E:\MOD01\Labfiles\Solution\.

4. In the details pane, double-click DeptList.txt. 5. 6. 7. 8. 9. The DeptList.txt - Notepad window opens. Notice that the results are similar to the results returned in the earlier task. Close Notepad. Close Windows Explorer. Close Command Prompt.
Results: After this exercise, you should have started and used sqlcmd to create reports.

Lab: Using SQL Server Management Studio and SQLCMD

L1-9

Exercise 3: Generate a report from a SQL Server database using Microsoft Office Excel
Task 1: Launch Excel
1. 2. On the Start menu, click All Programs, click Microsoft Office, and then click Microsoft Office Excel 2007. The Microsoft Excel window opens.

Task 2: Create a new data connection in the workbook


1. 2. 3. 4. On the Data menu, click Get External Data, click From Other Sources, and then click From SQL Server. The Data Connection Wizard wizard appears. In the Server name field, type NY-SQL-01. Click Next.

Task 3: Select the data to import and its format


1. 2. 3. 4. 5. 6. 7. 8. 9. On the Select Database and Table page, in the Select the database that contains the data you want list, click AdventureWorks2008. In the Connect to a specific table box, in the Name column, click Address. Click Next. In the Save Data Connection File and Finish page, in the Description field, type AdventureWorks2008 Addresses. In the Friendly Name field, type Addresses. Click Authentication Settings. The Excel Services Authentication Settings dialog box appears. Notice the three options for authentication and notice that Windows Authentication is selected. Click Cancel.

10. Click Finish.

L1-10

Lab: Using SQL Server Management Studio and SQLCMD

Task 4: View the report


1. 2. 3. 4. 5. 6. 7. 8. 9. The Import Data dialog box appears. Notice the options for importing the data. Click OK to accept the default settings. The Microsoft Excel-Book 1 is populated with the names of various cities. Examine the results. Close Microsoft Office Excel. The Microsoft Office Excel dialog box appears indicating whether you need to save the changes that you have made to Book 1. Click No. Turn off 2778A-NY-SQL-01 and delete changes.
Results: After this exercise, you should create a report from a SQL Server database using Microsoft Office Excel.

You might also like