Saving Scripts as Projects and Solutions

Developers familiar with Microsoft Visual Studio will welcome Solution Explorer in SQL Server Management Studio. The scripts that support your business can be grouped into script projects, and the script projects can be managed together as a solution. When scripts are placed in script projects and solutions they can be opened together as a group, or saved together to a source control product such as Visual SourceSafe. Script projects include the connection information for the scripts to execute properly, and can include non-script files such as a supporting text file.

The following practice creates a short script that queries the AdventureWorks database, placed in a script project and solution.

Using Script Projects and Solutions

To create a script project and solution

  1. Open Management Studio, and connect to a server with Object Explorer.

  2. On the File menu, point to New, and then click Project. The New Project dialog box opens.

  3. In the Name text box, type StatusCheck, click SQL Server Scripts in Templates, and then click OK to open a new solution and script project.

  4. In Solution Explorer, right-click Connections, and then click New Connection. The Connect to Server dialog box opens.

  5. In the Server name list box, type the name of your server.

  6. Click Options, and then click the Connection Properties tab.

  7. In the Connect to database box, browse the server, select the AdventureWorks database, and then click Connect. The connection information including the database is added to the project.

  8. If the Properties window is not displayed, click the new connection in Solution Explorer, and then press F4. The properties for the connection appear, and show information about the connection including the Initial Database as AdventureWorks.

  9. In Solution Explorer, right-click the connection, and then click New Query. A new query called SQLQuery1.sql is created, connected to the AdventureWorks database on your server, and added to your script project.

  10. In Query Editor, type the following query to determine how many work orders have due dates, before the work order starting dates. (You can copy and paste the code from the Tutorial window.)

    USE AdventureWorks;
    GO
    SELECT COUNT(WorkOrderID)
    FROM Production.WorkOrder
    WHERE DueDate < StartDate;
    
    USE AdventureWorks;
    GO
    SELECT COUNT(WorkOrderID)
    FROM Production.WorkOrder
    WHERE DueDate < StartDate;
    

    Note

    If you need more room to type your query, press SHIFT+ALT+ENTER, to switch to full-screen mode.

  11. In Solution Explorer, right-click SQLQuery1, and then click Rename. Type Check Workorders.sql as the new name for the query and press ENTER.

  12. To save your solution and script project, on the File menu, click Save All.