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

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - David

TFS Server 不存在时,如何去除对应Server的Workspaces 更改已有程序集中的资源 记录:如何删除TFS Project SQL – TRUNCATE vs DELETE 又好久没写东西了! Powershell: Send mail with attachment 【Update】 - David SSIS: Execute SSIS Package in command prompt 【Update】 随感 Windows Workflow Foundation 了解 通过代码将Word 2007 template (dotx)文档转换Word 2007 (docx)文档(续) 通过代码将Word 2007 template (dotx)文档转换Word 2007 (docx)文档 创建Word2007文档而无需安装Word2007 Visual WebGUI - 如何处理MessageBox的DialogResult值 Visual WebGUI - 如何将Dialog的结果传回主窗体 Visual WebGUI Report - 导出到PDF Visual WebGUI 在 Vista IIS 7 中的配置 项目管理片语 - Schedule 在IIS 7设置ASP.NET 1.1(在Vista中使用VS2003) 给儿子的歌!
SQL – Character data (varchar)
David · 2009-10-09 · via 博客园 - David

刚刚才考虑是否天天写!不过,是该继续努力做下去了。不然,浑浑噩噩过日子不好了,上有老下有小的。再说,写写文章长长记性!提高提高自己!:)

好了,不废话了!刚才查查邮件,收到了sqlservercentral.com的Newsletter。这期里面有个问题:

declare @str varchar(max) 
select @str = replicate('#',10000) + replicate(cast('#' as varchar(max)),8000)+ '#' + '#' + '#' 
select len(@str)


The output of the above code in SQL Server 2005 is:

A. 16003

B. 18005

C. 8000

D. Error: Incorrect syntax near '.


可能不怎么用过varchar这个数据类型,更加是不清楚varchar(max)的情况。结果,我选的答案是C,不过 Sorry - you were wrong

我的理解是varchar在SQL Server 2005中的长度只有8000 bytes,最多也就8000 bytes了。所以就选了C,一个错误的答案。

原来,varchar在varchar(max)的时候,最大能容下2,147,483,648 bytes,这是在SQL Server 2005中的情况。而在Oracle 9i,varchar只有 4000 bytes。在MySql,有65,535 bytes。

因此,那答案会不会是B呢?不,B也是错误的。也不会是D,至少那些SQL Statement不会出错。

正确答案是A。

原因是:replicate这个函数虽然可以按照你的想法把指定的字符(串)复制一定的次数,但不是无限的,在没有指定字符(串)的数据类型为varchar(max)或nvarchar(max)的情况下,replicate只会得到一个8000 bytes的值,多出都会自动截断。

这样,第一个replicate('#',10000) 只能得到 8000 个 '#' ;而第二个有把 '#' 显式转换为varchar(max),但由于指定了 8000 ,因此也只有 8000 个 '#' 。

再加上后面 3 个 '#' ,最后的 len 函数的结果也就是 16003 了。

虽然这个问题的目的是replicate函数,但同时varchar也在里面,也有varchar(max)。