--- title: "ALTER XML SCHEMA COLLECTION (Transact-SQL) | Microsoft Docs" ms.custom: "" ms.date: "03/14/2017" ms.prod: "sql-non-specified" ms.reviewer: "" ms.suite: "" ms.technology: - "database-engine" ms.tgt_pltfrm: "" ms.topic: "language-reference" f1_keywords: - "ALTER_XML_SCHEMA_COLLECTION_TSQL" - "ALTER XML SCHEMA COLLECTION" dev_langs: - "TSQL" helpviewer_keywords: - "schema collections [SQL Server], altering" - "xml_schema_namespace function" - "adding schema components" - "ALTER XML SCHEMA COLLECTION statement" - "XML schemas [SQL Server], adding" - "XML schema collections [SQL Server], modifying" - "schema collections [SQL Server], adding components" - "XML schema collections [SQL Server], adding components" - "importing schemas" - "XML schema collections [SQL Server], altering" - "schema collections [SQL Server], modifying" - "multiple schema namespaces" ms.assetid: e311c425-742a-4b0d-b847-8b974bf66d53 caps.latest.revision: 23 author: "douglaslMS" ms.author: "douglasl" manager: "jhubbard" --- # ALTER XML SCHEMA COLLECTION (Transact-SQL) [!INCLUDE[tsql-appliesto-ss2008-xxxx-xxxx-xxx_md](../../includes/tsql-appliesto-ss2008-xxxx-xxxx-xxx-md.md)] Adds new schema components to an existing XML schema collection. ![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md) ## Syntax ``` ALTER XML SCHEMA COLLECTION [ relational_schema. ]sql_identifier ADD 'Schema Component' ``` ## Arguments *relational_schema* Identifies the relational schema name. If not specified, the default relational schema is assumed. *sql_identifier* Is the SQL identifier for the XML schema collection. **'** *Schema Component* **'** Is the schema component to insert. ## Remarks Use the ALTER XML SCHEMA COLLECTION to add new XML schemas whose namespaces are not already in the XML schema collection, or add new components to existing namespaces in the collection. The following example adds a new \ to the existing namespace `http://MySchema/test_xml_schema` in the collection `MyColl`. ``` -- First create an XML schema collection. CREATE XML SCHEMA COLLECTION MyColl AS ' ' -- Modify the collection. ALTER XML SCHEMA COLLECTION MyColl ADD ' '; ``` `ALTER XML SCHEMA` adds element `` to the previously defined namespace `http://MySchema/test_xml_schema`. Note that if some of the components you want to add in the collection reference components that are already in the same collection, you must use ``. However, it is not valid to use the current schema namespace in ``, and therefore components from the same target namespace as the current schema namespace are automatically imported. To remove collections, use [DROP XML SCHEMA COLLECTION (Transact-SQL)](../../t-sql/statements/drop-xml-schema-collection-transact-sql.md). If the schema collection already contains a lax validation wildcard or an element of type **xs:anyType**, adding a new global element, type, or attribute declaration to the schema collection will cause a revalidation of all the stored data that is constrained by the schema collection. ## Permissions To alter an XML SCHEMA COLLECTION requires ALTER permission on the collection. ## Examples ### A. Creating XML schema collection in the database The following example creates the XML schema collection `ManuInstructionsSchemaCollection`. The collection has only one schema namespace. ``` -- Create a sample database in which to load the XML schema collection. CREATE DATABASE SampleDB; GO USE SampleDB; GO CREATE XML SCHEMA COLLECTION ManuInstructionsSchemaCollection AS N' ' ; GO -- Verify - list of collections in the database. SELECT * FROM sys.xml_schema_collections; -- Verify - list of namespaces in the database. SELECT name FROM sys.xml_schema_namespaces; -- Use it. Create a typed xml variable. Note the collection name -- that is specified. DECLARE @x xml (ManuInstructionsSchemaCollection); GO --Or create a typed xml column. CREATE TABLE T ( i int primary key, x xml (ManuInstructionsSchemaCollection)); GO -- Clean up. DROP TABLE T; GO DROP XML SCHEMA COLLECTION ManuInstructionsSchemaCollection; Go USE master; GO DROP DATABASE SampleDB; ``` Alternatively, you can assign the schema collection to a variable and specify the variable in the `CREATE XML SCHEMA COLLECTION` statement as follows: ``` DECLARE @MySchemaCollection nvarchar(max); SET @MySchemaCollection = N' copy the schema collection here'; CREATE XML SCHEMA COLLECTION AS @MySchemaCollection; ``` The variable in the example is of `nvarchar(max)` type. The variable can also be of **xml** data type, in which case, it is implicitly converted to a string. For more information, see [View a Stored XML Schema Collection](../../relational-databases/xml/view-a-stored-xml-schema-collection.md). You can store schema collections in an **xml** type column. In this case, to create XML schema collection, perform the following steps: 1. Retrieve the schema collection from the column by using a SELECT statement and assign it to a variable of **xml** type, or a **varchar** type. 2. Specify the variable name in the CREATE XML SCHEMA COLLECTION statement. The CREATE XML SCHEMA COLLECTION stores only the schema components that [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] understands; everything in the XML schema is not stored in the database. Therefore, if you want the XML schema collection back exactly the way it was supplied, we recommend that you save your XML schemas in a database column or some other folder on your computer. ### B. Specifying multiple schema namespaces in a schema collection You can specify multiple XML schemas when you create an XML schema collection. For example: ``` CREATE XML SCHEMA COLLECTION N' .... ...'; ``` The following example creates the XML schema collection `ProductDescriptionSchemaCollection` that includes two XML schema namespaces. ``` CREATE XML SCHEMA COLLECTION ProductDescriptionSchemaCollection AS ' ' ; GO -- Clean up DROP XML SCHEMA COLLECTION ProductDescriptionSchemaCollection; GO ``` ### C. Importing a schema that does not specify a target namespace If a schema that does not contain a **targetNamespace** attribute is imported in a collection, its components are associated with the empty string target namespace as shown in the following example. Note that not associating one or more schemas imported in the collection results in multiple schema components (potentially unrelated) being associated with the default empty string namespace. ``` -- Create a collection that contains a schema with no target namespace. CREATE XML SCHEMA COLLECTION MySampleCollection AS ' '; GO -- query will return the names of all the collections that --contain a schema with no target namespace SELECT sys.xml_schema_collections.name FROM sys.xml_schema_collections JOIN sys.xml_schema_namespaces ON sys.xml_schema_collections.xml_collection_id = sys.xml_schema_namespaces.xml_collection_id WHERE sys.xml_schema_namespaces.name=''; ``` ## See Also [CREATE XML SCHEMA COLLECTION (Transact-SQL)](../../t-sql/statements/create-xml-schema-collection-transact-sql.md) [DROP XML SCHEMA COLLECTION (Transact-SQL)](../../t-sql/statements/drop-xml-schema-collection-transact-sql.md) [EVENTDATA (Transact-SQL)](../../t-sql/functions/eventdata-transact-sql.md) [Compare Typed XML to Untyped XML](../../relational-databases/xml/compare-typed-xml-to-untyped-xml.md) [Requirements and Limitations for XML Schema Collections on the Server](../../relational-databases/xml/requirements-and-limitations-for-xml-schema-collections-on-the-server.md)