The database project template automatically creates two folders for holding SQL scripts. The Create Scripts folder is meant to hold scripts that are used to re-create the database from scratch, or to re-create a portion of the database that has undergone changes. The Change Scripts folder is meant to contain SQL scripts that reflect desired changes that you haven't yet made to the database. Changes may be "placed on hold" because, as a developer, you don't have sufficient security privileges to modify a production database (sometimes not a bad idea!) or because changes from multiple sources are to be combined and applied all at once as a unit. Note The folder names given are by convention only. You can change them to something different that you prefer or find more meaningful. Of course, being able to change them also means that you have the flexibility to mess things up, or at least confuse others (or even yourself) who need to use your project. You can also add other folders to a database project. For example, you may want a separate folder to store scripts required for upgrading the database to a specific release version or for other maintenance activities. You may want to have several different query folders, each containing different types of queries. You can add a new folder to the project by right-clicking on the project and selecting the New Folder menu item from the context menu that is displayed. You can create any SQL script manually, by right-clicking on a folder (or the project) node in the Solution Explorer and then selecting the Add New Item or the Add SQL Script menu item from the displayed context menu. After selecting one of the standard script templates, shown in Figure 8.2, you can edit it manually in the Visual Studio editor. Figure 8.2. The standard script templates displayed in the Add New Item dialog boxWhat is special about the Create and Change scripts is that you can have them generated automatically. Create ScriptsAs we said previously, Create Scripts are SQL scripts that create new database objects, including tables, views, stored procedures, and constraints. They are normally used to set up an installation or revert an existing site (such as a development server) to its pristine state. Note You can generate Create Scripts only if you are using SQL Server 7.0 or SQL Server 2000. Moreover, to use this feature, the client tools for SQL Server must be installed on the same machine as Visual Studio. The reason is that Visual Studio utilizes the same tools and dialogs. To generate Create Scripts, do the following.
Note, however, that these scripts create only the database schema and do not populate the newly created tables with any data. In the Command Files section, we show how to copy a table's data. Change ScriptsChange scripts are used to apply changes to an existing database schema. Although these scripts could be written manually, it is preferable to use a tool to generate them. When we use the Visual Database tools that we used in Chapters 1 and 2, Visual Studio automatically maintains a script of any changes made to the database schema that haven't yet been applied to the database. Let's say that you want to add a new field, StartDate, to the tblEmployee table to track the start date of each employee. Do the following.
Because you will want to apply this change to all the databases already installed and deployed at different sites in the field, you need to create a change script for what you just did.
Running the ScriptsYou can run a script directly within the Solution Explorer. The easiest way to do so is to drag the script that you want to run and drop it on the database reference that you want to run it against. Alternatively, you can right-click on the script that you want to run. The context menu displayed has both a Run and a Run On menu items. Selecting the Run item executes the script against the default database reference. Selecting the Run On menu item, as shown in Figure 8.9, allows you to specify a database other than the current default. Note that you can choose from existing database references or use this opportunity to add a new one. Figure 8.9. The Run On dialog boxNote You can also choose to define a temporary database reference to run this script on. Double-clicking on the last item in the list, <temporary reference>, displays the familiar Data Link Properties dialog for you to use to define the connection. However, this reference won't be added to the project or to the Server Explorer. To apply the changes that you previously designed and saved in the script tblEmployee.sql, do the following.
Command FilesNow that you have created all these scripts to create and modify the different database objects, wouldn't it be nice if you could organize multiple scripts into a logical group to be run as a single unit? Yes it would be, and VS.NET can create command files to do just that. These command files, which have the .cmd extension, are meant to be used on the Windows 2000 or Windows XP operating systems, which recognize and can execute such files. These files can also load a newly created table with data that we exported from an existing database. Note The ability to easily and automatically create a script that loads table data in addition to creating database schema and objects is a VS.NET feature not found in the SQL Server Enterprise Manager. Let's say that we want to create a single command file that will automatically run all the Create Scripts that we need to create a brand new version of our Novelty database on another computer. Although this new system will have its own customers, employees, and orders, the inventory information in tblInventory will be the same. We therefore want to populate the new database's tblInventory table with the data currently in our existing tblInventory table. Because you will want to have the command file load the inventory data from the existing database to the newly created one, you must first export the data and then continue with the process of creating the command file, as follows.
Listing 8.1 The contents of the Create Scripts.cmd command file@echo off REM: Command File Created by Microsoft Visual Database Tools REM: Date Generated: 08-Feb-02 REM: Authentication type: Windows NT REM: Usage: CommandFilename [Server] [Database] if '%1' == '' goto usage if '%2' == '' goto usage if '%1' == '/?' goto usage if '%1' == '-?' goto usage if '%1' == '?' goto usage if '%1' == '/help' goto usage osql -S %1 -d %2 -E -b -i "dbo.tblCustomer.tab" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblDepartment.tab" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblEmployee.tab" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblInventory.tab" if %ERRORLEVEL% NEQ 0 goto errors bcp "%2.dbo.tblInventory" in "dbo.tblInventory.dat" -S %1 -T -k -n -q if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblOrder.tab" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblOrderItem.tab" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblCustomer.kci" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblDepartment.kci" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblEmployee.kci" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblInventory.kci" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblOrder.kci" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblOrderItem.kci" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblCustomer.fky" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblDepartment.fky" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblEmployee.fky" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblInventory.fky" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblOrder.fky" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblOrderItem.fky" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblCustomer.ext" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblDepartment.ext" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblEmployee.ext" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblInventory.ext" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblOrder.ext" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.tblOrderItem.ext" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.Employee_view.viw" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.EmployeeDepartment_view.viw" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.DeleteEmployee.prc" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.GetCustomerFromID.prc" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.InsertEmployee.prc" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.InsertEmployeeOrg.prc" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.LastNameLookup.prc" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.procEmployeesSorted.prc" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.SelectEmployees.prc" if %ERRORLEVEL% NEQ 0 goto errors osql -S %1 -d %2 -E -b -i "dbo.UpdateEmployee.prc" if %ERRORLEVEL% NEQ 0 goto errors goto finish REM: How to use screen :usage echo. echo Usage: MyScript Server Database echo Server: the name of the target SQL Server echo Database: the name of the target database echo. echo Example: MyScript.cmd MainServer MainDatabase echo. echo. goto done REM: error handler :errors echo. echo WARNING! Error(s) were detected! echo echo Please evaluate the situation and, if needed, echo restart this command file. You may need to echo supply command parameters when executing echo this command file. echo. pause goto done REM: finished execution :finish echo. echo Script execution is complete! :done @echo on Note The command file makes use of the osql and bcp command line utilities that are part of the SQL Server installation. The osql utility allows you to execute SQL statements, system procedures, and script files. The bcp is a bulk copy program that copies data to and from a data file and an instance of SQL Server. You can run this command file from within the Solution Explorer by right-clicking on it and then selecting the Run menu item. You can also invoke it externally, independent of Visual Studio, so long as all the scripts exist together with the command file. Tip Remember that running this command file against a database will delete all the data that currently exists in that database! |