当前位置: 代码迷 >> Sql Server >> sql脚本大神
  详细解决方案

sql脚本大神

热度:3   发布时间:2016-04-24 09:55:26.0
sql脚本求助大神。

create table t1(year int,month int,[key] varchar(5))
go
insert t1
select 2013,12,'A' union all
select 2014,1,'A' union all
select 2014,2,'A' union all
select 2014,3,'A' union all
select 2013,12,'B' union all
select 2014,1,'B' union all
select 2014,2,'B' union all
select 2014,3,'B' union all
select 2014,4,'B' union all
select 2014,5,'B' union all
select 2014,6,'B' union all
select 2014,7,'B' union all
select 2014,8,'B'
go

create type tt as table
(
[key] varchar(5)
--这里其他字段就不创建了,实际业务中有很多其他信息
)
go

create proc proc_test
@year int,
@month int,
@data tt readonly
as
begin
--测试一传入的参数,key为‘B’的记录已经到2014-8不做处理。
--而‘A’的记录是2014-3,则需要补充5条记录,
--如:select 2014,4,'A' union all
--select 2014,5,'A' union all
--select 2014,6,'A' union all
--select 2014,7,'A' union all
--select 2014,8,'A' union all
select * from t1

end
go

--测试一
declare @data tt
insert @data
select 'A' union all
select 'B' 
exec proc_test 2014,8,@data



注释中有说明
------解决思路----------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

没看懂 你这个要干嘛,你需要这些 直接把
表A(只有一列2行,分别是A,B) 表B(只有一年 ,年份),表C (月份)进行CROSS JOIN 连接不就好了。
然后你需要到哪个日期截止 就用个选择条件。就好了。


好吧我没说清楚。
就是实现proc_test存储过程中的注释功能
根据传入的表类型数据往t1中插入数据。

我知道你这个实现功能。但是我说的方法不也是能够实现你的功能吗 

这个脚本实在不知道怎么写

你建立一个表A年月表,里面只有一列(201401,201402.。。)里插入的时候用
 select distinct [key]from t1 cross join a where a.years<把存储过程里面的参数转换下。and not exists ..
------解决思路----------------------
以为tt是你的表,原来是个类型。那直接用@data当左表就行
with n as (
select number
from master..spt_values v
where v.type='p'
)
insert into t1
select 
     (t2.year*12+t2.month+t3.number-1)/12 
    ,(t2.year*12+t2.month+t3.number-1)%12 +1
    ,tt.[key]
from @data tt 
cross apply(select top 1 * from t1 where t1.[key]=tt.[key] order by 1 desc,2 desc) t2
cross apply(select * from n where n.number between 1 and (@year*12+@month-(t2.year*12+t2.month))) t3
  相关解决方案