Share via


GenerateSQL Method (Index)

This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

The GenerateSQL method returns a string that contains a Transact-SQL command batch used to create the Microsoft SQL Server index defined by the properties of the Index object used.

Syntax

object
.GenerateSQL(
Table
)
as
String

Parts

  • object
    Expression that evaluates to an object in the Applies To list.
  • Table
    Expression that evaluates to a SQL Distributed Management Objects (SQL-DMO) Table object.

Prototype (C/C++)

HRESULT GenerateSQL(
LPSQLDMOTABLE pTable,
SQLDMO_LPBSTR pRetVal);

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 using SysFreeString.

Returns

A Transact-SQL command batch as a string.

Remarks

Use the GenerateSQL or GenerateCreationSQL method to create a command batch for use in another process. For example, to define a new index, capture the command batch using the GenerateSQL or GenerateCreationSQL method, and then use the command batch to create a job step for scheduled index creation.

For the Index object, the GenerateSQL and GenerateCreationSQL methods perform similar functions. The script returned by the GenerateSQL method includes a Transact-SQL statement creating an index. The GenerateCreationSQL method prefixes the index creation statement with Transact-SQL syntax that conditionally removes an existing index.

To use the GenerateSQL or GenerateCreationSQL method

  1. Create a new Index object.

  2. Set the Name property.

  3. Set the IndexedColumns property; reference columns in the target table by name.

  4. Set additional properties that define the index such as FileGroup and Type.

  5. Get the Table object that references the target table from the Tables collection.

  6. Call the method that generates the Transact-SQL command batch, capturing the returned text.

    Important

    The GenerateSQL and GenerateCreationSQL methods generate a Transact-SQL batch used to create an index. The method fails if the Index object used references an existing SQL Server index. Use the Script method of the Index object to create a Transact-SQL command batch that defines an existing index.

Applies To:

Index Object