sql存储过程 循环插入数据

mengdett 2009-12-25 10:28:26
CREATE PROCEDURE insertsql
AS
BEGIN
declare @n int ,@date ,
declare @count int
select @count=(select count(标号) from 维护班组)
select @n=1
select @date=select convert(varchar(100),getdate(),23)

while @n<=@count
begin
declare @companyname=select 单位 from 生产汇报索引表
--这里我做循环读取的时候,肯定只能读取一行数据
--select 班组 from 生产汇报索引表 where 标号=?
--我想明白这里怎么处理
declare @classname= select 班组 from 生产汇报索引表
insert 生产汇报索引表 (时间,时段,单位,班组)values(@date,'白班',)
end
END
GO
...全文
422 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengdett 2009-12-25
  • 打赏
  • 举报
回复
不好意思。
table A 中有数据
标号, 单位, 班组
1, a, a1
2, b, b1
3, a, a2
我想把插入到table b 中
标号 单位, 班组 , 索引号(table A 中的标号) 时段
1, a a1 1 AA
2 a a1 1 BB
3 a a2 2 AA
4 a a3 2 BB
这样子才是最终效果哦 用SQL存储过程怎么处理 请再指导一下
忆轩辕 2009-12-25
  • 打赏
  • 举报
回复


--上一个列没对齐.........
insert into b(索引号,单位,班组)
select 标号,单位,班组 from a


忆轩辕 2009-12-25
  • 打赏
  • 举报
回复



--这样?
insert into b(单位,班组,索引号)
select 标号,单位,班组 from a


mengdett 2009-12-25
  • 打赏
  • 举报
回复
麻烦大侠再指导一下了
mengdett 2009-12-25
  • 打赏
  • 举报
回复
不好意思。
table A 中有数据
标号, 单位, 班组
1, a, a1
2, b, b1
3, a, a2
我想把插入到table b 中
标号 单位, 班组 , 索引号(table A 中的标号)

用SQL存储过程怎么处理
dawugui 2009-12-25
  • 打赏
  • 举报
回复
你要给变量赋值,可以这样

select @companyname = 单位 from 生产汇报索引表
select @classname = 班组 from 生产汇报索引表 where 标号=@n

另外最后要加
set @n = @n + 1
不然死循环了.

其他的需求没看懂.
aimee_99 2009-12-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sgtzzc 的回复:]
SQL codeinsert 生产汇报索引表 (时间,时段,单位,班组)select@date,'白班','单位?',班组from 生产汇报索引表where 标号=?
[/Quote]
UP
bancxc 2009-12-25
  • 打赏
  • 举报
回复
insert 生产汇报索引表 (时间,时段,单位,班组)values(@date,'白班',)
select @date,'白班',@companyname,班组
from 生产汇报索引表 where 标号=?
??
sgtzzc 2009-12-25
  • 打赏
  • 举报
回复
insert 生产汇报索引表 (时间,时段,单位,班组)
select @date,'白班','单位?',班组 from 生产汇报索引表 where 标号=?
--小F-- 2009-12-25
  • 打赏
  • 举报
回复
用动态语句??没有看明白你想做什么?
dawugui 2009-12-25
  • 打赏
  • 举报
回复
很乱

最好给出完整的表结构,测试数据,计算方法和正确结果.


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

34,594

社区成员

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

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