目前工作中几个常用的存储过程

本文介绍如何使用SQL创建节假日表,并实现判断指定日期是否为有行情的工作日及查找最近一个有行情的日期的存储过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下几个存储过程以HolidayList表为基础

--创建节假日表
CREATE
 TABLE [dbo].[HolidayList] (
    
[HolidayId] [int] IDENTITY (11NOT NULL ,
    
[HolidayDate] [smalldatetime] NULL ,
    
[HolidayName] [varchar] (16) COLLATE Chinese_PRC_CI_AS NULL 
ON [PRIMARY]

--插入节假日数据
insert HolidayList
select '2009-10-1','国庆节' union 
select '2009-10-2','国庆节' union 
select '2009-10-3','国庆节' union 
select '2009-10-4','国庆节' union 
select '2009-10-5','国庆节' union 
select '2009-10-6','国庆节' union 
select '2009-10-7','国庆节' union 
select '2009-10-8','中秋节' 


判断当天是否有行情:

CREATE PROC dbo.IsQuoteDate
@bQuoteDate bit output
AS  
/*
description :    判断当天是否有行情
return :    @bQuoteDate(0:无行情/1:有行情)
author :    totem
create date :   2009-09-16
*/
declare @currDay varchar(10),@HolidayName varchar(16)
set @currDay = convert(varchar(10),getdate(),121)
set @bQuoteDate = 0

if datepart(w,getdate())<>7 and datepart(w,getdate())<>1 --不为周六和周日
begin
  
select @HolidayName = holidayName from holidaylist where holidaydate = @currDay
  
if @HolidayName is null
    
set @bQuoteDate = 1
  
else
    
set @bQuoteDate = 0
end


调用方式:

declare @bQuote bit
exec IsQuoteDate @bQuote output
print ' result = ' + convert(char(1),@bQuote)


获取当前日期的上一有行情的日期:

CREATE PROC GetLastQuoteDate
@CurrDay smalldatetime ,
@LastQuoteDate smalldatetime output
AS
/*
description :    获取指定日期的上一有行情的日期
author :    totem
create date :   2009-09-16
*/
declare @HolidayName varchar(16)
declare @bSuccessed bit

set @LastQuoteDate = dateadd(d,-1,@CurrDay)
set @bSuccessed = 0

while @bSuccessed = 0
begin
    
if datepart(w,@LastQuoteDate)=7 or datepart(w,@LastQuoteDate)=1
    
begin
        
set @LastQuoteDate = dateadd(d,-1,@LastQuoteDate)
        
continue
    
end
    
set @HolidayName = null
    
select @HolidayName = holidayName from holidaylist where holidaydate = convert(varchar(10),@LastQuoteDate,121)
    
if @HolidayName is null 
        
break
    
else
        
set @LastQuoteDate = dateadd(d,-1,@LastQuoteDate)
end


调用方式:

declare @currDay smalldatetime
declare @QuoteDate smalldatetime
set @currDay = getdate()
exec GetLastQuoteDate @currDay@QuoteDate output
print 'result = '+ convert(varchar(10),@QuoteDate,121)


 

转载于:https://www.cnblogs.com/ztotem/archive/2009/09/16/1568139.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值