简体   繁体   中英

Alter all tables in database

I would like to run a "Alter Table" on ALL the tables in my SQL databse:

ALTER TABLE test ADD  CONSTRAINT [COLLUM_NAME]  DEFAULT ((0)) FOR [COLLUM_NAME]

I know how to get all of the existing tables from the database:

SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'

or

USE DATABASE_NAME
GO 
SELECT name
FROM sys.Tables
GO

But I don't know how to combine these two.

In my database (50+ tables) all of the tables have 1 row in common. and I would like to set a default value to all of these rows.

You can try to generate a command and execute it after. You can do something like this:

SELECT CONCAT("Alter Table `", TABLE_SCHEMA,"`.`", TABLE_NAME, "` this is my default value change on the column") as MySQLCMD 
FROM TABLES 

And execute the retrieving.

If this is a one-off process that doesn't need to be automated then you could probably do worse than running something like the following and just copy/pasting the output:

select 'alter table ' + t.name + ' add constraint ' + c.name + ' default ((0)) for ' + c.name
from    sysobjects t join syscolumns c on c.id = t.id
where   t.xtype = 'U'

If u want use INFORMATION_SCHEMA

SELECT 'ALTER TABLE ' +t.TABLE_NAME+ ' ADD  CONSTRAINT '
+c.COLUMN_NAME  +' DEFAULT ((0)) FOR '+c.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLES t
INNER JOIN INFORMATION_SCHEMA.COLUMNS c on t.TABLE_NAME=c.TABLE_NAME
WHERE TABLE_TYPE = 'BASE TABLE'

set the 'COLUMN NAME' and execute, it will add a default constraint to setted column.

DECLARE @sql NVARCHAR(MAX) ;
DECLARE @LINEBREAK AS VARCHAR(2)
SET @LINEBREAK = CHAR(13) + CHAR(10)      

SELECT  @sql = COALESCE(@sql + ';' + @LINEBREAK, '') + 'ALTER TABLE ' 
        + QUOTENAME([TABLES].TABLE_NAME) + ' ADD  CONSTRAINT ' + QUOTENAME([COLUMNS].COLUMN_NAME)
        + ' DEFAULT ((0)) FOR ' + QUOTENAME([COLUMNS].COLUMN_NAME)
FROM    INFORMATION_SCHEMA.TABLES [TABLES]
        INNER JOIN INFORMATION_SCHEMA.COLUMNS AS [COLUMNS] ON [TABLES].TABLE_NAME = [COLUMNS].TABLE_NAME
WHERE   TABLE_TYPE = 'BASE TABLE'
        AND [COLUMNS].[COLUMN_NAME] = 'COLUMN NAME'
PRINT @sql
EXEC sp_executesql @sql

My preferred way is to write a SP for this. I have some of these utility SPs in my databases and I alter them as needed to update things. Here's an example that changes the collation. You can see that by modifying the SET @S=... statement you can do any table alteration you like.

DECLARE table_name VARCHAR(255);
DECLARE end_of_tables INT DEFAULT 0;
DECLARE num_tables INT DEFAULT 0;

DECLARE cur CURSOR FOR 
    SELECT t.table_name 
    FROM information_schema.tables t 
    WHERE t.table_schema = DATABASE() AND t.table_type='BASE TABLE';

DECLARE CONTINUE HANDLER FOR NOT FOUND SET end_of_tables = 1;

OPEN cur;

tables_loop: LOOP

    FETCH cur INTO table_name;

    IF end_of_tables = 1 THEN
        LEAVE tables_loop;
    END IF;

    SET num_tables = num_tables + 1;

    SET @s = CONCAT('ALTER TABLE ' , table_name , ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci');

    PREPARE stmt FROM @s;
    EXECUTE stmt;
END LOOP;

CLOSE cur;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM