Script Method (SQL-DMO)

The Script method generates a Transact-SQL command batch that can be used to re-create the Microsoft® SQL Server™ component referenced by the SQL-DMO object.

Applies To
Alert Object Jobs Collection
Alerts Collection Key Object
Check Object Login Object
Database Object Operator Object
DatabaseRole Object Operators Collection
DBObject Object Rule Object
Default Object StoredProcedure Object
DRIDefault Object Trigger Object
FullTextCatalog Object User Object
Index Object UserDefinedDatatype Object
Job Object View Object

Syntax

object.Script( [ ScriptType ] [, ScriptFilePath ] [, Script2Type ] ) as String

Part Description
object Expression that evaluates to an object in the Applies To list.
ScriptType Optional. A long integer overriding default scripting behavior as described in Settings.
ScriptFilePath Optional. A string specifying an operating system file as an additional target for the generated Transact-SQL script.
Script2Type Optional. A long integer overriding default scripting behavior as described in Settings.

Settings

When setting the ScriptType argument specifying multiple behaviors, combine values by using an Or. Use these values to set ScriptType.

Constant Value Description
SQLDMOScript_AppendToFile 256 Append output to the file identified in the ScriptFilePath argument. By default, Script overwrites an existing file.
SQLDMOScript_
Database Permissions
32 Command batch includes Transact-SQL defining database privilege.
SQLDMOScript_Default 4 SQLDMOScript_
PrimaryObject.
SQLDMOScript_Drops 1 Command batch includes Transact-SQL removing the referenced component. Transact-SQL statements removing a component is prefixed by a test for existence.
SQLDMOScript_IncludeHeaders 131072 Command batch statements are prefixed by a Transact-SQL comment containing date and time of generation and other descriptive information.
SQLDMOScript_IncludeIfNotExists 4096 Transact-SQL creating a component is prefixed by a check for existence. When the command batch is executed, the scripted component is created only when a copy does not exist.
SQLDMOScript_NoCommandTerm 32768 Individual Transact-SQL statements in the command batch are not delimited using the connection-specific command terminator. By default, individual Transact-SQL statements are delimited.
SQLDMOScript_ObjectPermissions 2 Command batch includes Transact-SQL statements defining privilege on database objects.
SQLDMOScript_OwnerQualify 262144 When the command batch removes or creates database or other owned objects, object names are qualified by the current owner of the referenced object.
SQLDMOScript_Permissions 34 SQLDMOScript_
ObjectPermissions and SQLDMOScript_
DatabasePermissions combined by using an Or.
SQLDMOScript_PrimaryObject 4 Default. Command batch includes Transact-SQL statements creating the referenced component.
SQLDMOScript_4
TimestampToBinary
524288 When command batch Transact-SQL creates a user-defined data type, a type derived from timestamp is scripted to use the base data type binary(8).
SQLDMOScript_ToFileOnly 64 When used, and an output file is specified in the ScriptFilePath argument, the Script method does not return the script to the caller, but only writes the script to the output file.
SQLDMOScript_4
UseQuotedIdentifiers
-1 Quote characters delimit identifier parts when scripting object names.

When setting the Script2Type argument specifying multiple behaviors, combine values by using an Or. Use these values to set Script2Type.

Constant Value Description
SQLDMOScript2_AgentAlertJob 2048 When scripting an alert, the command batch includes Transact-SQL statements creating a SQL Server Agent job run when the alert is raised.
SQLDMOScript2_AgentNotify 1024 When scripting an alert, the command batch includes Transact-SQL statements creating notifications for the alert.
SQLDMOScript2_AnsiFile 2 Create output file as a multibyte character text file. Code page 1252 is used to determine character meaning.
SQLDMOScript2_Default 0 Default. No scripting options specified.
SQLDMOScript2_EncryptPWD 128 Encrypt passwords with script. When specified, SQLDMOScript2_
UnicodeFile must be specified as well.
SQLDMOScript2_FullTextCat 2097152 Command batch includes Transact-SQL statements creating Microsoft Search full-text catalogs.
SQLDMOScript2_LoginSID 1048576 Include security identifiers for logins scripted.
SQLDMOScript2_UnicodeFile 4 Create output file as a Unicode character text file. Must be set when using SQLDMOScript2_
EncryptPWD.

Returns

A Transact-SQL command batch as a string.

Remarks

The Script method generates a Transact-SQL command batch defining an existing SQL Server component. Some SQL-DMO objects, such as the Index object, support command batch generation for SQL-DMO objects defining new components through the GenerateSQL method.

Use the GenerateSQL method when capturing object definition. Use the Script method when capturing an image of an exiting component. When using the Script method as part of an application process re-creating a component, specify SQLDMOScript_Drops in the ScriptType argument to include a drop of the existing component in the command batch.


Note SQL-DMO object scripting methods are fully compatible with SQL Server version 7.0. However, database compatibility level affects Transact-SQL command batch contents.

When scripting a database with a compatibility level of less than 7.0, or when scripting any of its objects, the resulting Transact-SQL command batch includes only keywords reserved by that level.

Transact-SQL command syntax is always compliant with SQL Server 7.0. Where they are provided, you can use optional scripting arguments, such as SQLDMOScript2_NoFG to remove some version 7.0 specific syntax.


Prototype (C/C++)

HRESULT Script(
SQLDMO_SCRIPT_TYPE ScriptType = SQLDMOScript_Default, SQLDMO_LPCSTR ScriptFilePath = NULL,
SQLDMO_LPBSTR ScriptText = NULL,
SQLDMO_SCRIPT2_TYPE Script2Type = SQLDMOScript2_Default);


Note SQL-DMO strings are always returned as OLE BSTR objects. A C/C++ application obtains a reference to the string. The application must release the reference by using SysFreeString.


  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.