我用PowerDesigner做数据库设计,明明每个表我都指定了用户,但建上去以后,有两个表,一个是这个指定用户的,一个是dbo的。数据有些保存在dbo用户下,有些又在我建的这个用户里面。不知道什么原因,请大侠指点指点,现在怎么改变一下,保留我建的这个用户下面的所有表。怎么操作比较合理。if exists (select 1
from sysobjects
where id = object_id('sq_tangxiangren.EC_CSS')
and type = 'U')
drop table sq_tangxiangren.EC_CSS
go/*==============================================================*/
/* Table: EC_CSS */
/*==============================================================*/
create table sq_tangxiangren.EC_CSS (
pk int not null,
name varchar(50) null,
cssadress varchar(50) null,
re varchar(200) null,
isntavaile varchar(4) null,
isnthavepic varchar(4) null,
csspic image null,
constraint PK_EC_CSS primary key (pk)
)
goexecute sp_addextendedproperty 'MS_Description',
'CSS方案维护表',
'user', 'sq_tangxiangren', 'table', 'EC_CSS'
goexecute sp_addextendedproperty 'MS_Description',
'主键',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'pk'
goexecute sp_addextendedproperty 'MS_Description',
'名称',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'name'
goexecute sp_addextendedproperty 'MS_Description',
'css地址',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'cssadress'
goexecute sp_addextendedproperty 'MS_Description',
'说明',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 're'
goexecute sp_addextendedproperty 'MS_Description',
'是否有效',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'isntavaile'
goexecute sp_addextendedproperty 'MS_Description',
'是否有图像',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'isnthavepic'
goexecute sp_addextendedproperty 'MS_Description',
'图像',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'csspic'
go
from sysobjects
where id = object_id('sq_tangxiangren.EC_CSS')
and type = 'U')
drop table sq_tangxiangren.EC_CSS
go/*==============================================================*/
/* Table: EC_CSS */
/*==============================================================*/
create table sq_tangxiangren.EC_CSS (
pk int not null,
name varchar(50) null,
cssadress varchar(50) null,
re varchar(200) null,
isntavaile varchar(4) null,
isnthavepic varchar(4) null,
csspic image null,
constraint PK_EC_CSS primary key (pk)
)
goexecute sp_addextendedproperty 'MS_Description',
'CSS方案维护表',
'user', 'sq_tangxiangren', 'table', 'EC_CSS'
goexecute sp_addextendedproperty 'MS_Description',
'主键',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'pk'
goexecute sp_addextendedproperty 'MS_Description',
'名称',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'name'
goexecute sp_addextendedproperty 'MS_Description',
'css地址',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'cssadress'
goexecute sp_addextendedproperty 'MS_Description',
'说明',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 're'
goexecute sp_addextendedproperty 'MS_Description',
'是否有效',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'isntavaile'
goexecute sp_addextendedproperty 'MS_Description',
'是否有图像',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'isnthavepic'
goexecute sp_addextendedproperty 'MS_Description',
'图像',
'user', 'sq_tangxiangren', 'table', 'EC_CSS', 'column', 'csspic'
go
create table 库名.所有者.表名
DECLARE @CursorVar CURSOR
declare @tablename varchar(40)
declare @tb1 varchar(40)
declare @dlycnt int --IsProcedure
DECLARE Bti_cursor CURSOR FOR
select name from dbo.sysobjects where OBJECTPROPERTY(id, N'IsTable') = 1 and uid <> 1 --(默认情况下1就是指sa用户)
set @CursorVar = Bti_cursor
open @CursorVar
set @dlycnt = 0
FETCH NEXT FROM @CursorVar into @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
select @tb1 = 'ccuser.'+ @tablename --修改USER为你的帐户,必须以这个帐号登陆,因为SA已经不能访问了。
EXEC sp_changeobjectowner @tb1,'dbo'
select @dlycnt = @dlycnt + 1
FETCH NEXT FROM @CursorVar into @tablename
END
CLOSE Bti_cursor
SET QUOTED_IDENTIFIER OFF
GO
方法二
--使用dbo.sp_msforeachtable
exec dbo.sp_msforeachtable
@command1=N'exec dbo.sp_changeobjectowner N''*'',N''wangshixing''',
@replacechar=N'*',
@whereand=N' and uid=1',
@precommand=N' print ''处理之前执行的SQL语句''',
@postcommand=N' print ''处理之后执行的SQL语句'''
比如当前所有者为CCCC,
那么就以CCCC登录。
-新增用户
exec sp_addlogin 'test' --添加登录
exec sp_grantdbaccess N'test' --使其成为当前数据库的合法用户
exec sp_addrolemember N'db_owner', N'test' --授予对自己数据库的所有权限 --这样创建的用户就只能访问自己的数据库,及数据库中包含了guest用户的公共表
go --删除测试用户
exec sp_revokedbaccess N'test' --移除对数据库的访问权限
exec sp_droplogin N'test' --删除登录
建表和你登陆的用户有关,如果你是SA登陆查询分析器的话,那么建的表的所有者为dbo.
如果是sq_tangxiangren的话,建的表的所有着就是sq_tangxiangren.
可以用sp_changeobjectowner [ @objname = ] 'object' , [ @newowner = ] 'owner'
修改对象的所有者。
或者用select into