當前位置:首頁 » 存儲配置 » 時間存儲過程

時間存儲過程

發布時間: 2022-05-27 08:17:22

㈠ MSsql 寫一個存儲過程按時間段進行查詢

/*
主要查詢信息表tc_data表,
createtabletc_data(IDnumeric(22)--ID號
,char_namenvarcher(64)--訂單名稱
,numnumeric(22,3)--數量
,moneynumeric(18,22)--金額
,create_timedatetime--時間
)
寫一個存儲過程參數是條件(@conditionnvarchar(64)),開始時間(@begintimenvarchar(64)),結束時間(@endtimenvarchar(64));
說明:
條件(@conditionnvarchar(64))的內容為:Year、Quarter、Month、Week
開始時間(@begintimenvarchar(64))和結束時間(@endtimenvarchar(64))的內容為YYYY-MM-DD;
查詢時,當條件為Year年時,顯示年的最後一天的記錄,按開始時間和結束時間來確定查詢哪年,如果跨兩個或兩個以上年則顯示各年最後一天的記錄;
當條件為Quarter季度時,顯示季度的最後一天的記錄,按開始時間和結束時間來確定查詢哪個季度,如果跨兩個或兩個以上的季度則顯示各季度最後一天的記錄;
當條件為Month月時,顯示月最後一天的記錄,按開始時間和結束時間來確定查詢哪個月,如果跨兩個或兩個以上的月則顯示各月最後一天的記錄;
當條件為Week周時,顯示周內每天的記錄,按開始時間和結束時間來確定查詢哪個周,如果跨兩個或兩個以上的周則最多顯示四周每天的記錄。
*/
--以下為SQL2005或更高版本,在SQL2000上會出錯的
createprocdbo.Usp_Getdata
(
@conditionasnvarchar(64),--條件
@begintimeasnvarchar(64),--開始時間
@endtimeasnvarchar(64)--結束時間
)
as
begin
setnocounton;
ifdatediff(dd,@begintime,@endtime)<0
begin
raiserror50001'@begintime必須小於@endtime'
return
end
createtable#(begintimedatetimenotnull,endtimedatetimenotnull)
declare@Tcountasint
if@condition='Year'
begin
insertinto#
select
cast(tempYearasvarchar(4))+'-12-31',cast(tempYearasvarchar(4))+'-12-3123:59:59.997'
from(selecttop(datediff(yy,@begintime,@endtime)+1)((row_number()over(orderbyID))-1)+Year(@begintime)tempYearfromsyscolumns)a
end
declare@tempbeginasint
if@condition='Quarter'
begin
set@tempbegin=casewhenMonth(@begintime)>=1andMonth(@begintime)<4then3whenMonth(@begintime)>=4andMonth(@begintime)<7then6whenMonth(@begintime)>=7andMonth(@begintime)<10then9else12end
insertinto#
select
dateadd(m,tempNum*3,dateadd(dd,-1,dateadd(mm,1,cast((cast(Year(@begintime)asvarchar(4))+'-'+cast(@tempbeginasvarchar(2))+'-1')asdatetime)))),dateadd(m,tempNum*3,dateadd(dd,-1,dateadd(mm,1,cast((cast(Year(@begintime)asvarchar(4))+'-'+cast(@tempbeginasvarchar(2))+'-123:59:59.997')asdatetime))))
from(selecttop(datediff(qq,@begintime,@endtime)+1)((row_number()over(orderbyID))-1)tempNumfromsyscolumns)a
end
if@condition='Month'
begin
set@tempbegin=Month(@begintime)
insertinto#
select
dateadd(m,tempNum,dateadd(dd,-1,dateadd(mm,1,cast((cast(Year(@begintime)asvarchar(4))+'-'+cast(@tempbeginasvarchar(2))+'-1')asdatetime)))),dateadd(m,tempNum,dateadd(dd,-1,dateadd(mm,1,cast((cast(Year(@begintime)asvarchar(4))+'-'+cast(@tempbeginasvarchar(2))+'-123:59:59.997')asdatetime))))
from(selecttop(datediff(mm,@begintime,@endtime)+1)((row_number()over(orderbyID))-1)tempNumfromsyscolumns)a
end
if@condition='Week'
begin
declare@tempbegintimeasdatetime
SETDATEFIRST1;
select@tempbegintime=dateadd(dd,(Datepart(wk,@begintime)-1)*7-Datepart(dw,cast(Year(@begintime)asvarchar(4))+'-1-1')+1,cast(Year(@begintime)asvarchar(4))+'-1-1')
--insertinto#
select
dateadd(dd,0,@tempbegintime),convert(varchar(10),dateadd(dd,(casewhen(datediff(wk,@begintime,@endtime)+1)>4then4*7else(Datepart(wk,@endtime)-Datepart(wk,@begintime)+1)*7end),@tempbegintime),120)+'23:59:59.997'
end
selecta.*fromtc_dataainnerjoin#bona.create_timebetweenb.begintimeandb.endtime
droptable#
end
go
/*
--測試
execdbo.Usp_Getdata'Year','2014-01-01','2014-01-01';
execdbo.Usp_Getdata'Quarter','2014-01-01','2015-01-01';
execdbo.Usp_Getdata'Month','2014-01-01','2015-01-01';
execdbo.Usp_Getdata'Week','2014-06-15','2015-01-01';
execdbo.Usp_Getdata'Week','2014-06-15','2014-06-28';
execdbo.Usp_Getdata'Week','2014-01-01','2015-01-01';
*/

㈡ 如何查詢存儲過程開始執行時間和結束時間

需要在存儲過程中定義的時候把開始執行時間和結束時間列印出來。

舉例如下:

創建存儲過程:

createorreplaceprocerep_testasv_begintimevarchar2(20);v_endtimevarchar2(20);v_strvarchar2(10);beginv_begintime:=to_char(sysdate,'yyyy-mm-ddhh24:mi:ss');select'badkano'intov_strfromal;v_endtime:=to_char(sysdate,'yyyy-mm-ddhh24:mi:ss');dbms_output.put_line('開始時間為:'||v_begintime);dbms_output.put_line('結束時間為:'||v_endtime);end;end;

執行存儲過程:


beginp_test;end;

執行結果:

說明:由於樣例存儲過程過於簡單,但在進行過多數據處理的時候,看到的結果會比較明顯。

㈢ sqlserver 包含一個時間區間的存儲過程如何寫

create PROCEDURE ps_test
declare @id varchar(50)
as
begin
declare @from_date datetime
declare @to_date datetime
create table #tt_table (id,output_date)
select @from_date = 開始時間, @to_date = 結束時間
from A
where id = @id
while(@from_date <= @to_date)
begin
insert into #tt_table(id,output_date)
values( @id,@from_date)

DATEADD(day,1,@from_date)
end
select id, output_date
from #tt_table
end
go

㈣ 存儲過程中時間如何比較

可以直接比較的,不過要把他們轉化為字元串類型,如to_char(sysdate,'yyyymmddhh24miss'); 這是把當前時間轉化為指定格式的字元串,比如現在是2009年7月30日0:36分42秒,那麼轉化後即為20090730003642,然後去比較即可,順便告訴你,日期轉化為字元串還能直接減,並且減出來的即為相差天數~~

㈤ 時間統計的一個存儲過程寫法

1.121.12121121212

㈥ 如何創建一個返回當前系統時間的存儲過程

創建:
create procere p_1
as
declare
@date datetime
begin
set @date=getdate()
print @date
end

執行:
exec p_1

㈦ mysql 如何實現時間隔調用存儲過程

crontab
-e0
*
*
*
*
mysql
-e
"call
sp_test()"
對我有用[0]丟個板磚[0]引用舉報管理TOPACMAIN_CHM(acmain)等
級:26更多勛章

㈧ 時間比較的存儲過程

當然可以時間是可以直接比較的
也可以使用 datapart求時間差

㈨ 存儲過程 向date型欄位存入當前系統時間

sysdate是系統時間,它本身就是時間類型了,如果time_last_login是時間類型,那就可以直接使用sysdate。如果你的意思是要轉化成字元串,那函數應該是to_char

㈩ 關於時間的存儲過程

BEGIN
--select CAST(@dt1 AS datetime)
select convert(datetime,@dt1)
--select CAST(@dt2 AS datetime)
select convert(datetime,@dt2)
end

我看了半天 還是覺得會是這里出問題 select convert(datetime,@dt1)到這里沒有賦值,應該就是查詢出來了 其實你直接在@dt的地方用convert(datetime,@dt1)是一樣的,不用這樣

熱點內容
python3默認安裝路徑 發布:2024-09-19 08:50:22 瀏覽:514
環衛視頻拍攝腳本 發布:2024-09-19 08:35:44 瀏覽:416
sqlserveronlinux 發布:2024-09-19 08:16:54 瀏覽:255
編程常數 發布:2024-09-19 08:06:36 瀏覽:951
甘肅高性能邊緣計算伺服器雲空間 發布:2024-09-19 08:06:26 瀏覽:161
win7家庭版ftp 發布:2024-09-19 07:59:06 瀏覽:716
資料庫的優化都有哪些方法 發布:2024-09-19 07:44:43 瀏覽:268
知乎華為編譯器有用嗎 發布:2024-09-19 07:32:20 瀏覽:617
訪問虛擬機磁碟 發布:2024-09-19 07:28:13 瀏覽:668
原地工作演算法 發布:2024-09-19 07:28:07 瀏覽:423