国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

9、SQL Server 操作數據

2024-08-31 00:54:05
字體:
來源:轉載
供稿:網友
9、SQL Server 操作數據

插入數據

使用Insert Into 插入

if(exists(select * from sys.databases where name = 'webDB'))    drop database webDBgo--創建數據庫create database webDB on PRimary(    name = 'webDB',    filename='d:/webDB.mdf',    size = 5mb,    maxsize=unlimited,    filegrowth=10%)log on(    name = 'webDB_log',    filename = 'd:/webDB.ldf',    size=3mb,    maxsize=50mb,    filegrowth=2mb)use webDBgo--創建表create table student(    id int identity(1,1) primary key,    name varchar(20) not null,    sex char(2) not null,    age int)--使用Insert Into 插入一行數據insert into student (name,sex,age) values ('白大偉','男',26)--id為標識列 自動增長 無需為其添加值--插入某個列insert into student (name,sex) values ('魏力夫','妖')--如果插入全部列,可以不用寫列名insert into student values ('李東','男',37)

插入標識列Set Identity_Insert

--如果需要在標識列中插入一個值,可以使用Set Identity_Insert語句,但注意 主鍵不可重復!set Identity_Insert student on --設置允許插入標識列insert into student (id,name,sex,age) values (4,'梁不賤','男',24)set Identity_Insert student off --插入后 關閉

使用InsertInto插入多行數據

--使用Insert Into 插入多行數據,使用逗號分割insert into student (name,sex,age) values ('張三','男',18),('李四','女',19),('王五','女',20)

使用Select語句插入

--使用Select 語句可以將現有表中的多行數據插入到目標表中insert into student (name,sex,age) select Sname,Ssex,Sage from oldTable

使用SelectInto插入

--使用Select Into 插入數據--該方法其實是創建了一張新表,然后由select語句將所有行添加到新創建的表中。select id,name,sex,ageinto newTablefrom student--這種方式創建的表,沒有主鍵,索引,以及約束,并且表列的長度也會發生變化。不適合創建永久表

更新數據

使用Update語句更新數據

--需要注意的是,如果不限制范圍,則整表都會更新update student set name='王大錘' where id = 3

引用多表更新數據

--有時會需要引用另外的表,用來限制更新記錄create table [user](    userID int identity(1,1) primary key,    userName varchar(30) not null,    passWord varchar(30) not null,    RoleId int not null)create table [role](    roleId int identity(1,1) primary key,    roleName varchar(30) not null,    roleContent varchar(50) not null)insert into [user] values ('admin','admin',1),('editor','editor',2),('system','system',3)insert into [role] values ('管理員','網站管理員'),('編輯','網站編輯'),('系統','系統管理員')update [role] set roleContent = '只有user表中的用戶名類似adm才會修改'from Role rinner join [user] uon r.roleId = u.RoleIdwhere u.userName like 'adm%' --如果沒有這個限制條件 則整表都更新select * from [role]

刪除數據

使用Delete刪除

--如果不限制條件則整表都會被刪除。delete from student where id = 1

引用多表刪除數據

--先刪除user表中的admindelete from [user] where userName = 'Admin'delete from [role]from [role] rleft outer join [user] uon r.roleId = u.userIDwhere u.RoleId is null --刪除權限表中未被user表中引用的數據,管理員被刪除select * from [role]

使用truncate刪除所有行

truncate table oldTable--truncate 會刪除所有行,無法指定范圍.

合并數據

Merge語句是一個多種功能的混合語句,在一個查詢中可以完成Insert、Update、Delete等功能。

根據與源表聯接的結果,對目標表執行插入、更新或刪除操作。源表中包含即將被添加(或更新)到目標表中的數據行,而目標表接受插入(或更新)操作,可以對兩個表進行同步操作。

SQLServer 2008之前的版本中是沒有的,所以以前都是先刪掉再添加,或寫一些分支條件判斷存在否再insert或update。

--創建源表create table sourceTable(    id int,    content varchar(30))--創建目標表create table targetTable(    id int,    content varchar(30))--插入測試數據insert into sourceTable values (1,'S001'),(2,'S002'),(3,'S003'),(4,'S004'),(5,'S005')insert into targetTable values (1,'target001'),(2,'target002'),(6,'target006'),(7,'target007')select * from sourceTable--源表--1        S001--2        S002--3        S003--4        S004--5        S005select * from targetTable--目標表--1        target001--2        target002--6        target006--7        target007--編寫merge語句merge into targetTable t    --目標表using sourceTable s            --源表on t.id = s.id                --類似join 完成兩表之間的匹配when matched                --如果兩表中有值被匹配,更新then update set t.content = s.content when not matched            --如果沒有匹配結果,插入then insert values(s.id,s.content)when not matched by source    --目標表中存在但源表中不存在,刪除then delete;--再次查詢,則兩表同步

返回輸出的數據

OUTPUT子句可以把受影響的數據行返回給執行請求的任何接口,并且可以插入到一張表中。

OUTPUT子句可以引用inserted或deleted虛擬表,取決于需要修改前(deleted)或修改后(inserted)的數據。

輸出insert語句的執行結果

insert into studentoutput inserted.id,inserted.name,inserted.sex,inserted.age --inserted.* 所有select '哈哈','女',24--返回insert語句插入的記錄,對于查找服務器生成的值并返回給應用程序是很有用的。

輸出update語句的執行結果

update student set name='張振'output deleted.name as oldName,inserted.name as updateValuewhere id = 4--返回修改之前的名字 和修改之后的名字--可以用來追蹤對數據庫的刪除操作

將OUTPUT結果插入一張表中

--創建審計表create table db_Audit(    id int not null,    name varchar(50) not null,    sex  varchar(50) not null,    age int,    deleteDate datetime not null        constraint DF_deleteDate_TOday default(getdate()) --默認約束 當前時間)delete from studentoutput deleted.id,deleted.name,deleted.sex,deleted.ageinto db_Audit(id,name,sex,age)where id <10 -- 將id小于10的全部刪除并插入到審計表中select * from db_Audit

Merge通過output子句,可以將剛剛做過變動的數據進行輸出。

merge into targetTable t    --目標表using sourceTable s            --源表on t.id = s.id                --類似join 完成兩表之間的匹配when matched                --如果兩表中有值被匹配,更新then update set t.content = s.content when not matched            --如果沒有匹配結果,插入then insert values(s.id,s.content)when not matched by source    --目標表中存在但源表中不存在,刪除then deleteoutput $action as action,inserted.id as 插入的id,inserted.content as 插入的內容,deleted.id as 刪除的id,deleted.content as 刪除的內容;


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庐江县| 凤庆县| 万州区| 嘉义市| 辽宁省| 娄底市| 茶陵县| 丘北县| 新沂市| 和平区| 大新县| 喀什市| 确山县| 厦门市| 梅州市| 河南省| 宜都市| 南木林县| 吴堡县| 德州市| 西乡县| 迁安市| 体育| 呼伦贝尔市| 城步| 方城县| 山阳县| 辉县市| 德州市| 大连市| 乡宁县| 江源县| 和平区| 凤庆县| 眉山市| 靖宇县| 河间市| 合山市| 吉隆县| 长岭县| 和静县|