欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

MsSql

當(dāng)前位置:主頁(yè) > 數(shù)據(jù)庫(kù) > MsSql >

sql server使用公用表表達(dá)式CTE通過(guò)遞歸方式編寫(xiě)通用函數(shù)自動(dòng)生成連續(xù)數(shù)字和日期

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:MsSql|點(diǎn)擊: 次

問(wèn)題:

在數(shù)據(jù)庫(kù)腳本開(kāi)發(fā)中,有時(shí)需要生成一堆連續(xù)數(shù)字或者日期,例如yearly report就需要連續(xù)數(shù)字做年份,例如daily report就需要生成一定時(shí)間范圍內(nèi)的每一天日期。

而自帶的系統(tǒng)表master..spt_values存在一定的局限性,只是從0到2047(驗(yàn)證腳本:select * from master..spt_values b where b.type = 'P'),也不能直接生成連續(xù)日期。

可能大部分人會(huì)想到一個(gè)笨辦法,通過(guò)while循環(huán)去逐條插入數(shù)據(jù)到臨時(shí)表,每次數(shù)字加1或者日期加1天,但這樣和數(shù)據(jù)庫(kù)服務(wù)器的交互就太頻繁了。如果生成1W個(gè)連續(xù)數(shù)字,那就要跟數(shù)據(jù)庫(kù)服務(wù)器交互1W次,可怕!如果是有1000個(gè)客戶端都需要調(diào)用這個(gè)while循環(huán),那就是1000W次!可怕!

解決方案:

可以使用公用表表達(dá)式CTE通過(guò)遞歸方式實(shí)現(xiàn),并編寫(xiě)為一個(gè)通用表值函數(shù)方便調(diào)用,封裝起來(lái)簡(jiǎn)化使用,返回表格式數(shù)據(jù)。

CTE是在內(nèi)存中準(zhǔn)備好數(shù)據(jù),而不是每次一條往返服務(wù)器和客戶端一次。如果需要再插入到臨時(shí)表的話就是全部數(shù)據(jù)一次性插入。

如果傳入?yún)?shù)為數(shù)字,則生成連續(xù)數(shù)字;如果傳入?yún)?shù)為日期,則生成連續(xù)日期。
是不是覺(jué)得很方便呢?

函數(shù)腳本:

if object_id('dbo.fun_ConcatStringsToTable') is not null drop function dbo.fun_ConcatStringsToTable
go
/*
  功能:連續(xù)字符串(數(shù)字或日期)以table形式返回
  作者:zhang502219048 2018-12-10
  腳本來(lái)源:https://www.cnblogs.com/zhang502219048/p/11108991.html 
-- 示例1(數(shù)字):
  select * from dbo.fun_ConcatStringsToTable(1, 10000)
-- 示例2(數(shù)字文本):
  select * from dbo.fun_ConcatStringsToTable('1', '10000')
-- 示例3(日期):
  declare @dateBegin datetime = '2009-1-1', @dateEnd datetime = '2018-12-31'
  select * from dbo.fun_ConcatStringsToTable(@dateBegin, @dateEnd)
-- 示例4(日期文本):
  select * from dbo.fun_ConcatStringsToTable('2009-1-1', '2018-12-31')
**/
create function [dbo].[fun_ConcatStringsToTable]
(
  @strBegin as nvarchar(100),
  @strEnd as nvarchar(100)
)
returns @tempResult table (vid nvarchar(100))
as
begin
  --數(shù)字
  if isnumeric(@strBegin) = 1 and isnumeric(@strEnd) = 1
  begin
    --使用CTE遞歸批量插入數(shù)字?jǐn)?shù)據(jù)
    ;with cte_table(id) as
    (
      select cast(@strBegin as int)
      union all
      select id + 1
      from cte_table
      where id < @strEnd
    )
    insert into @tempResult
    select cast(id as nvarchar(100))
    from cte_table 
    option (maxrecursion 0)
  end
  --日期
  else if isdate(@strBegin) = 1 and isdate(@strEnd) = 1
  begin
    --使用CTE遞歸批量插入日期數(shù)據(jù)
    ;with cte_table(CreatedDate) as
    (
      select cast(@strBegin as datetime)
      union all
      select dateadd(day, 1, CreatedDate)
      from cte_table
      where CreatedDate < @strEnd
    )
    insert into @tempResult
    select convert(varchar(10), CreatedDate, 120)
    from cte_table
    option (maxrecursion 0)
  end

  return;
end
go

調(diào)用函數(shù)示例:

-- 示例1(數(shù)字):
  select * from dbo.fun_ConcatStringsToTable(1, 10000)
-- 示例2(數(shù)字文本):
  select * from dbo.fun_ConcatStringsToTable('1', '10000')
-- 示例3(日期):
  declare @dateBegin datetime = '2009-1-1', @dateEnd datetime = '2018-12-31'
  select * from dbo.fun_ConcatStringsToTable(@dateBegin, @dateEnd)
-- 示例4(日期文本):
  select * from dbo.fun_ConcatStringsToTable('2009-1-1', '2018-12-31')

腳本運(yùn)行結(jié)果:


 

  結(jié)論:

從上面幾個(gè)圖可以看到,通過(guò)簡(jiǎn)單調(diào)用fun_ConcatStringsToTable這個(gè)自定義表值函數(shù),指定起止數(shù)字或日期,就達(dá)到了生成連續(xù)數(shù)字和日期的目的。

擴(kuò)展:

如果想生成連續(xù)月份呢?博主在這里也幫大家寫(xiě)了一下腳本,如果需要可以在此基礎(chǔ)上再自行做成表值函數(shù):

with cte_table(CreatedDate) as
(
  select cast('2017-12-1' as datetime)
  union all
  select dateadd(month, 1, CreatedDate)
  from cte_table
  where CreatedDate < '2018-04-01'
)
select convert(varchar(7), CreatedDate, 120) as YearMonth
from cte_table
option (maxrecursion 0)

總結(jié)

以上所述是小編給大家介紹的sql server使用公用表表達(dá)式CTE通過(guò)遞歸方式編寫(xiě)通用函數(shù)自動(dòng)生成連續(xù)數(shù)字和日期 ,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有