是否可以同时向多个表添加列?

发布于 2024-10-13 21:44:16 字数 752 浏览 6 评论 0原文

我正在使用 SQL Server。我想将名为 [DateCreated] 的单个列添加到多个表中。是否可以通过一条语句将此列添加到数据库中的所有表中?

我偶然发现了 Joe Steffaneli 的一个答案,他在其中建议了一个查询,该查询又返回包含 Alter table 语句的行。 查询如下:

select 'alter table ' + quotename(s.name) + '.' + quotename(t.name) + ' add [DateModified] datetime'
    from sys.columns c
        inner join sys.tables t
            on c.object_id = t.object_id
        inner join sys.schemas s
            on t.schema_id = s.schema_id
        left join sys.columns c2
            on t.object_id = c2.object_id
                and c2.name = 'DateModified'
    where c.name = 'DateCreated'
        and t.type = 'U'
        and c2.column_id is null /* DateModified column does not already exist */ 

有什么方法可以执行返回的行吗?对不起英语。

I am using SQL Server. I want to add a single column named [DateCreated] to multiple tables. Is it possible that with a single statement I could add this column to all the tables in my database?

I stumble upon an answer by Joe Steffaneli in which he suggested a query which in turn returns rows consisting Alter table statements.
Query is as follows :

select 'alter table ' + quotename(s.name) + '.' + quotename(t.name) + ' add [DateModified] datetime'
    from sys.columns c
        inner join sys.tables t
            on c.object_id = t.object_id
        inner join sys.schemas s
            on t.schema_id = s.schema_id
        left join sys.columns c2
            on t.object_id = c2.object_id
                and c2.name = 'DateModified'
    where c.name = 'DateCreated'
        and t.type = 'U'
        and c2.column_id is null /* DateModified column does not already exist */ 

Is there any way that I can execute returned rows? Sorry for English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

完美的未来在梦里 2024-10-20 21:44:16

你可能需要这样的东西。在运行脚本之前检查脚本是否执行了您想要的操作(添加默认值为 getdate() 的非空列)!

DECLARE @Dynsql nvarchar(max) 
SET @Dynsql = ''

SELECT @Dynsql = @Dynsql + '
alter table ' + QUOTENAME(SCHEMA_NAME(schema_id))+ '.' + QUOTENAME(name)  + 
' add [DateCreated] datetime not null default getdate()' 
FROM sys.tables
WHERE type='U' and object_id NOT IN (select object_id from sys.columns where name='DateCreated')


EXEC (@Dynsql)

You probably need something like this. Check that the script does what you want before running it (adds a non null column with a default value of getdate())!

DECLARE @Dynsql nvarchar(max) 
SET @Dynsql = ''

SELECT @Dynsql = @Dynsql + '
alter table ' + QUOTENAME(SCHEMA_NAME(schema_id))+ '.' + QUOTENAME(name)  + 
' add [DateCreated] datetime not null default getdate()' 
FROM sys.tables
WHERE type='U' and object_id NOT IN (select object_id from sys.columns where name='DateCreated')


EXEC (@Dynsql)
日久见人心 2024-10-20 21:44:16

您可以使用此查询
我在此查询中使用 where 子句(可选)来查找所有具有 ID 的表并向其中添加新字段。

您可以更改 where 子句,例如查找具有 BusinessId 列的所有表并添加新字段或删除 where 子句并为所有表添加

DECLARE @SQL varchar(max)

SELECT @SQL= STUFF((SELECT ';' + 'ALTER TABLE ' + t.TABLE_SCHEMA+'.' +t.TABLE_NAME+ ' ADD newfield nvarchar(max)' 

from information_schema.tables t
inner join information_schema.columns c on c.table_name = t.table_name
                                and c.table_schema = t.table_schema
where c.column_name = 'id'
      and t.table_schema not in ('information_schema', 'pg_catalog')
      and t.table_type = 'BASE TABLE'
      FOR XML PATH('')),1,1,'')


EXEC (@SQL)

You can use this query
I use the where clause in this query (it is optional) to find all tables that have ID and add the new field to them.

you can change where clause for example find all tables that have BusinessId column and add new filed or remove where clause and add for all tables

DECLARE @SQL varchar(max)

SELECT @SQL= STUFF((SELECT ';' + 'ALTER TABLE ' + t.TABLE_SCHEMA+'.' +t.TABLE_NAME+ ' ADD newfield nvarchar(max)' 

from information_schema.tables t
inner join information_schema.columns c on c.table_name = t.table_name
                                and c.table_schema = t.table_schema
where c.column_name = 'id'
      and t.table_schema not in ('information_schema', 'pg_catalog')
      and t.table_type = 'BASE TABLE'
      FOR XML PATH('')),1,1,'')


EXEC (@SQL)
我不会写诗 2024-10-20 21:44:16

这个存储过程工作正常

USE databaseName;
exec sp_msforeachtable 'alter table ? Add [DateCreated] datetime not null default getdate()';

This stored procedure works fine

USE databaseName;
exec sp_msforeachtable 'alter table ? Add [DateCreated] datetime not null default getdate()';
梦在夏天 2024-10-20 21:44:16
declare @i int

set @i=1
while(@i<45)
begin
    declare @sql varchar(200)

    with TempTable as (select Row_number() over(order by stdID) as RowNo,* from SomeTable)

    select @sql= 'alter table Table'+(select Name from TempTable where RowNo=@i)+' add NewColumn int'

    exec (@sql)
    set @i=@i+1
end
declare @i int

set @i=1
while(@i<45)
begin
    declare @sql varchar(200)

    with TempTable as (select Row_number() over(order by stdID) as RowNo,* from SomeTable)

    select @sql= 'alter table Table'+(select Name from TempTable where RowNo=@i)+' add NewColumn int'

    exec (@sql)
    set @i=@i+1
end
触ぅ动初心 2024-10-20 21:44:16

不,没有任何一条语句可以将列添加到数据库中的所有表中。

下次请用您正在使用的 RDBMS 标记您的问题。如果有办法的话,我们将无法在不知道您正在使用哪个数据库系统的情况下向您提供命令。

No, there is no single statement that will add a column to all the tables in your database.

Next time please tag your question with the RDBMS you're using. If there were a way, we wouldn't be able to give you the command without knowing which database system you are using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文