---
title: "一次性更改数据库中所有用户表名称"
author: "hipeace87"
date: "2009-03-04T16:29:17Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2009-03-04T16:29:43Z"
id: 343029
url: "https://hipeace87.iteye.com/blog/343029"
---

从网上下了一个系统,要想把数据库和项目中用到的整合到一起,不过会有重名发生

想把所有表都加上一个前缀,不过一个一个改又麻烦.就写了这个东西

```
declare curRt  cursor
for
select [name] from sysobjects where xtype = 'U' and [name]<> 'dtproperties'
declare @tn nvarchar(20),@sql nvarchar(2000)
open curRt
fetch curRt into @tn

while @@fetch_status =0
begin
  select @sql = N'exec sp_rename ''' + @tn + ''', ''thr_' + @tn +''',''OBJECT'''     ---这里加的前缀为thr_
  execute @sql
  fetch next from curRt into @tn
end

close curRt
deallocate curRt
```

  sp\_rename和execute功能都很强大很有帮助

希望大家多交流。共同进步