高分求助!!!关于中间表的问题更新问题

wclhack 2011-03-26 11:59:26

if exists (select * from sysobjects where id = OBJECT_ID('[UserRoles]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [UserRoles]
CREATE TABLE [UserRoles]
(
[UserID] [int] NOT NULL,
[RoleID] [int] NOT NULL
)

我现在有这样一张中间表,需要同时更新UesrID 和RoleID ,UserID是Users的主键,RoleID是Roles主键,我主需要更新UserID和RoleID,而另外两张表的数据保留不变。

求sql语句
...全文
249 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2011-03-26
  • 打赏
  • 举报
回复
不知道要干什么?
最好给出完整的表结构,测试数据,计算方法和正确结果.否则耽搁的是你宝贵的时间。
如果有多表,表之间如何关联?


发帖注意事项
http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281


如果是级联更新则参考如下:

/*
标题:两表通过字段关联进行级联删除。
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-20
地点:广东深圳
*/

create table ta(id int not null)
create table tb(id int , aid int)
insert into ta values(1)
insert into ta values(2)
insert into tb values(1 , 1)
insert into tb values(2 , 2)
insert into tb values(3 , 1)
go

--一、查看原始数据
--ta表的原始数据
select * from ta
/*
id
-----------
1
2
*/
--tb表的原始数据
select * from tb
/*
id aid
----------- -----------
1 1
2 2
3 1
*/

--二、看看没有创建级联删除时的情况(删除ta表id=1的数据,看看是否影响tb表)
delete from ta where id = 1
select * from ta
/*
id
-----------
2
*/
select * from tb
/*
id aid
----------- -----------
1 1
2 2
3 1
*/

--三、恢复原始数据,创建级联删除,删除ta表id=1的数据,看看是否影响tb表
insert into ta values(1)
--为ta创建主健
alter table ta add constraint pk_ta_id primary key (id)
go
--为tb创建外健,并指定级联删除
alter table tb add constraint fk_tb_aid foreign key (aid) references ta(id) on delete cascade
go
delete from ta where id = 1
select * from ta
/*
id
-----------
2
*/
select * from tb
/*
id aid
----------- -----------
2 2
*/

--删除级联约束
alter table tb drop constraint fk_tb_aid
go
--删除测试表
drop table ta , tb
go
--小F-- 2011-03-26
  • 打赏
  • 举报
回复
楼主可以去查询下级联更新的用法
AcHerat 2011-03-26
  • 打赏
  • 举报
回复
[Quote=引用楼主 wclhack 的回复:]
SQL code

if exists (select * from sysobjects where id = OBJECT_ID('[UserRoles]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [UserRoles]
CREATE TABLE [UserRoles]
(
[UserID] [int] N……
[/Quote]

这个分别在另外两张表上做修改的触发器!


create trigger t_up on Users
after update
as
update a
set a.userID = c.userID
from UserRoles a,deleted b,inserted c
where b.id = c.id and a.userID = b.userID

--id是自增列,如果Users没有其他能够标识userID变化的,估计没办法!
wclhack 2011-03-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jornye 的回复:]

SQL code
if exists (select * from sysobjects where id = OBJECT_ID('[Users]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [Users]
create table Users(userid numeric(4),username varchar(10))……
[/Quote]

我是说只想更改UserID RoleID
----------- -----------
1 100
2 100
3 100
1 101
2 101
3 101
1 102
2 102
3 102

比喻1 100改为2 100 或者 2 200 或者 。。。总之是更新Userid 和RoleID 另外两张表数据不变
jornye 2011-03-26
  • 打赏
  • 举报
回复
if exists (select * from sysobjects where id = OBJECT_ID('[Users]') and OBJECTPROPERTY(id, 'IsUserTable') = 1) 
DROP TABLE [Users]
create table Users(userid numeric(4),username varchar(10));
insert into users values(1,'a');
insert into users values(2,'b');
insert into users values(3,'c');

if exists (select * from sysobjects where id = OBJECT_ID('[Roles]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [Roles]

create table Roles(roleid numeric(3),rolename varchar(12));
insert into Roles values(100,'r1');
insert into Roles values(101,'r2');
insert into Roles values(102,'r3');
select * from users;
select * from roles;

if exists (select * from sysobjects where id = OBJECT_ID('[UserRoles]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [UserRoles]
CREATE TABLE [UserRoles]
(
[UserID] [int] NOT NULL,
[RoleID] [int] NOT NULL
)

insert into UserRoles select t1.userid,t2.roleid from users t1,roles t2;
select * from UserRoles;

userid username
------ ----------
1 a
2 b
3 c

(所影响的行数为 3 行)

roleid rolename
------ ------------
100 r1
101 r2
102 r3

(所影响的行数为 3 行)


(所影响的行数为 9 行)

UserID RoleID
----------- -----------
1 100
2 100
3 100
1 101
2 101
3 101
1 102
2 102
3 102

(所影响的行数为 9 行)
jornye 2011-03-26
  • 打赏
  • 举报
回复
是不是要这个??
insert into UserRoles select t1.userid,t2.roleid from users t1,roles t2;


27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧