我有一个用户 ID 为 PK 的表“用户”。我需要将其作为 FK 添加到多个现有表的 2 列中。现在,我正在更新每个表。方法。
alter table table1
add foreign key(createdby) references users(id)
go
alter table table1
add foreign key(createdby) references users(id)
go
有没有一种更简单的方法可以一次性更新所有表。谢谢你。
我有一个用户 ID 为 PK 的表“用户”。我需要将其作为 FK 添加到多个现有表的 2 列中。现在,我正在更新每个表。方法。
alter table table1
add foreign key(createdby) references users(id)
go
alter table table1
add foreign key(createdby) references users(id)
go
有没有一种更简单的方法可以一次性更新所有表。谢谢你。
这是针对另一个具有不同语法和不同 DDL 更改但结构相同的数据库。声明一个游标,查询数据字典表并返回要运行的 SQL 语句,然后在执行每个语句时打开并循环游标。
DECLARE SQLStr SHORTSTRING(1000);
DECLARE NoDefaultsCursor CURSOR FOR
SELECT 'ALTER TABLE "' || TABLE_NAME ||
'" ALTER COLUMN "' || FIELD_NAME || '" SET DEFAULT ' ||
CASE FIELD_TYPE_SQL
WHEN 'INTEGER' THEN '0'
WHEN 'BOOLEAN' THEN 'FALSE'
WHEN 'SHORTSTRING' THEN ''''''
WHEN 'CHARACTER VARYING' THEN ''''''
END || ';' AS AddDefaultSQL
FROM #FIELDS WHERE NOT FIELD_HASDEFAULT
AND FIELD_TYPE_SQL IN
('INTEGER','BOOLEAN','SHORTSTRING','CHARACTER VARYING');
OPEN NoDefaultsCursor;
FETCH NEXT FROM NoDefaultsCursor INTO SQLStr;
WHILE @@FETCH_STATUS = 0 DO
Execute Immediate SQLStr;
FETCH NEXT FROM NoDefaultsCursor INTO SQLStr;
END WHILE;
CLOSE NoDefaultsCursor;
也许这样的事情会成功!
DECLARE @TableName AS NVARCHAR(128)
DECLARE @Schema AS NVARCHAR(128)
DECLARE @Modify AS INTEGER
DECLARE @Created AS INTEGER
DECLARE @sql1 as nvarchar(max)
DECLARE @sql2 as nvarchar(max)
DECLARE curs CURSOR FOR
SELECT
A.[name],
S.[name] as schemaname,
(SELECT 1 FROM sys.all_objects innero
where innero.[name] = 'FK_ModifiedBy'
AND innero.parent_object_id = a.[object_id]) as ModifiedBy,
(SELECT 1 FROM sys.all_objects innero
where innero.[name] = 'FK_CreatedBy'
AND innero.parent_object_id = a.[object_id]) as CreatedBy
FROM
sys.all_objects A
INNER JOIN
sys.all_columns O ON
A.[object_id] = O.[object_id]
INNER JOIN
SYS.schemas S ON
A.[schema_id] = S.[schema_id]
WHERE
O.name = 'ModifiedBy'
OPEN curs
FETCH NEXT FROM curs
INTO @TableName, @Schema, @Modify, @Created
WHILE @@FETCH_STATUS = 0
BEGIN
IF @Modify IS NULL
BEGIN
set @sql1 = N'ALTER TABLE ' + @Schema + '.' + @TableName
+ ' ADD CONSTRAINT FK_ModifiedBy FOREIGN KEY (ModifiedBy)
REFERENCES ' + @Schema + '.Users(userid)'
exec SP_EXECUTESQL @sql1
END
IF @Created IS NULL
BEGIN
set @sql2 = N'ALTER TABLE ' + @Schema + '.' + @TableName
+ ' ADD CONSTRAINT FK_CreatedBy FOREIGN KEY (createdby)
REFERENCES ' + @Schema + '.Users(userid)'
exec SP_EXECUTESQL @sql2
END
FETCH NEXT FROM curs
INTO @TableName, @Schema, @Modify, @Created
END
CLOSE curs
DEALLOCATE curs
抱歉,在 sql 构建中缩进不好,在这里使它变得好似乎有点麻烦。如果某些表只有其他两列之一,这可能需要稍微调整。
我还尝试了一个紧凑版本的 christer。这也很有效。认为可能对其他人有帮助
declare @tblname nvarchar(50),@sql1 nvarchar(max),@sql2 nvarchar(max)
declare curs cursor for
select TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME ='createdby'
open curs
fetch next from curs into @tblname
while @@FETCH_STATUS=0
begin
set @sql1='alter table dbo.'+ @tblname+
' add foreign key(createdby) references users(id)'
exec sp_executesql @sql1
set @sql2='alter table dbo.'+@tblname+
' add foreign key(modifiedby) references users(id)'
exec sp_executesql @sql2
fetch next from curs into @tblname
end
close curs
deallocate curs