在SQL Server 2005中我要尋找使用通配符來重命名了很多表, 例如,表名是:查詢到重命名錶
OrderItem
OrderItems
OrderItemss
我想他們被重命名爲:
OrderItem2
OrderItems2
OrderItemss2
這可以使用通配符完成,所以我不必指定完整的表名稱?謝謝。
在SQL Server 2005中我要尋找使用通配符來重命名了很多表, 例如,表名是:查詢到重命名錶
OrderItem
OrderItems
OrderItemss
我想他們被重命名爲:
OrderItem2
OrderItems2
OrderItemss2
這可以使用通配符完成,所以我不必指定完整的表名稱?謝謝。
您可以使用系統表選擇與您的圖案匹配的所有表名稱,例如'OrderItem%',或更復雜。然後使用動態sql更新名稱使用exec SP_RENAME 'old_name' 'new_name'
。
但是,這不會對查詢表的存儲過程生效。這些存儲過程需要在重命名完成後重新編譯。
解決方案的Sql
好,這裏是整個代碼。它基於表名以「OrderItem」開頭,並且它們都應具有帶「2」後綴的原始名稱。在重命名錶之前,我創建了一個表變量來存放找到的表。如果你「取消註釋」選擇語句,只執行到該語句,你可以看到選擇哪些表被重命名,以及它們將被重命名。 注意,我只是從dbo架構中選擇表。
declare @OldTableName nvarchar(max)
declare @NewTableName nvarchar(max)
declare @SqlStmt nvarchar(max)
declare @Tables table
(
OldTableName nvarchar(max),
NewTableName nvarchar(max)
)
insert into @Tables (OldTableName, NewTableName)
select
name as OldTableName ,
name + '2' as NewTableName
from sys.tables
where name like 'OrderItem%'
and schema_id = SCHEMA_ID('dbo')
-- select * from @Tables
declare cur cursor for
select OldTableName,
NewTableName
from @Tables
open cur
while (1=1)
begin
fetch next from cur into @OldTableName, @NewTableName
if @@FETCH_STATUS <> 0
break
set @SqlStmt = N'sp_rename ''' + @OldTableName + ''', ''' + @NewTableName + ''''
exec sp_executesql @SqlStmt
end
close cur
deallocate cur
新24apr2013
,包括架構名稱,而不是隻着眼於dbo
模式
select
s.name + '.' + t.name as OldTableName,
t.name + '2' as NewTableName
from sys.tables as t
inner join sys.schemas as s on t.schema_id = s.schema_id
where t.name like 'OrderItem%'
新增
查找範圍sys.tables
所有表(Select語句記得只選擇t要重命名的模式中的那些)。將名稱和您提供的新表名一起插入臨時表中,然後遍歷每個名稱,生成一個sql字符串,並調用sp_rename
並使用exec sp_executesql 'SQL STMT'
進行實際執行。
你可以得到你所需要的東西,例如用如下查詢篩選的所有用戶表的列表:
select name from sysobjects where type = 'U' and name like 'user%'
然後使用sp_executesql
和光標向查詢的結果執行動態SQL( MSDN)執行實際的重命名。
希望有所幫助。
從SQL Server 2005開始,我建議使用像'sys.tables'這樣的*更有針對性的*目錄視圖,而不是僅僅使用「catch all」'sysobjects'(並且btw:'sysobjects'在2005年被棄用 - 使用'sys.objects'如果你必須的) – 2013-04-23 14:24:40
marc_s我想明白爲什麼我有一個問題運行幾個select命令來引用表。使用sys.tables-它只是將第二部分的表名帶回來。完整的表名是Customers.OrderItems.I,與舊版SQL2000一起工作,我通常不會看到像這樣的表名。所以我知道不建議重命名錶,但我只是這樣做來學習和必須合併具有相同表名的數據庫。但基於aweis的評論,它看起來像我必須回去閱讀基本教程第一。現在我將等待來自Aewis的SQL Lesson鏈接。謝謝。 – user1804387 2013-04-24 16:59:51
首先,所有查找您的數據庫(我有3) 表中的數量,並把這個代碼存儲過程中,也可以把參數!
`alter procedure [dbo].[changetableName]
as
declare @sql varchar(8000), @table varchar(1000), @oldTable varchar(1000),
@newTable
varchar(1000);
declare @Inc integer;
set @Inc=1;
set @oldTable = '%OrderItem%'
set @newTable = 'OrderItem_n'
while exists(SELECT object_id, t.name AS table_name FROM sys.tables AS t where
t.name Like @oldTable) and (@Inc<=3)
begin
select @table = name from sys.tables
where object_id in(select object_id from sys.tables as t where t.name like
@oldTable)
set @sql = 'sp_rename ' + @table + ' , ' + @newTable + CONVERT(varchar(2),@Inc)
exec(@sql)
SET @Inc = @Inc + 1;
select @sql
end`
感謝您的代碼。我收到一個錯誤: – user1804387 2013-04-23 16:45:27
請注意,您正在爲這些表分配一個仍然與該模式匹配的新名稱,因此,您可能會多次在同一個表中使用同一個表,因爲您在循環中繼續使用sys.table。 – aweis 2013-04-23 17:00:12
ok aweis,我很快就做了這段代碼,沒有測試它 – 2013-04-23 20:14:57
正是這種簡單:
DECLARE @SQL nvarchar(max);
SELECT
@SQL = Coalesce(@SQL + Char(13) + Char(10), '') + 'EXEC sp_rename '''
+ Replace(QuoteName(TABLE_SCHEMA) + '.' + QuoteName(TABLE_NAME), '''', '''''')
+ ''', ''' + Replace(TABLE_NAME + '2', '''', '''''') + ''';'
FROM INFORMATION_SCHEMA.TABLES T
WHERE
T.TABLE_TYPE = 'BASE TABLE'
AND T.TABLE_NAME LIKE 'OrderItem%'
;
EXEC (@SQL);
感謝您的答覆。我不想重新命名錶格。我必須爲許多表執行此過程,我在問是否可以使用通配符執行此操作,因此我不必爲以相同字母開頭的表指定完整表名。 – user1804387 2013-04-23 15:05:58
這就是我所建議的。你選擇與你的通配符匹配的sys.table中的所有表格,並重命名它們! – aweis 2013-04-23 15:17:34
感謝您的跟進。我現在可以在沒有錯誤的情況下運行代碼。我不知道如何執行第二部分。你可以詳細說明如何通過調用sp_rename遍歷它們中的每一個嗎? – user1804387 2013-04-23 23:43:27