首页 > 数据库 >Sql server-自定义函数

Sql server-自定义函数

时间:2023-08-19 13:55:30浏览次数:33  
标签:dbo 自定义 year server birthday Sql getrealage select getdate

定义:一种方法

1.创建函数,求该银行的金额总和--(没有参数,返回标量值)

go

create function getsumcardbalance()

return money

as

begin

declare @sumcardbalance money

set @sumcardbalance =(select sum(cardbalance) from bankcard)

return @sumcardbalance 

end

2.调用验证

select dbo.getsumcardbalance() 银行金额总和

3.删除函数

drop function getsumcardbalance

4.创建函数:传入账户编号(假设accid=1),返回账户真实姓名

go

create function getrealname(@accid int)

returns nvarchar(30)

as

begin

declare @realname nvarchar(30)

set @realname =(select accname from accountinfo where accid=@accid)

return @realname

end

go

调用函数

select dbo.getrealname(1)

删除函数

drop function getrealname

验证是否删除成功

select dbo.getrealname(1)

创建函数:传递开始时间和结束时间,返回交易记录(存钱取钱),交易记录中包含真实姓名,卡号,存钱金额,取钱金额,交易时间,用中文标识别名

go

create function getcardchange(@start datetime, @end datetime)

returns @result table 

{

真实姓名 nvarchar(30),

卡号 varchar(30),

存钱金额 money,

取钱金额 money,

交易时间 datetime

}

as 

begin

insert into @result

select accname,bankcard.cardno,moneyin,moneyout,cextime

from bankcard inner join accountinfo

on bankcard ,accid = accountinfo.accid

inner join cardexchange

on bankcard.cardno = cardexchange.cardno

where cextime between @start and @end

return

end

go

调用函数

select * from cardexchange

select * from dbo.getcardchange('2022-11-25','2022-12-25')

--总结: datetime数据类型,如果值输入日期date部分,而不输入time部分,则默认time=00:00:00,

如果需要明确时间范围,需要明确time值。

删除函数

drop function getcardchange

不创建函数:查询银行卡信息,将银行卡状态1.2.3.4分别转换为汉字“正常,挂失,冻结,注销”

select *,case cardstate

when 1 then '正常'

when 2 then '挂失'

when 3 then '冻结'

when 4 then '冻结'

end 银行卡状态

from bankcard

不创建函数,根据银行卡余额显示银行卡等级30万以下为“普通用户”,30万及以上为“vip用户”

select *,case 

 

when cardbalance > 300000 then 'VIP用户'

else '普通用户'

end 用户等级

from bankcard

创建函数:分别显示卡号、身份证,姓名,用户等级,银行卡状态,

select cardno 卡号,accident 身份证,accname 姓名,cardbalance 余额,dbo.getuserrank(cardbalance) 用户等级,dbo.getcardstate(cardstate) 银行卡状态 from bankcard inner join accountinfo 

on bankcard,accid = accountinfo.accid

编写求用户等级函数

go

create function getuserrank (@cardbalance money)

returns nvarchar(30)

as

begin

declare @userrank nvarchar(30)

set @userrank =(select cardbalance from bankcard)

return @userrank

end

go

编辑求银行卡状态函数

go

create function getcardstate(@cardstate int)

returns nvarchar(30)

as 

begin

deciare @state nvarchar(30)

if @cardstate = 1

set @state ='正常'

else if @cardstate = 2

set @state ='挂失'

else if @cardstate = 3

set @state ='冻结'

else if @cardstate = 4

set @state ='注销'

return @state

end

go

编写函数,根据出生日期求实岁年龄,例如

生日为2000-5-5,当前为2020-5-4,年龄为19岁

生日为2000-5-5,当前为2020-5-6,年龄为20岁

分析思路,定义出生日期:@dirthday,@getrealage:

--1.year(getdata())=year(@birthday),@getrealage =0

 

--2.year(getdate()) >year(@birthday) and month(getdate())>month(@birthday),@getrealage=year(getdate())-year(@birthday)

--3.year(getdate()) >year(@birthday) and month(getdate()) = month(@birthday) and day(getdate()) >= day(@birthday),@getrealage=year(getdate())-year(@birthday)

 

--4. year(getdate()) > year(@birthday) and month(getdate())=month(@birthday) and day(getdate()) < day(@birthday),@getrealage=year(getdate())-year(@birthday)-1

--5. year(getdate()) > year(@birthday) and month(getdate())<month(@birthday) ,@getrealage=year(getdate())-year(@birthday)-1

 

go

create function getrealage(@birthday datetime)

returns int 

as

begin

declard @age int 

if  year(getdata())=year(@birthday)

set @age =0

 

else if year(getdate()) >year(@birthday) and month(getdate())>month(@birthday)

set @age=year(getdate())-year(@birthday)

 

else if year(getdate()) >year(@birthday) and month(getdate()) = month(@birthday) and day(getdate()) >= day(@birthday)

set @age=year(getdate())-year(@birthday)

 

else

set  @age=year(getdate())-year(@birthday) -1

 

return @age

end

go

验证:当年出生的‘2022-1-23’

select dbo.getrealage('2022-1-23')

跨年的且当前月份大于出生月份的‘2020-10-23’

select dbo.getrealage('2022-10-23')

跨年的且当前月份等于出生月份的且当前日期=出生日期 “2020-11-27”

select dbo.getrealage('2022-11-27')

跨年的且当前月份等于出生月份的且当前日期》出生日期‘2020-11-21’

select dbo.getrealage('2020-11-23')

跨年的且当前月份等于出生月份的且当前日期<出生日期‘2020-11-28’

select dbo.getrealage('2020-11-28')

跨年的且当前月份小于出生月份‘2020-12-21’

select dbo.getrealage('2020-11-28')

 

标签:dbo,自定义,year,server,birthday,Sql,getrealage,select,getdate
From: https://www.cnblogs.com/KevinSteven/p/17642070.html

相关文章

  • Visual Studio 2022 没有MySQLDatabase数据源
    解决办法: ①下载安装MySQLODBC驱动②运行ODBC数据源管理器③添加MySQL数据源,填入相应信息,测试通过即可④打开VS 工具>>连接到数据库,选择MicrosoftODBCDataSource⑤下拉列表中选择刚才新建的ODBC数据源,确定。       由此,在VS的侧边栏就可以对MySQL......
  • 【LeetCode2199. 找到每篇文章的主题】字符串处理题,使用MySQL里的group_concat和LOCAT
    题目地址https://leetcode.cn/problems/finding-the-topic-of-each-post/description/代码witht1as(selectp.*,k.*fromPostspleftjoinKeywordskonLOCATE(LOWER(CONCAT('',word,'')),LOWER(CONCAT('',conte......
  • 关于日常开发中的一些小的SQL优化建议
    优化建议:索引优化:根据查询条件和连接条件创建索引,可以提高查询性能(有连表查询时,连表关联字段添加上索引)。子查询优化:将子查询改为连接查询,可以提高查询性能。具体可以考虑将es_info_relation_owner表的子查询改为连接查询。避免使用DISTINCT关键字:如果查询结果中有重复的记录,可......
  • rhel 6.5以编译方式安装mysql 5.5.18
    文档课题:rhel6.5以编译方式安装mysql5.5.18数据库:mysql5.5.18系统:rhel6.564位安装包:mysql-5.5.18.tar.gz1、卸载MariaDB--卸载系统自带的mysql和mariadb-lib.[root@MySQL5518-Master~]#rpm-qa|grepmysqlmysql-libs-5.1.71-1.el6.x86_64[root@MySQL5518-Master~......
  • [SQL Server---For XML PATH 的运用]
    SELECTA.CID,B.TrueNameinto#UserNameFROMRH_CommuUserRoleAWITH(NOLOCK)LeftjoinRH_UserBWITH(NOLOCK)onA.UserID=B.idANDA.UType=1WHEREB.UserState=1selectCID,TrueName=STUFF((select','+ltrim(TrueName)from#UserNamewhereCID=t.CIDfo......
  • SQLserver批量批量导出索引
    WITHindexInfoas(SELECTSCHEMA_NAME(t.schema_id)[schema_name],t.nameas[table_name],t1.nameas[index_name],t1.type,t1.type_desc,t1.is_unique,t1.is_primary_key,t1.is_unique_constraint,t1.has_filter,t1.filter_definition,STUFF((SELECT�......
  • 【LeetCode1225. 报告系统状态的连续日期】MySQL使用lag,lead得到连续段的:开始标志,结束
    目录题目地址题目描述代码题目地址https://leetcode.cn/problems/report-contiguous-dates/description/题目描述Asystemisrunningonetaskeveryday.Everytaskisindependentoftheprevioustasks.Thetaskscanfailorsucceed.Writeasolution toreportth......
  • SQL server表锁
    --查询出锁表进程selectrequest_session_idspid,OBJECT_NAME(resource_associated_entity_id)tableNamefromsys.dm_tran_lockswhereresource_type='OBJECT'--需要将锁表进程@spid换成上面查询出来的锁表进程号declare@spidintSet@spid=65--锁表进程declare@......
  • 开源数据库Mysql_DBA运维实战 (总结)
    开源数据库Mysql_DBA运维实战(总结)SQL语句都包含哪些类型DDLDCLDMLDQLYum安装MySQL的配置文件配置文件:/etc/my.cnf日志目录:/var/log/mysqld.log错误日志:/var/log/mysql/error.logMySQL的主从切换查看主从复制状态停止主数据库的写入操作记录当前二级制日志文件和位置更新从数据库......
  • 启动mysql时报错"/etc/init.d/mysqld: Permission denied"
    问题描述:启动mysql时报错"/etc/init.d/mysqld:Permissiondenied",如下所示:数据库:mysql5.5.18系统:rhel6.51、异常重现[root@MySQL5518-Master~]#servicemysqldstartenv:/etc/init.d/mysqld:Permissiondenied2、解决步骤[root@MySQL5518-Master~]#ll/etc/init.d......