[存储过程] 如何赋值

黑马 2003-08-23 07:07:18
我现在想写一个存储过程
从A的a1字段获得一个开始日期,然后从A的a2字段获得结束日期
然后将所有a1和a2之间的月份一个个得到放到B表的b1字段中

请问如何实现,小弟第一次编写存储过程,请多指教!
...全文
29 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑马 2003-08-26
  • 打赏
  • 举报
回复
搞定了:)
pengdali 2003-08-25
  • 打赏
  • 举报
回复
insert B表 (b1) select a1 from 表 where a1 between @开始日期 and @结束日期
黑马 2003-08-25
  • 打赏
  • 举报
回复
up
黑马 2003-08-23
  • 打赏
  • 举报
回复
如果不在同一年的话,光用月份判断不行吧?
sdhdy 2003-08-23
  • 打赏
  • 举报
回复
--不是很规范
create table a(a1 datetime,a2 datetime)
insert a values('2003-01-01','2003-08-23')
go
create table b(b1 int )
go

create proc test
as
declare @begin int,@end int
select @begin=month(a1),@end=month(a2) from a
while @begin<=@end
begin
insert b select @begin
set @begin=@begin+1
end
go
exec test
go
select * from b
j9988 2003-08-23
  • 打赏
  • 举报
回复
insert tabB(b1)
select m from (select 1 as m
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10
union all select 11
union all select 12
) A,(select a1,a2 from TabA where 你的条件) B where m between month(a1) and month(a2)

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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