您的位置:程序门 -> ms-sql server -> 基础类



在开一贴:同样的问题--从sql server 数据库中 a表插入的b表


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


在开一贴:同样的问题--从sql server 数据库中 a表插入的b表[已结贴,结贴人:haok123]
发表于:2007-05-30 11:04:44 楼主
--
insert   into   a(id,name)
select       'maxid ',name     from     b  
--关键是这个   'maxid '   不在b表中,我是用这个存储过程   计算的id
-----------------------------------------------------------
alter     procedure   dbo.alloc_id
@tbnamevarchar(32)
as
begin
begin   transaction
update   table_alloc_id   set   id   =   id   +   1   where   tablename   =   @tbname
if   @@rowcount   =   0
begin
insert   into   table_alloc_id   values(@tbname,   1)
end
select   id   from   table_alloc_id   where   tablename   =   @tbname
commit   transaction
end
------------------------------------------------------------------
比如b表中有50条数据,那么上面这个alloc_id   存储过程就应该循环计算id,并插入到a表中?
请问该怎么写sql   语句?
发表于:2007-05-30 11:05:571楼 得分:0
http://community.csdn.net/expert/topic/5564/5564379.xml?temp=.3065607
还有这个帖子的,问题是一样的,解决两个分一起奉上。
发表于:2007-05-30 11:31:532楼 得分:0
如楼上所述~
发表于:2007-05-30 11:36:503楼 得分:0
topfans(爱上了tcmm)   (   )   信誉:100         blog       加为好友     2007-05-30   11:31:53     得分:   0    
 
 
      如楼上所述~


--------------------------------
这家伙使用自动回复的?
发表于:2007-05-30 11:38:074楼 得分:50
declare   @name
declare   my_cursor   cursor   scroll   dynamic
for
select   name
from   b

open   my_cursor--打开游标

fetch   next   from   my_cursor   into   @name
while(@@fetch_status=0)
begin
EXEC   alloc_id   'b ',@this_id   output--存储过程,用来生成表id
insert   into   a(id,name)
        select       @this_id,name  
        from   b
where   current   of   my_cursor  
fetch   next   from   my_cursor   into   @name
end

close   my_cursor--关闭游标
deallocate   my_cursor--删除游标
发表于:2007-05-30 11:38:455楼 得分:0
alter     procedure   dbo.alloc_id
@tbname varchar(32)
@get_id varchar(50)   output
as
begin
begin   transaction
update   table_alloc_id   set   id   =   id   +   1   where   tablename   =   @tbname
if   @@rowcount   =   0
begin
insert   into   table_alloc_id   values(@tbname,   1)
end
select   @get_id=max(id)   from   table_alloc_id   where   tablename   =   @tbname
commit   transaction
end
发表于:2007-05-30 17:38:126楼 得分:0
这边也顶下。


快速检索

最新资讯
热门点击