[英]Alter all tables in database
I would like to run a "Alter Table" on ALL the tables in my SQL databse: 我想在我的SQL数据库中的所有表上运行“Alter Table”:
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. 在我的数据库(50多个表)中,所有表共有1行。 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
如果你想使用
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. 设置
'COLUMN NAME'
并执行,它将为设置列添加默认约束。
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. 我首选的方法是为此编写一个SP。 I have some of these utility SPs in my databases and I alter them as needed to update things.
我在我的数据库中有一些这些实用程序SP,我根据需要更改它们以更新内容。 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.
您可以通过修改SET @S = ...语句看到您可以执行任何您喜欢的表更改。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.