我正在尝试一次更改多个 SQL Server 2008 R2 表。
这是我的代码:
use DatabaseName
go
Declare @SchemaUsed varchar(20) = 'dbo'
create table #Tables
(
TableName varchar(100), Processed int
)
insert into #Tables
select top 1 table_name, 0
from INFORMATION_SCHEMA.TABLES
where TABLE_SCHEMA = @SchemaUsed
and table_type = 'Base Table'
and (TABLE_NAME like 'PM%' )
ORDER BY TABLE_NAME
DECLARE @TableName varchar(max)
DECLARE @SQL varchar(max)
WHILE EXISTS (select top 1 'x' from #Tables where Processed = 0)
BEGIN
SET @TableName = (select top 1 TableName from #Tables where Processed = 0)
Set @SQL = 'ALTER TABLE ' + @SchemaUsed + '.' + @TableName + ' ADD [identityID] bigint IDENTITY(1, 1) NOT NULL '
-- Set @SQL = '''' + @SQL + ''''
Print @SQL
EXEC @SQL;
update #Tables
set Processed = 1
where TableName = @TableName
END
drop table #Tables
我无法让它发挥作用来挽救我的生命并出现以下错误:
查找错误 - SQL Server 数据库错误:名称“ALTER TABLE dbo.PM1GTVLV ADD [identityID] bigint IDENTITY(1, 1) NOT NULL”不是有效标识符。
我也尝试过多种字符串变体并使用sp_executesql
。
有人能指出我哪里出错了吗?