When creating a database object from scratch, the needed objects are created along the way. The objects can be tied in relationships and constraints to make them dependent on each other.
Creating objects from existing scripts is a bit different. The database structure is not known, along with its dependencies and constraints. If a table that utilizes a user-defined type that doesn’t exist in the database is created, the following error message will be encountered:
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: Consolas, "Courier New", Courier, Monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .err { color: #FF0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
Msg 2715, Level 16, State 6, Line 2
Column, parameter, or variable #11: Cannot find data type dbo.Flag.
Something similar happens when a database object that depends on another, non-existent object is created:
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: Consolas, "Courier New" , Courier, Monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .err { color: #FF0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
Msg 208, Level 16, State 1, Procedure vEmployeeDepartment, Line 8
Invalid object name 'HumanResources.Employee'.
The solution is to create the parent objects first, and then create the objects that reference them.
This would be easy if the database only had a couple of objects and a dependency schema is known. Unfortunately, this is not usually the case.
One of the methods is to create database objects by type:
But, what happens if the ViewEmployee view references the ViewEmployeeHistory view? The ViewEmployeeHistory view has to be created first. So, the above method might not always work.
How to execute the object scripts in a correct order
ApexSQL Build is a SQL database deployment tool that builds new databases or updates existing ones by packaging SQL scripts, script folders, database snapshots and even scripts under source control into a single deployment SQL script, a C# solution or an executable installer. It allows specifying the objects to be created or updated, ensures error-free deployment and more.
Select the Database option as an output type:
Under the Database options step, provide a database name and other database properties such as compatibility mode, collation, etc.:
If the Save a copy of script options is chosen, the created script can be reviewed or modified and re-execute it afterwards, directly in SQL Server Management Studio.
Additionally, the Header can be inserted, along with the Version number of the build script:
Click the Finish button
Once the execution of the build script is done, the Results window will be shown:
The script will create a new database and all objects in it.
Creating a database from a bunch of DDL scripts doesn’t have to be a problem. ApexSQL Build provides the optimal object creation order so that all objects are created successfully and no problems with missing dependencies are encountered.
© ALL RIGHTS RESERVED. Terms of Use Privacy Cookie Preference Center