sql server常用知识点
--删除表 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) --测试
,
熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python作為當下最大眾化的程式語言,相信每天都會有大量的新手朋友進入學習大軍的行列。但無論語言是多麼的容易學習,其基本概念、基礎知識還是比較多的,對於小白來說,一時間要掌握這麼多還是有些吃力。今天精選收集了眾多Python相關的知識速查表,可以說是包羅萬象,以後媽媽再也不用擔心大家記不住任何知識點了! Python基礎Pythonbasics這個速查表包含了所有的Python基本知識,從變數資料型別到列表字串,從環境安裝到常用函式庫的使用,可以說面面俱到。 Beginner'sPytho

WindowsServerBackup是WindowsServer作業系統自帶的功能,旨在協助使用者保護重要資料和系統配置,並為中小型和企業級企業提供完整的備份和復原解決方案。只有執行Server2022及更高版本的使用者才能使用此功能。在本文中,我們將介紹如何安裝、解除安裝或重設WindowsServerBackup。如何重置Windows伺服器備份如果您的伺服器備份遇到問題,備份所需時間過長,或無法存取已儲存的文件,那麼您可以考慮重新設定WindowsServer備份設定。要重設Windows

探索Canvas框架:了解常用的Canvas框架有哪些,需要具體程式碼範例引言:Canvas是HTML5中提供的一個繪圖API,透過它我們可以實現豐富的圖形和動畫效果。為了提高繪圖的效率和便利性,許多開發者開發了不同的Canvas框架。本文將介紹一些常用的Canvas框架,並提供具體程式碼範例,以幫助讀者更深入地了解這些框架的使用方法。一、EaselJS框架Ea

HTML快取機制大揭密:必備的知識點,需要具體程式碼範例在Web開發中,效能一直是重要的考量。而HTML快取機制是提升Web頁面效能的關鍵之一。本文將揭秘HTML快取機制的原理與實務技巧,並提供具體的程式碼範例。一、HTML快取機制的原理Web頁面存取過程中,瀏覽器透過HTTP協定請求伺服器取得HTML頁面。 HTML快取機制就是將HTML頁面快取在瀏覽器

Spring是一個開源框架,提供了許多註解來簡化和增強Java開發。本文將詳細解釋常用的Spring註解,並提供具體的程式碼範例。 @Autowired:自動組裝@Autowired註解可以用於自動組裝Spring容器中的Bean。當我們在需要依賴的地方使用@Autowired註解時,Spring將會在容器中尋找匹配的Bean並自動注入。範例程式碼如下:@Auto

如何修改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

在發布WindowsServer的build26040版本之際,微軟公佈了該產品的官方名稱:WindowsServer2025。一同推出的,還有Windows11WindowsInsiderCanaryChannel版本的build26040。有些朋友可能還記得,多年前有人成功將WindowsNT從工作站模式轉換為伺服器模式,顯示微軟作業系統各版本之間的共通性。儘管現在微軟的伺服器作業系統版本和Windows11之間有明顯區別,但關注細節的人可能會好奇:為什麼WindowsServer更新了品牌,

微軟在針對桌面端發布Win11預覽版更新的同時,今天也發布了WindowsServer長期服務頻道(LTSC)預覽版Build25335。微軟和以往相同,並未公佈完整的更新日誌,甚至於沒有提供相應的部落格文章。微軟調整了WindowsServer預覽版更新日誌,讓其和Canary頻道版本相同,如果沒有引進新的內容,則不放官方部落格文章。 IT之家註:Server的品牌尚未更新,預覽版仍為WindowsServer2022。此外,微軟將這些版本稱為WindowsServervNext,而不是已經上市的W
