惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
S
Schneier on Security
B
Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
T
Tenable Blog
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
D
DataBreaches.Net
S
Secure Thoughts
Security Latest
Security Latest
H
Heimdal Security Blog
The Hacker News
The Hacker News
O
OpenAI News
AWS News Blog
AWS News Blog
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
腾讯CDC
U
Unit 42
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hugging Face - Blog
Hugging Face - Blog
The Last Watchdog
The Last Watchdog
Recorded Future
Recorded Future
V2EX - 技术
V2EX - 技术
爱范儿
爱范儿
F
Full Disclosure

博客园 - allanlau

冒个泡 五危 快被网站的回复搞崩溃了! 紧急求助啊:当前平台上不支持“ResourcePool”如何解决? 你能有效制怒吗? 上善若水,厚德载物 “ASP 0201 错误 无效的默认脚本语言”的解决办法 - allanlau IIS里不能正常显示asp页面 转变 含义 鹰的重生 有种感觉叫回忆 windows的140个技巧! 求助啊,打开资源管理器的快捷键没用了 又一年了 有感而发,10年后我会在做什么 干掉ctfmon.exe 怎样才能娶到比尔-盖茨的女儿 dudu,想把原来的随笔分类好像很麻烦啊
SQL Server 中奇怪的 Return
allanlau · 2006-07-21 · via 博客园 - allanlau

    今天帮同事查找一个函数中的bug。给我的源代码如下:

if exists (select * from dbo.sysobjects where id = object_id(N'[tb_Holiday]'and OBJECTPROPERTY(id, N'IsUserTable'= 1)
drop table [tb_Holiday]
GO
--定义节假日表
CREATE TABLE tb_Holiday(
HDate 
smalldatetime primary key clustered--节假日期
Name nvarchar(50not null)             --假日名称
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_WorkDay]'and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_WorkDay]
GO
--计算两个日期之间的工作天数
CREATE FUNCTION f_WorkDay(
@dt_begin datetime,  --计算的开始日期
@dt_end  datetime   --计算的结束日期
)RETURNS int
AS
BEGIN
 
IF @dt_begin>@dt_end
  
RETURN(DATEDIFF(Day,@dt_begin,@dt_end)
   
+1-(
    
SELECT COUNT(*FROM tb_Holiday
    
WHERE HDate BETWEEN @dt_begin AND @dt_end))
 
RETURN(-(DATEDIFF(Day,@dt_end,@dt_begin)
  
+1-(
   
SELECT COUNT(*FROM tb_Holiday
   
WHERE HDate BETWEEN @dt_end AND @dt_begin)))
END
GO

    执行如下查询:
    select dbo.f_workday('2006-01-01','2006-02-01')
    select dbo.f_workday('2006-02-01','2006-01-01')
    和理想结果相差悬殊啊。
    虽然上本身就有逻辑的错误,但主要的问题不在这里。当时就怀疑实际语句的执行情况是否正确,测试后发现一个奇怪的问题。就是不管在函数还是存储过程中,return 后面加上确切的数字和简单的算术计算都没问题,都可以使函数或存储过程立刻返回。但是返回值中如果包含查询语句,如:return (select count(*) from TableName),那么函数或存储过程将继续执行,如果后面没有另外的return,外面得到的返回值倒是还能记得之前那个返回值(就是return (select count(*) from TableName)这句的返回值)。百思不得其解,网上随便查了查,没找到原因。难道return 后面的表达式不能是一个子查询?看看有哪位高手知晓其根本原因。