一个存储过程调用另一个存储过程 (2009-12-29 13:57:05)

标签: 

it

 
 

--在开发阶段,有时不想在c#中处理一些表中的字段内容,直接在存储过程中处理,
--以下是我的在开发时,在存储过程中处理日期时,通过一个存储过程中调用另一个存储过程的实现方法,
--第一次运用这种,刚开始不会的,在网上查了一下,感觉有必要写下来,以便下会遇到时方便查阅!刚学存储过程不长,
--有些代码处理,对高手来说,可能很差劲,还望高手指正,本人将加一修改!谢谢


--外部传参数如 2007年6月25日,处理完后为25-Jun-07
CREATE procedure pro_test
@date_1 varchar(255)
as
--保存年
declare @year varchar(4)
--保存月
declare @month varchar(2)
--保存日
declare @day varchar(2)
--根据@date_1调用另一个存储过程,返回与@month相对应的英文
declare @result varchar(20)
--保存最终处理结果25-Jun-07
declare @ymd varchar(20)
begin
set @year = substring(@date_1,1,4);--2007
set @month = substring(@date_1,charindex('年',@date_1)+1,charindex('月',@date_1)-charindex('年',@date_1));--月
--清空月前面的'0'
if left(@month,1) = "0"
    set @month =substring(@month,2,1);
--调用另一个存储过程,请注意output
exec pro_test2 @month, @result output;
set @day = substring(@date_1,charindex('月',@date_1)+1,len(@date_1)-charindex('月',@date_1));
--清空日前面的'0'
if left(@day,1) ="0"
    set @day = substring(@day,2,1);
set @ymd = @day+'-' +@result+'-' +@year;
select @ymd;
select @year as y;
select @month as m;
select @day as d;
select @result
select @day+'-' +@result+'-' +@year
end
GO

--另一个存储过程,用来处理月
CREATE procedure pro_test2
@month varchar(255),
@result varchar(255) output
as
set @result =
case
    when @month = '1' then 'Jan'
    when @month = '2' then 'Feb'
    when @month = '3' then 'Mar'
    when @month = '4' then 'Apr'
    when @month = '5' then  'May'
    when @month = '6' then'Jun'
    when @month = '7' then 'Jul'
    when @month = '8' then 'Aug'
    when @month = '9' then 'Sep'
    when @month = '10' then  'Oct'
    when @month = '11' then  'Nov'
    else  'Dec'
end
select @result ;
GO
--追加一点:case的用法,case的用法,如c#中switch的用法相类试,但case执行完后要返回内容,而switch中,case的直接在case中代码段中,处理逻辑!

0

0

 
阅读(250)┊ 收藏(0) 转载(0) ┊ 喜欢 打印举报
已投稿到:
 
http://blog.sina.com.cn/s/blog_62386e9d0100h19p.html
原文地址:https://www.cnblogs.com/shuibi/p/6627930.html