MSSQL 构建临时表SQL
Posted DrHao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MSSQL 构建临时表SQL相关的知识,希望对你有一定的参考价值。
declare @StartQuarter int set @StartQuarter =4 declare @StartYear int set @StartYear=2013 declare @EndQuarter int set @EndQuarter=4 declare @EndYear int set @EndYear=2014 declare @StartRange int, @EndRange int, @SYear int, @SQuarter int set @SYear=@StartYear set @SQuarter=@StartQuarter set @StartRange=convert(int,convert(varchar(4),@SYear)+convert(varchar(4),@SQuarter)) set @EndRange=CONVERT(int,CONVERT(varchar(4),@EndYear)+CONVERT(varchar(4),@EndQuarter)) declare @Quarter table ( yearQuarter int not null, yearQuarterName varchar(30), sortFlg int ) declare @qCount int set @qCount=(@[email protected])*4+(@EndQuarter-@StartQuarter) declare @sortFlg int set @sortFlg=1 insert into @Quarter values(CONVERT(varchar(4),@SYear)+CONVERT(varchar(4),0), ‘Prior to FY‘+CONVERT(varchar(4),@SYear),0) while @qCount>=0 begin declare @name varchar(30) if @SQuarter=1 set @name=(‘Q1 FY‘+convert(varchar(4),@SYear)+‘ Apr ‘+RIGHT(convert(varchar(4),@SYear),2)+‘ to Jun ‘+RIGHT(@SYear,2)) else if @SQuarter=2 set @name=(‘Q2 FY‘+convert(varchar(4),@SYear)+‘ Jul ‘+RIGHT(convert(varchar(4),@SYear),2)+‘ to Sep ‘+RIGHT(@SYear,2)) else if @SQuarter=3 set @name=(‘Q3 FY‘+convert(varchar(4),@SYear)+‘ Oct ‘+RIGHT(convert(varchar(4),@SYear),2)+‘ to Dec ‘+RIGHT(@SYear,2)) else if @SQuarter=4 set @name=(‘Q4 FY‘+convert(varchar(4),@SYear)+‘ Jan ‘+RIGHT(convert(varchar(4),@SYear),2)+‘ to Mar ‘+RIGHT(@SYear,2)) insert into @Quarter values(convert(varchar(4),@SYear)+convert(varchar(4),@SQuarter), @name,@sortFlg) set @[email protected]+1 if @SQuarter%5=0 begin set @[email protected]+1 set @SQuarter=1 end set @[email protected]1 set @[email protected]+1 end select yearQuarter,yearQuarterName,sortFlg from @Quarter
以上是关于MSSQL 构建临时表SQL的主要内容,如果未能解决你的问题,请参考以下文章