Query all SQL Server tables and change their schemas

Source: Internet
Author: User

Querying all the Databases

Select [Name] from [sysdatabases] ORDER by [name]

Querying for all tables in a database

Select [ID], [name] from [sysobjects] where [type] = ' u '

all know [Type] = ' u ' is the user table, [type] = ' s ' is the system table.

Based on the query results from the previous statement, and then

Query all the fields in a table (I try to use [name] instead of [ID] and the results are wrong)

Select [Name] from [syscolumns] where [id] = 1931153925

All tables are queried:

Here's how to change all table schemas:

Schema A and user A are present in the database TestDB, the user who owns the TestDB database is changed from a to B, the user A is deleted, the schema is changed from a to B, the schema A is deleted, and the operation is as follows:

1, create User B, and then create schema B;

2. Assign the permissions of schema A to User B, cancel user A's permission to have schema A, delete User A;

3. Change all objects belonging to schema A of the database to schema B with the following code:

ALTER schema [new schema name] TRANSFER the old schema name. [Object table or view or stored procedure in database]

ALTER SCHEMA [B] TRANSFER a.[Object 1]

ALTER SCHEMA [B] TRANSFER a.[Object 2]

ALTER SCHEMA [B] TRANSFER a.[object N]

4, delete the schema A.

Such as:

ALTER SCHEMA dbo TRANSFER danxiao1.ds_city1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.