--删除表

use [20130823_Recource]

go

drop table my_table1,my_table2,My_table3

--创建表

use [20130823_Recource]

go

if(exists(select * from sys.objects where name='Student1'))

drop table Student1

go

create table Student1

(

Id int primary key identity(1,2) not null,

Name nvarchar(30) not null,

Age int not null,

MyMoney decimal ,

CreateDateTime datetime default getdate()

)

--插入数据

insert into Student values('zhangsan',34,2300,GETDATE())

insert into Student

select 'zhangsi',23,4300 ,GETDATE()union

select 'zhangwu',33,5400,GETDATE() union

select 'zhanghong',12,2300,GETDATE()

--修改数据

update Student set MyMoney=10000 where Age=12

--删除数据

delete Student  where Age=12

truncate table student

--存储过程

if(exists(select * from sys.objects where name='proc_Name'))

drop proc proc_Name

go

create proc proc_Name(@number int,@number1 int output)

as

begin

select @number1=su.MyMoney from Student as su where su.Id=@number

end

--执行存储过程

declare @num int

exec proc_Name 3,@num output

print @num

--统计存储过程写法

create procedure proc_name
@CountDate datetime=null --统计时间
as
begin
  if(@CountDate is null)  --统计时间为空则赋值
  begin
    set @CountDate=dateadd(day,-1,getdate());
     end
  if exists(select top 1* from 表 where datediff(day,Countdate,@Countdate)=0) --如果统计过了则退出
  return;
end

--函数

if(exists(select * from sys.objects where name='function_Name'))

drop function function_Name

go

create function function_Name(@number int)

returns int

as

begin

declare @number1 int

select @number1=su.MyMoney from Student as su where su.Id=@number

return @number1

end

--执行函数

select dbo.function_Name(3)

--视图

if(exists(select * from sys.objects where name='view_Name'))

drop view view_Name

go

create view view_Name

as

select * from Student where ID=3

--执行函数

select * from view_Name

--游标

declare cursor_name cursor scroll for

select su.Name from student as su

open cursor_name

declare @Name nvarchar(20)

fetch last from cursor_name into @Name

print @Name

fetch absolute 3 from cursor_name into @Name

print @Name

fetch relative 1 from cursor_name into @Name

print @Name

fetch prior from cursor_name into @Name

print @Name

fetch first from cursor_name into @Name

while(@@FETCH_STATUS=0)

begin

print @Name

fetch next from cursor_name into @Name

end

close cursor_name

deallocate cursor_name

--事务

begin tran tran_Name

declare @error int

set @error=0

begin try

update Student set MyMoney=MyMoney+1000 where ID=1

set @error=@error+@@ERROR;

update Student set MyMoney =MyMoney -1000 where ID=2

set @error=@error +@@ERROR;

end try

begin catch

print '错误号:'+error_number()+'错误信息:'+error_message()

set @error=@error+1;

end catch

if(@error>=1)

begin

rollback tran

print '失败'

end

else

begin

commit tran

print '成功'

end

--触发器

if(exists(select * from sys.objects where name='trigger_Name'))

drop trigger trigger_Name

go

create trigger trigger_Name

on student

for delete

as

insert into Student values('zhangsss',11,3400,GETDATE())

--执行触发器

delete Student where ID=1

--排名

select *,ROW_NUMBER() over(partition by name order by id) as ran from Student

select *,RANK() over(order by id) as ran from Student

select *,DENSE_RANK() over(order by id ) as ran from Student

select *,NTILE(2) over(order by id) as ran from Student

--开窗函数

Count(*)

--集合

select * from Student

union--合并

select * from Student1

select * from Student

intersect--交集

select * from Student1

select * from Student

except--除去

select * from Student1

--连接

select su.name,su1.Name from Student as su

inner join Student1 as su1

on su.id=su1.Id

select su.name,su1.Name from Student as su

left join Student1 as su1

on su.id=su1.Id

select su.name,su1.Name from Student as su

right join Student1 as su1

on su.id=su1.Id

--case

select *,case

when MyMoney<=2500 then '穷人'

when 2500<MyMoney and MyMoney<=4500 then '资产'

when 4500<MyMoney then '富人'

end as ran

from Student1

--while循环

while(@number>0)

begin

end

--常用函数

select distinct top 2 * from Student --top,distinct

select isnull(null,2) --判断是否为null

select getdate() --获得日期

select datename(DAY,GETDATE())--获得日期的某一字段

select dateadd(MONTH,1,GETDATE()) --当前日期加

select COUNT(*),AVG(su.MyMoney),SUM(su.MyMoney),MIN(su.MyMoney),MAX(su.MyMoney) from Student as su --系统函数

select * from Student su where su.Id<>5 -- 符合:<、>、<>、<=、=>

select * from Student su where su.Name like'%wu'--模糊查询:%、_、[]、^

select * from Student su where su.Id between 2 and 6 -- between and

select * from Student su where su.Id in(3,4,5)--in()

select Age from Student su group by su.Age having Age>22 --筛选分组

select * from Student su order by su.Id desc--排序

select isnull(a.id,0)--为空就为0

触发器的两个重要的表

对表的操作

Inserted逻辑表

Deleted逻辑表

增加记录(insert)

存放增加的记录

删除记录(delete)

存放被删除的记录

修改记录(update)

存放更新后的记录

存放更新前的记录

触发器回滚

if(exists(select * from sys.objects where name = 'tr_Valid'))

drop trigger tr_Valid

go

create trigger tr_Valid

on mymsg

for insert

as

declare @age int;

select @age=age from inserted

if(@age>50)

begin

insert into mymsg select Name,Age from inserted

end

else

begin

print 'age数值不正确'

rollback tran;--数据不正确,就执行回滚业务

end

insert into mymsg values('zl68',51) --测试

读取相关文章数据

Select top 10 * from stu order by newid()--获得随机10条

Select top 1 * from stu where id>@0 order by id asc--下一条//@0为传入参数

Select top 1 * from stu where id<@0 order by id desc--上一条//@0为传入参数

--日期处理

DATEADD(s,CreatedAt,'1970-1-1 08:00:00') --秒转换成日期

convert(smalldatetime,convert(varchar(10),getdate(),120))--字符串转换为日期

dateadd(mm,datediff(mm,0,'2015-02-11'),0)--月第一天

dateadd(ms,-3,dateadd(mm,datediff(m,0,'2015-02-11')+1,0))--月最后一天

dateadd(mm,datediff(mm,0,'2015-02-11')+1,0)--下个月第一天

dateadd(mm,datediff(mm,0,'2015-02-11')+2,-1)--下个月最后一天

dateadd(yy,datediff(yy,0,getdate()),0)--一年中第一天

dateadd(day,7,dateadd(day,2-(case when datepart(weekday,'2015-02-11')=1 then 8 else datepart(weekday,'2015-02-11') end),'2015-02-11'))--周一

dateadd(day,7,dateadd(day,8-(case when datepart(weekday,'2015-02-11')=1 then 8 else datepart(weekday,'2015-02-11') end),'2015-02-11'))--周日

字符串截取

case when Len(UserDesc)>3 then substring(UserDesc,0,3)+'...'

when Len(UserDesc)<=3 then UserDesc

end as UserDesc

sql server常用知识点的更多相关文章

  1. 数据库(SQL SERVER)常用知识点

    1,连接数据库字符串 Data Source=192.168.1.249;Initial Catalog=bbx_uf_jiekou;User ID=sa;Password=123 Data Sour ...

  2. sql server 常用的系统存储过程

      系统存储过程 说明 sp_databases 列出服务上的所有数据库 sp_helpdb 报告有关指定数据库或所有数据库的信息 sp_renamedb 更改数据库的名称 sp_tables 返回当 ...

  3. SQL SERVER常用语法记录

    用于记录SQL SERVER常用语法,以及内置函数. 以下语句包含: WITH 临时表语法 ROW_NUMBER()内置函数,我一般主要是用来分页.针对于查出来的所有数据做一个数字排序 分页的BETW ...

  4. sql server 常用小知识点

    1. sql server的语法:中文要加 N select * from eVA_EMPBoard where name = N'施纪平' 而oracle的不用 2.

  5. SQL Server 常用内置函数(built-in)持续整理

    本文用于收集在运维中经常使用的系统内置函数,持续整理中 一,常用Metadata函数 1,查看数据库的ID和Name db_id(‘DB Name’),db_name('DB ID') 2,查看对象的 ...

  6. sql server 常用的扩展存储过程

    sql server 里面提供了丰富的系统存储过程来辅助我们管理数据库以及开发.今天分享介绍一些常用的数据库扩展存储过程 xp_cmdshell 这个大家都比较熟悉了,使用xp_cmdshell 可以 ...

  7. SQL server 常用语句

    SQL Server中常用的SQL语句   1.概述 2.查询概述 3.单表查询 4.连接查询 5.带有exists的相关子查询 6.SQL的集合操作 7.插入操作 8.删除操作 9.修改操作 10. ...

  8. sql server 常用的函数小汇

    摘录些许sqlserver 常用到的一些函数,便于日常学习使用 一.字符转换函数1.ASCII()返回字符表达式最左端字符的ASCII 码值.在ASCII()函数中,纯数字的字符串可不用‘’括起来,但 ...

  9. sql Server 常用存储过程的优化

    优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会返回相应的影响行数.使用SET NOCOUNT ON ...

随机推荐

  1. Android开源图表库介绍

    XCL-Charts XCL-Charts V1.8     Android开源图表库(XCL-Charts is a free charting library for Android platfo ...

  2. 看过的bootstrap书籍(附下载地址)

    http://yun.baidu.com/share/link?shareid=3820784617&uk=1008683945 以下书籍下载地址. <BootStrap入门教程> ...

  3. asp.net mvc 4 编译错误 CS0012

    CS0012: The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is no ...

  4. CMake编译linux C++

    CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的 ...

  5. Objective C静态代码扫描和代码质量管理 OClint + SonarQube

    OClint是针对C, C++及Objective C代码的静态扫描分析工具,而SonarQube是一个开源的代码质量管理平台.本文将实现将OClint的扫描结果导入到SonarQube中,已实现对O ...

  6. MyEclipse 8.6反编译插件安装

    一.下载插件文件:jad.exe.jadeclipse    http://www.varaneckas.com/sites/default/files/jad/jad158g.win.zip    ...

  7. C# 浅拷贝与深拷贝

    浅拷贝:给对象拷贝一份新的对象引用地址:(只是给一个对象多起了个名字,所以,当改变拷贝的某个属性的时候,原对象的对应属性亦会改变).浅拷贝的定义—— 只对值类型(或string)类型分配新的内存地址: ...

  8. SQLServer获取随机数据

    1.比较常见和好用的一种 SELECT TOP 10 *, NEWID() AS randomFROM tableORDER BY random --newid函数会随机生成一个guid,很长的一个字 ...

  9. shell切换用户执行后面语句 su与su -的区别

    关于su和su -的区别,切换用户是可以使用su tom或者su - tom来实现,但是两者有区别,su只是切换身份,但shell环境仍然是原用户的shell,su -是连用户的shell环境一起切换 ...

  10. php网站的定时事件

    提示信息: 1.crontab    (脚本定时.) 2. php    XXX.php(注:xxx.php页面是具体要做的事情.)