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

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - 孙英雄

C# 窗体抖动 “嵌入式资源”的调用 - 孙英雄 - 博客园 SQL2000与Oracle的分布式操作 只能输入数字的TextBox---补充 只能输入数字的TextBox DevExpress7.3.4部分控件继承后无法编辑解决方案 Mssql安全设置 .NET中如何生成静态页 - 孙英雄 - 博客园 ASP.NET跨页面传值技巧总结 预报:卡巴斯基v6.0个人版KEY获取器---修正版 - 孙英雄 - 博客园 动态读取App.Config WEB开发调试利器:Firebug 关注SharpDevelop的一些问题 又一款Auto病毒专杀工具,可解决双击无法打开盘符的问题 Office2007体验及下载 简单讲述基于SQL SERVER 分页的存储过程 Access中的模糊查询 - 孙英雄 - 博客园 DataGrid控件排序方法 Asp.net中的常用小技巧
SQL函数
孙英雄 · 2007-03-08 · via 博客园 - 孙英雄

--聚合函数
use pubs
go
select avg(distinct price)  --算平均数
from titles
where type='business'
go 
use pubs
go
select max(ytd_sales)  --最大数
from titles
go 

use pubs
go
select min(ytd_sales) --最小数
from titles
go 

use pubs
go
select type,sum(price),sum(advance)  --求和
from titles
group by type
order by type
go 

use pubs
go 
select count(distinct city)  --求个数
from authors
go 

use pubs
go
select stdev(royalty) --返回给定表达式中所有值的统计标准偏差
from titles
go 

use pubs
go
select stdevp(royalty) --返回表达式中所有制的填充统计标准偏差
from titles
go 

use pubs
go
select var(royalty) --返回所有值的统计方差
from titles
go 

use pubs
go
select varp(royalty) --返回所有值的填充的统计方差
from titles
go 

--数学函数 

select sin(23.45),atan(1.234),rand(),PI(),sign(-2.34--其中rand是获得一个随机数
--
配置函数
SELECT @@VERSION --获取当前数据库版本
SELECT @@LANGUAGE --当前语言
--
时间函数
select getdate() as 'wawa_getdate' --当前时间
select getutcdate() as 'wawa_getutcdate' --获取utc时间
select day(getdate()) as 'wawa_day' --取出天
select month(getdate()) as 'wawa_month' --取出月
select year(getdate()) as 'wawa_year' --取出年
select dateadd(d,3,getdate()) as wawa_dateadd --加三天,注意'd'表示天,'m'表示月,'yy'表示年,下面一样
select datediff(d,'2004-07-01','2004-07-15'as wawa_datediff --计算两个时间的差
select datename(d,'2004-07-15'as wawa_datename --取出时间的某一部分
select datepart(d,getdate()) as wawa_datepart  --取出时间的某一部分,和上面的那个差不多
--
字符串函数
select ascii(123as '123',ascii('123'as '"123"',ascii('abc'as '"abc"' --转换成ascii码
select char(123),char(321),char(-123--根据ascii转换成字符
select lower('ABC'),lower('Abc'),upper('Abc'),upper('abc'--转换大小写
select str(123.45,6,1), str(123.45,2,2--把数值转换成字符串
select ltrim('    "左边没有空格"')  --去空格
select rtrim('"右边没有空格"     '--去空格
select ltrim(rtrim('   "左右都没有空格"    ')) --去空格
select left('sql server',3),right('sql server',6--取左或者取右 

use pubs
select au_lname,substring(au_fname,1,1--取子串
from authors
order by au_lname 

select charindex('123','abc123def',2--返回字符串中指定表达式的起始位置
select patindex('123','abc123def'),patindex('%123%','abc123def'--返回表达式中某模式第一次出现的起始位置
select quotename('abc','{'),quotename('abc'--返回由指定字符扩住的字符串
select reverse('abc'),reverse('上海'--颠倒字符串顺序
select replace('abcdefghicde','cde','xxxx'--返回呗替换了指定子串的字符串
select space(5),space(-2

--系统函数
select host_name() as 'host_name',host_id() as 'host_id',user_name() as 'user_name',user_id() as 'user_id',db_name() as 'db_name'
--变量的定义使用
--
声明局部变量
declare @mycounter int
declare @last_name varchar(30),@fname varchar(20),@state varchar(2--一下声明多个变量
--
给变量赋值
use northwind
go
declare @firstnamevariable varchar(20),
 
@regionvariable varchar(30)
set @firstnamevariable='anne' --可以用set,也可以用select给变量赋值,微软推荐用set,但select在选择一个值直接赋值时很有用
set @regionvariable ='wa' 

select lastname,firstname,title  --用声明并赋值过的变量构建一个Select语句并查询
from employees
where firstname= @firstnamevariable or region=@regionvariable
go
--全局变量
select @@version  --返回数据库版本
select @@error  --返回最后的一次脚本错误
select @@identity  --返回最后的一个自动增长列的id 

--while,break,continue的使用
--
首先计算所有数的平均价格,如果低于30的话进入循环让所有的price翻倍,
--
里面又有个if来判断如果最大的单价还大于50的话,退出循环,否则继续循环,知道最大单价大于50就break出循环,呵呵,
--
我分析的应该对吧.
use pubs
go
while (select avg(price) from titles) <$30 
begin
 
update titles
  
set price=price*2
  
select max(price) from titles
  
if(select max(price) from titles) >$50
  
break
  
else
  
continue
end
print 'too much for the marker to bear' 

--事务编程经典例子
--
begin transaction是开始事务,commit transaction是提交事务,rollback transaction是回滚事务
--
这个例子是先插入一条记录,如果出现错误的话就回滚事务,也就是取消,并直接return(返回),如果没错的话就commit 提交这个事务了哦
--
上面的那个return返回可以返回一个整数值,如果这个值是0的话就是执行的时候没出错,如果出错了就是一个负数,
--
这个return也可以用在存储过程中,可用用 exec @return_status= pro_name来获取这个值
use pubs
go
begin tran mytran
 
insert into stores(stor_id,stor_name)
  
values('333','my books')
 
go
 
insert into discounts(discounttype,stor_id,discount)
  
values('清仓甩卖','9999',50.00)
 
if @@error<>0
  
begin
   
rollback tran mytran
   
print '插入打折记录出错'
   
return
  
end
commit tran mytran 

--事务处理的保存点示例
--
做了事务保存点后可以rollback(回滚)到指定的保存点,不至于所有的操作都不能用
use pubs
go
select * from stores
begin transaction testsavetran
 
insert into stores(stor_id,stor_name)
  
values('1234','W.Z.D Book')
 
save transaction before_insert_data2
 
go
 
insert into stores(stor_id,stor_name)
  
values('5678','foreat Books')
 
go
rollback transaction before_insert_data2
select * from stores 

--存储存储过程
use pubs
if exists(select name from sysobjects where name= 'proc_calculate_taxes' and type='P')
 
drop procedure proc_calculate_taxes
go
create procedure proc_calculate_taxes (@p1 smallint=42,@p2 char(1),@p3 varchar(8)='char')
as
select *
from titles
--执行过程
EXECUTE PROC_CALCULATE_TAXES @P2='A'