急救:SQL游标插入数据

wuruijuan 2011-06-12 04:55:24
用游标插入数据问题:

有两张表,
q_enter 记录某个用户对应的快捷操作方式,
字段有
id,account,show_index
1 A01 1
2 A01 2
3 A01 3
4 A02 1
5 A02 2
6 A02 3

相关联的另一个表
u_enter 与上表的ID关联,记录快捷名称
id name
1 KJ1
2 KJ2
3 KJ3
4 KJ1
5 KJ2
6 KJ3

现在如何用SQL语句,最好是游标的方式,向每个用户都能实现增加3个新快捷方式(KJ4,KJ5,KJ6)?
大哥大姐们,帮帮忙!O(∩_∩)O谢谢!
在线等…………
...全文
168 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuruijuan 2011-06-12
  • 打赏
  • 举报
回复
用游标怎么写?id那块逻辑搞不懂
rucypli 2011-06-12
  • 打赏
  • 举报
回复
写个循环就可以
wuruijuan 2011-06-12
  • 打赏
  • 举报
回复
@q_enter 中的id和@u_enter是对应的,这样出来的 @u_enter id是重复的,那每个用户的快捷名称也是重复的呀
dawogui 2011-06-12
  • 打赏
  • 举报
回复
不懂楼主意思,以下列语句帮顶:
create table q_enter(id int,account varchar(10),show_index int)
insert into q_enter select 1,'A01',1
insert into q_enter select 2,'A01',2
insert into q_enter select 3,'A01',3
insert into q_enter select 4,'A02',1
insert into q_enter select 5,'A02',2
insert into q_enter select 6,'A02',3
create table u_enter(id int,name varchar(10))
insert into u_enter select 1,'KJ1'
insert into u_enter select 2,'KJ2'
insert into u_enter select 3,'KJ3'
insert into u_enter select 4,'KJ1'
insert into u_enter select 5,'KJ2'
insert into u_enter select 6,'KJ3'
go
insert into q_enter
select number+7,'A0'+ltrim(number/3+1),number%3+4 from master..spt_values where type='p' and number<6
insert into u_enter
select number+7,'KJ'+ltrim(number%3+4) from master..spt_values where type='p' and number<6
select * from q_enter
select * from u_enter
/*
id account show_index
----------- ---------- -----------
1 A01 1
2 A01 2
3 A01 3
4 A02 1
5 A02 2
6 A02 3
7 A01 4
8 A01 5
9 A01 6
10 A02 4
11 A02 5
12 A02 6

(12 行受影响)

id name
----------- ----------
1 KJ1
2 KJ2
3 KJ3
4 KJ1
5 KJ2
6 KJ3
7 KJ4
8 KJ5
9 KJ6
10 KJ4
11 KJ5
12 KJ6

(12 行受影响)

*/
go
drop table q_enter,u_enter
wuruijuan 2011-06-12
  • 打赏
  • 举报
回复
id不能是重复的吧
叶子 2011-06-12
  • 打赏
  • 举报
回复

declare @q_enter table (id int,account varchar(3),show_index int)
insert into @q_enter
select 1,'A01',1 union all
select 2,'A01',2 union all
select 3,'A01',3 union all
select 4,'A02',1 union all
select 5,'A02',2 union all
select 6,'A02',3

declare @u_enter table (id int,name varchar(3))
insert into @u_enter
select 1,'KJ1' union all
select 2,'KJ2' union all
select 3,'KJ3' union all
select 4,'KJ1' union all
select 5,'KJ2' union all
select 6,'KJ3'

insert into @u_enter
select a.id,b.name from @q_enter a
cross join
(select 'KJ4' as name union select 'KJ5' union select 'KJ6') b

select * from @u_enter order by id,name
/*
id name
----------- ----
1 KJ1
1 KJ4
1 KJ5
1 KJ6
2 KJ2
2 KJ4
2 KJ5
2 KJ6
3 KJ3
3 KJ4
3 KJ5
3 KJ6
4 KJ1
4 KJ4
4 KJ5
4 KJ6
5 KJ2
5 KJ4
5 KJ5
5 KJ6
6 KJ3
6 KJ4
6 KJ5
6 KJ6
*/

游标的效率太差了,这样就可以了。

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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