search
HomeDatabaseMysql Tutorial sql server常用知识点
sql server常用知识点Jun 07, 2016 pm 05:37 PM
serverCommonly usedKnowledge points

--删除表 use [20130823_Recource] go drop table my_table1,my_table2,My_table3 --创建表 use [20130823_Recource] go if(exists(select * from sys.objects where)) drop table Student1 go create table Student1 ( Id int primary key identity(1,2) no

--删除表

use [20130823_Recource]

go

drop table my_table1,my_table2,My_table3

--创建表

use [20130823_Recource]

go

if(exists(select * from sys.objects where))

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))

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

--函数

if(exists(select * from sys.objects where))

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))

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))

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

when 2500

when 4500

end as ran

 from Student1

 

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.Id5 -- 符合:、、

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--排序

 

 

触发器的两个重要的表

对表的操作

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) --测试

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python速查表大全,你掌握了哪些知识点?Python速查表大全,你掌握了哪些知识点?Apr 26, 2023 pm 10:49 PM

Python作为当下最大众化的编程语言,相信每天都会有大量的新手朋友进入学习大军的行列。但是无论一门语言是多么的容易学习,其基本概念、基础知识还是比较多的,对于小白来说,一时间要掌握这么多还是有些吃力。今天精选收集了众多Python相关的知识速查表,可以说是包罗万象,以后妈妈再也不用担心大家记不住任何知识点了!Python基础Pythonbasics该速查表包含了所有的Python基本知识,从变量数据类型到列表字符串,从环境安装到常用库的使用,可以说面面俱到。Beginner'sPytho

如何安装、卸载、重置Windows服务器备份如何安装、卸载、重置Windows服务器备份Mar 06, 2024 am 10:37 AM

WindowsServerBackup是WindowsServer操作系统自带的一个功能,旨在帮助用户保护重要数据和系统配置,并为中小型和企业级企业提供完整的备份和恢复解决方案。只有运行Server2022及更高版本的用户才能使用这一功能。在本文中,我们将介绍如何安装、卸载或重置WindowsServerBackup。如何重置Windows服务器备份如果您的服务器备份遇到问题,备份所需时间过长,或无法访问已存储的文件,那么您可以考虑重新设置WindowsServer备份设置。要重置Windows

Windows Server 2025预览版迎来更新,微软改善Insiders测试体验Windows Server 2025预览版迎来更新,微软改善Insiders测试体验Feb 19, 2024 pm 02:36 PM

在发布WindowsServer的build26040版本之际,微软公布了该产品的官方名称:WindowsServer2025。一同推出的,还有Windows11WindowsInsiderCanaryChannel版本的build26040。有些朋友可能还记得,多年前有人成功将WindowsNT从工作站模式转换为服务器模式,显示微软操作系统各版本之间的共性。尽管现在微软的服务器操作系统版本和Windows11之间有明显区别,但关注细节的人可能会好奇:为什么WindowsServer更新了品牌,

揭秘HTML缓存机制:不可或缺的知识要点揭秘HTML缓存机制:不可或缺的知识要点Jan 23, 2024 am 08:51 AM

HTML缓存机制大揭秘:必备的知识点,需要具体代码示例在Web开发中,性能一直是一个重要的考量因素。而HTML缓存机制是提升Web页面性能的关键之一。本文将揭秘HTML缓存机制的原理和实践技巧,并提供具体的代码示例。一、HTML缓存机制的原理Web页面访问过程中,浏览器通过HTTP协议请求服务器获取HTML页面。HTML缓存机制就是将HTML页面缓存在浏览器

Spring注解大揭秘:常用注解解析Spring注解大揭秘:常用注解解析Dec 30, 2023 am 11:28 AM

Spring是一个开源框架,提供了许多注解来简化和增强Java开发。本文将详细解释常用的Spring注解,并提供具体的代码示例。@Autowired:自动装配@Autowired注解可以用于自动装配Spring容器中的Bean。当我们在需要依赖的地方使用@Autowired注解时,Spring将会在容器中查找匹配的Bean并自动注入。示例代码如下:@Auto

学习canvas框架 详解常用的canvas框架学习canvas框架 详解常用的canvas框架Jan 17, 2024 am 11:03 AM

探索Canvas框架:了解常用的Canvas框架有哪些,需要具体代码示例引言:Canvas是HTML5中提供的一个绘图API,通过它我们可以实现丰富的图形和动画效果。为了提高绘图的效率和便捷性,许多开发者开发了不同的Canvas框架。本文将介绍一些常用的Canvas框架,并提供具体代码示例,以帮助读者更深入地了解这些框架的使用方法。一、EaselJS框架Ea

怎么修改Nginx版本名称伪装任意web server怎么修改Nginx版本名称伪装任意web serverMay 14, 2023 pm 09:19 PM

如何修改nginx默认的名称,可以稍微的伪装一下,也可以装x一般来说修改3个位置,一个是nginx.h、另一个是ngx_http_header_filter_module.c、还有一个ngx_http_special_response.c。提示:一般修改都是在nginx编译之前修改,修改完了之后需要重新编译代码如下:scr/core/nginx.conf#definenginx_version"1.4.7"#definenginx_ver"nginx/"n

微软发布 Windows Server vNext 预览版 25335微软发布 Windows Server vNext 预览版 25335Jan 10, 2024 am 08:49 AM

微软在面向桌面端发布Win11预览版更新的同时,今天还发布了WindowsServer长期服务通道(LTSC)预览版Build25335。微软和以往相同,并未公布完整的更新日志,甚至于没有提供相应的博客文章。微软调整了WindowsServer预览版更新日志,让其和Canary频道版本相同,如果没有引入新的内容,则不放官方博文。IT之家注:Server的品牌尚未更新,在预览版中仍为WindowsServer2022。此外,微软将这些版本称为WindowsServervNext,而不是已经上市的W

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.