Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server Error 1734: Fix Sparse Column Set Issue

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Error 1734 in SQL Server occurs when you try to create a sparse column set on a table that already has one, or when the table contains columns that are not compatible with a column set. The fix is to remove the existing column set before adding a new one, or to ensure the table has no incompatible columns (such as computed columns, or columns of certain data types) before creating the set. This guide walks you through the correct steps to resolve the error and successfully create a sparse column set.

What You'll Need

  • SQL Server Management Studio (SSMS) or another client that can run T-SQL queries.
  • ALTER permission on the table where you want to add the column set.
  • Knowledge of the table schema, especially existing sparse columns and any computed columns.

Step-by-Step

  1. Identify the existing column set. Open a new query window and run the following query against your database, replacing YourTable with the actual table name:
    SELECT name, is_column_set
    FROM sys.columns
    WHERE object_id = OBJECT_ID('YourTable') AND is_column_set = 1;
    If a row is returned, a column set already exists. Note its name.
  2. Check for incompatible columns. A sparse column set cannot coexist with certain column types. Run this query to list all columns and their types:
    SELECT name, system_type_id, is_computed, is_sparse
    FROM sys.columns
    WHERE object_id = OBJECT_ID('YourTable');
    Columns with data types like text, ntext, image, timestamp, geometry, geography, or user-defined types cannot be part of a column set. Computed columns are also not allowed. If any exist, you must alter or drop them before creating the column set.
  3. Remove the existing column set if present. If step 1 returned a column set, drop it using:
    ALTER TABLE YourTable DROP COLUMN YourColumnSetName;
    Replace YourColumnSetName with the actual name. This removes the column set but does not affect the sparse columns themselves.
  4. Ensure the table has at least one sparse column. A column set can only be created if the table already contains at least one sparse column. If none exist, add one or convert an existing column to sparse. For example:
    ALTER TABLE YourTable ALTER COLUMN YourColumn ADD SPARSE;
    Replace YourColumn with a nullable column that can be sparse. Note that not all data types support sparse; see SQL Server documentation for allowed types.
  5. Create the new column set. Now add the column set with a unique name:
    ALTER TABLE YourTable ADD YourColumnSetName XML COLUMN_SET FOR ALL_SPARSE_COLUMNS;
    This creates an XML column that represents all sparse columns in the table. The operation will fail if any incompatible columns remain, so ensure step 2 is fully resolved.
  6. Verify the column set. Run the query from step 1 again to confirm the column set exists. You can also insert or update data and query the column set to see the XML representation of sparse column values.

Common Mistakes

  • Trying to add a second column set. A table can have only one column set. If you attempt to add another without dropping the first, error 1734 occurs.
  • Ignoring incompatible columns. Computed columns, columns with certain data types, or columns that are part of a unique constraint may prevent column set creation. Always check the schema first.
  • Forgetting to make a column sparse. A column set requires at least one sparse column. If the table has none, the operation fails with a different error, but it's a common oversight.
  • Using the wrong syntax. The correct syntax is ADD <column_set_name> XML COLUMN_SET FOR ALL_SPARSE_COLUMNS. Omitting XML or using a different clause will cause errors.

Tips

  • Before making schema changes, back up the table or database, especially in production environments.
  • Use sp_help or sp_columns to quickly inspect table structure and identify sparse columns.
  • Column sets are useful for exposing sparse columns as XML, but they add overhead. Only create them if your application needs to query sparse data as XML.
  • If you need to modify a column set, you must drop and recreate it; you cannot alter it directly.
  • Test changes on a development copy first to avoid disrupting live applications.

Troubleshooting

If error 1734 persists after following the steps, check the following:

  • Error message details: The full error may mention a specific column or constraint. Use that to pinpoint the issue.
  • Permissions: Ensure your login has ALTER permission on the table. You can check with SELECT HAS_PERMS_BY_NAME('YourTable', 'OBJECT', 'ALTER');
  • Dependencies: If the table is referenced by indexed views, statistics, or other objects, you may need to drop and recreate those dependencies. Script them out first.
  • Schema-bound objects: If the table is part of a schema-bound view or function, you must drop those objects before altering the table.
  • Check for hidden column sets: Sometimes a column set exists but is not visible in Object Explorer. Use the query from step 1 to be sure.
  • Review SQL Server error logs: Look for additional errors that might indicate a deeper issue, such as corruption or locking.

If you are still stuck, consult the official Microsoft documentation on sparse columns and column sets, or seek help from a DBA.

Frequently Asked Questions

What causes SQL Server error 1734?

Error 1734 occurs when you try to create a sparse column set on a table that already has one, or when the table contains columns that are incompatible with a column set (e.g., computed columns, text/ntext/image types).

Can I have multiple column sets in one table?

No, a table can have only one column set. You must drop the existing one before creating a new one.

Do I need to drop the column set to add a sparse column?

No, you can add sparse columns without dropping the column set. The column set automatically includes all sparse columns.

What data types are not allowed in a column set?

Columns with data types like text, ntext, image, timestamp, geometry, geography, and user-defined types cannot be part of a column set. Computed columns are also not allowed.

How do I remove a column set?

Use ALTER TABLE table_name DROP COLUMN column_set_name;. This removes the column set but leaves the sparse columns intact.

Is error 1734 related to sparse columns?

Yes, it specifically occurs when creating a column set for sparse columns. Ensure the table meets all requirements before attempting to create the set.