SQLServer 操作详解

SQLServer基础语法大全(基础篇) SQLServer基础语法大全,再难写的SQL也只是这些基础语句的复杂组合而已…… 阅读详情
AI摘要

本文详细介绍了数据库的相关知识,包括SQL语言的四大组成部分(DDL、DML、DCL和DTM),以及如何创建、修改和删除数据库及表。讨论了事务的开启、提交和结束,以及DDL和DCL操作对事务的影响。此外,还涵盖了数据的插入、删除和更新,查询语句的使用,以及表连接、视图、存储过程和触发器的概念。文章最后探讨了索引、游标和用户定义函数的应用,并强调了数据库设计的原则和范式遵循。


/*  缩写:
    DDL(Database Definition Language): 数据库定义语言
    DML(Database Manipulation Language): 数据库操作语言
    DCL(Database Control Language): 数据库控制语言
    DTM(Database Trasaction Management): 数据库事物管理

    知识概要:
            |---1.查询Select
            |
            |---2.数据库定义语言DDL: 对表,视图等的操作, 包括create,drop,alter,rename,truncate
            |
 数据库操作--|---3.数据库操作语言DML: 对记录进行的操作, 包括insert,delete,update
            |
            |---2.数据库控制语言DCL: 对访问权限等的操作, 包括grant,revoke
            |
            |---2.数据库事物管理DTM: 对事物的操作, 包括commit,rollback,savepoint

    事物的是由DML(insert,delete,update)开启的;
    而引起事物的提交或结束原因有:
    1.DTM操作: commit,rollback,savepoint
    2.系统崩溃宕机: 隐式rollback
    3.正常:    隐式commit
    4.DDL和DCL操作: DDL(create,drop,alter,rename,truncate)
                    DCL(grant,revoke)
    
    注意MS-Sql的特殊处:
    MS-Sql中的事物: 自动事物(commit)和手动事物(begin transaction). 
                    在Sql中DML(insert,delete,update)会引起自动事物commit, 而Oracle中不会
    MS-Sql的参数: 只能返回0或非0(如: 1,2)数字
    MS-Sql的存储过程: 一定会有返回值(return value), 默认返回0(成功). 在程序获取改值, 需要创建return参数对象(添加到参数列表), 并设定其方向.

    MSSqlServer的登录过程分两种:
    1. Windows集成验证: windows用户名和口令 -> SqlServer登录列表 -> 映射到用户列表    -> 登录成功
    2. SqlServer验证: Sql用户名和口令 -> 映射到用户列表 -> 登录成功
    两种登录方式的连接串:
    string connectionStr = "data source=.;database=Test;user id=sa;password=sa";
    string connectiongStr ="data source=.\sqlexpress;database=Test;integrated security=true";
    
    
    数据库设计原则:
    1. 数据库设计指导原则(关系数据库中的数据通过表来体现): 先确定表后确定业务字段.
    每个业务对象在数据库中对应一张表(若业务对象复杂则可对应多张表), 业务对象间每有一个关系也要对应一张表.
    注意: 业务字段需要跟项目结合, 例如: 学生的健康情况可以用一个字段(优、良等)表示, 但针对健康普查, 学生的健康情况需进一步划分为身高、体重、血压等
    如: 学校中的学生对象: 学院表(学院ID, 专业ID); 专业表: 专业表(专业ID, 专业名);学生表(学生ID, 学院ID,专业ID)

    2. 数据库设三大计原则:
    a. 完整性: 设计方案能够保存项目中的各种信息(要全)
    b. 低冗余: 通过主键保证记录的不重复、通过表间关系减少冗余字段
    c. 尽可能满足3范式(NormalForm): 1NF: 1个字段只能包含一个业务信息片(即项目中的业务字段表示的信息不可再分)
                                    2NF: 能找到1个或多个字段的组合, 用来唯一的确定表中的记录(即必须有主键).
                                    3NF: 主键唯一且直接确定表中的其他字段(即无传递依赖, 如: 教师id, 办公室id, 办公室电话关系中存在传递依赖)
    注意事项: 尽可能不用业务字段做主键, 通常的手段为自增列当主键, 并且末尾添加默认时间字段(getdate()).
              尽量采用SQL92代码, 保证可移植性. 如: 在Sql2000中, top函数只能跟常量(Sql2005可跟变量). 通用的解法为拼Query串, 用exec(query串)取结果

    备注:
    Sql中使用+拼接字符串, Oracle中使用||拼接字符串.
    C#数据类型:
        整数: sbyte,byte,short,ushort,int,uint,long,ulong
        实数: float,double,decimal
        字符: char,string
        布尔: boolean
        日期: datetime
        对象: object
        全局唯一标识: GUID
    Sql数据类型:
        整数: bit(0,1),tinyint(8),smallint(16),int(32),bigint(64)
        实数: float,real,numeric
        字符: char(8000),nchar(4000),varchar(8000),nvarchar(4000),ntext
        日期: smalldatetime(1900,1,1-2079,6,6),datetime(1753,1,1-9999,12,31)
        货比: money
              二进制: binary
        图像: image
        标识: uniqueidentity        
*/
--###################################################################################

--创建数据库:
if exists(select * from sysdatabases where [name]='TestStudent')
    drop database TestStudent
go
create database TestStudent
on
(
    name = 'TestStudent_DB',    --数据库逻辑名
    filename = 'D:\WorkCode\DB\TestStudent.mdf',
    size = 3,
    filegrowth = 10,
    maxsize = 100
)
log
on
(
    name = 'TestStudent_Log',
    filename = 'D:\WorkCode\DB\TestStudent.log',
    size = 3,
    filegrowth = 10,
    maxsize = 100
)
go

--###################################################################################

use TestStudent
go

--创建表, 约束类型: 主键、外键、唯一、check约束、非空约束
if exists( select * from sysobjects where [name] = 'T_Student' and [type] = 'U')
    drop table T_Student
go
create table T_Student
(
    Sno        int identity(100,1) primary key,    --可以使用scope_identity获得刚生成的id
    Sname    nvarchar(50) not null,
    Sgender    nchar(1),
    Sage        tinyint check(Sage >= 20 and Sage <= 30),
    home    nvarchar(100) default('北京'),
    idcard    nvarchar(18) unique
)
go
if exists( select * from sysobjects where [name] = 'T_Score' and [type] = 'U')
    drop table T_Score
go
create table T_Score
(
    id int primary key,
    Sno int identity(100,1) foreign key references T_Student(Sno),
    Score tinyint
)
go

--修改表结构
alter table T_Student
    add Education nchar(3)
go
alter table T_Student
    drop column Education
go

--修改表约束
alter table T_Student
    add constraint PK_Sno primary key(Sno),
        constraint CK_gender check(gender in('男','女')),
        constraint DF_home default('北京') for home,
        constraint UQ_idcard unique(idcard)
go
alter table T_Score
    add constraint FK_Sno foreign key references T_Student(Sno)
go
--创建联合主键
alter table T_SC with nocheck 
    add constraint [PK_T_SC] primary key nonclustered(
    studentId,
    courseId
)
go

--###################################################################################

--新增(插入)数据
insert into T_Student(Sname,Sgender,Sage) values('张三','男',23)
go
insert into T_Student(Sname,Sgender,Sage)
    select '李四','男',25 union
    select '王五','女',26 union
    select '赵六','男',28
go

--删除数据
truncate table T_Student    --只删除表的数据
delete from T_Student where sgender = '男'

--修改数据
update T_Student set sgender = '女' where sgender='男'

--###################################################################################

--查询数据
select * from T_Student where sgender = '女'
select top 3 * from T_Student    --选择前三项, top 3是用来筛选记录的, 所以得紧跟select, 不用考虑字段顺序
select top 40 percent * from T_Student    --选择前百分之几的记录
select sname as '姓名', '年龄' = sage from T_Student    --起别名的两种方式as和=
select * from T_Student where sage >= 20 and age <= 30
select * from T_Student where sage between 20 and 30
select * from T_Student where sage is null
select sname into T_Stu from T_StuInfo    --用T_StuInfo中的sname字段创建新表
--模糊查询呢: 通配符: %(0-任意字符), _(1个字符),[abc](选择含a或b或c),[^abc](选择不含a或b或c)
select * from T_Student where sname like '张%'
select * from T_Student where sname like '王_'
select * from T_Student where sname like '[张李]%'
select * from T_Student where sname like '[^张李]%'

--###################################################################################

--排序: order by子句的结果将额外生成一个新表(2字段: 原位置索引字段和排好序的字段)
select * from T_Student order by Sage desc    --默认是升序asc

--###################################################################################

--聚合函数: 若有聚合函数, 即使不写group by 子句, 也会默认将表分为一个大组
select sum([sid]) from T_StuScore
select count([sid]) from T_StuScore    --count(*)表示记录数, 而count(字段)忽略掉null值
select avg([sid]) from T_StuScore
select max([sid]) from T_StuScore
select min([sid]) from T_StuScore

select distinct([sid]) from T_StuScore

--###################################################################################

--分组函数, where用于对记录的筛选, having用于对组的筛选
select gender,Counter = count(*) from T_Stuinfo group by gender
select gender,Counter = count(*) from T_Stuinfo group by gender having count(*) >= 2

--###################################################################################

--表连接: 笛卡尔积(m*n条记录), 内连接, 外连接(左外连接、右外连接、全外连接)
--笛卡尔积
select sname,[sid],cid,score
from T_StuInfo s cross join T_StuScore c on s.[sid] = c.[sid]

--内连接: 先从m和n中选择, 然后再连接
select sname,[sid],cid,score 
from T_StuInfo s inner join T_StuScore c on s.[sid] = c.[sid]

--左外连接(左连接): 内连接 + 左表剩余记录(右表记录置null)
select sname,[sid],cid,score 
from T_StuInfo s left join T_StuScore c on s.[sid] = c.[sid]

--右外连接(右连接): 内连接 + 右表剩余记录(左表记录置null)
select sname,[sid],cid,score 
from T_StuInfo s right join T_StuScore c on s.[sid] = c.[sid]

--全外连接(全连接): 内连接 + 左表剩余记录(右表记录置null) + 右表剩余记录(左表记录置null)
select sname,[sid],cid,score 
from T_StuInfo s full outer join T_StuScore c on s.[sid] = c.[sid]

--###################################################################################

--函数: 字符串函数、日期函数、数学函数、系统函数
--字符串函数
print charindex('1','ab1cd')    --Sql中下表从1开始, 类似于C#中indexof、lastindexof, 返回包含'1'的下表位置
print len('abcd')    --C#中的length
print lower('ABCD')    --ToLower(), ToUpper()
print upper('abcd')    --ToUpper()
print ltrim(' abcd')    --LTrim()
print rtrim('abcd ')    --RTrim()
print rtrim(ltrim(' abcd '))    --Trim()
print right('abcd',2)    --从右截取, C#中的SubString(length-3,2)截取下表从0开始的2个
print left('abcd',2)    --SubString(0,2)
print replace('abcdef','cd','1234')    --Replace(), 用1234替换cd
update [card] set [passWord] = Replace(Replace([PassWord] ,'O','0'),'i','1') from T_UserInfo
print stuff('abcdef',2,3,'#')    --填充替换, 从第2个开始的3个字符替换成#
print cast('2010-11-08' as datetime)    --数据类型转换
print convert(datetime,'2010-11-08')    --数据类型转换
print str(67)    --数字变字符串
print newid()    --新uniqueidentifier, 它将会为记录临时添加一列, 且内容是随机的
print getdate()    --获得当前日期

--日期函数
print getdate()    --DateTime.Now
print dateadd(yyyy,10,'2010-1-2')    --增加10年
print dateadd(mm,2,getdate())    --增加月, 其他可以查sql联机丛书
print datediff(yyyy,'1985-12-13','2010-10-10')    --时间差距
print datediff(dd,'1985-12-13','2010-10-10')    --时间差距
print datename(mm,'2010-10-01') + '2' --返回月份+'2'
print datepart(mm,'2010-10-01') + '2'    --日期10+2,结果为12

--数学函数
print abs(-100)    --求绝对值, Abs()
print ceiling(10.05)    --取整数, 如果有小数则进1
print floor(10.05)    --取整数, 忽略小数
print round(10.567,2)    --四舍五入
print power(10,2)    --求幂运算
print sqrt(108)    --求平方根
print rand(10)    --只能选择0-1之间的数, Random.Ran(0,1)
print rand(10)*10    --随机0-10之间的数
print sign(30)    --只返回±1

--系统函数
print 'abcd' + convert(nvarchar(10),5)    --ToString()
print 'abcd' + cast(5 as nvarchar(10))    --同上
print datalength('1+1=2')    --返回表达式的字节数
print current_user    --返回当前登录的角色
print host_name()    --返回当前计算机名
print system_user    --返回当前用户id
print user_name()    --给定用户id返回其角色名
print isnull(filedname,0)    --替换null为0
raiserror('抛出异常',16,1)    --抛出异常, 1-15被系统占用, 对应C#中的throw
select * from sysobjects where objectproperty(id,N'IsUserTable') = 1    --判断是否用户表(y=1,n=0), N表示后边的串为unicode字符串.
select * from sysobjects where type= 'U'    --等价于上一行
select databasepropertyex('Northwind','IsBrokerEnabled')    --查询该库是否开启缓存技术中的通知机制, 1为开启, 0为关闭
alter database northwind set enable_broker    --开启数据库中, 缓存技术中的通知机制
--注意以下三个函数的用法    eg: 结果集(1,5,11,17,19,25)
select row_number() over(order by [sid]) from T_StuInfo        --1,2,3,4,5,6
select rank() over(order by [sid]) from T_StuInfo            --1,1,1,4,5,6
select dense_rank() over(order by [sid]) from T_StuInfo        --1,1,1,2,3,4
select ntile(2) over(order by [sid]) from T_StuInfo            --1,5  11,17  19,25

select row_number() over(order by [sid]) as sequence, sname, age, (case gender when '0' then '男' else '女' end) gender
from T_StuInfo s left join T_StuScore c on s.sid = c.sid
go

--###################################################################################

--范式: 1NF: 原子性, 2NF: 单主键, 3NF: 去除传递依赖
--E-R模型(表间关系): 1对1: 任何一个表添加外键; 1对多: 在多的一方添加外键; 多对多: 需要第三个表, 添加前两表的外键

--###################################################################################

--变量
--系统变量:
select * from T_StuInfo
print @@identity;    --获得结果集最大标识值
print @@error;    --有错误, 返回大于0的数; 无错误返回0
print @@rowcount;    --返回结果集的行数

--自定义变量
declare @age tinyint
declare @age2 tinyint
declare @name nvarchar(20)
declare @name2 nvarchar(20)

set @age = 15    --一次只能对一个量变赋值
select @name = '张三',@name2 = '李四'    --一次性给多个变量赋值
select @age2 = max(age) from T_StuInfo    --可以用在查询语句中

print @age
print @age2
print @name
print @name2

--###################################################################################

--条件表达式
declare @age int
set @age = 1

if (@age <20)
begin
    set @age = @age + 1
end
else
begin
    set @age = @age - 1
end

--循环
declare @index int
declare @sum int

set @index = 1
set @sum = 0

while (@index < 11)
begin
    set @sum = @sum + @index
    set @index = @index + 1
end
print @sum

--批处理Sql语句: 练习---打印三角形, 即成批处理语句+go即可, 只访问一次数据库
declare @row int
declare @col int
declare @n int    --总行数
declare @result nvarchar(2000)

set @row = 0
set @col = 0
set @n = 10        --可以修改n的值
set @result = ''

while (@row < @n)
begin
    set @col = 0    --复位
    set @result = ''
    while (@col < @n + @row)
    begin
        if (@col < @n - @row - 1)
        begin
            set @result = @result + ' '
        end
        else
        begin
            set @result = @result + '*'
        end
        set @col = @col + 1
    end
    print @result
    set @row = @row + 1
end
go

--case when
--搜索case when(用于一个范围)
select '评语' = case when SqlServer>= 90 then '优秀'
                    when SqlServer >= 80 and SqlServer < 90 then '良'
                    when SqlServer >= 60 and SqlServer < 80 then '及格'
                    else '不及格'
                end
from T_StuInfo

--简单case when(类似swtich, 用于一个定值)
declare @gender bit
set @gender = 'true'
print case @gender when 'true' then '男' else '女' end

--###################################################################################

--事物: 事物的特性ACID(一致性(Consistency)、原子性(Atomicity)、隔离性(Isolation)、持久性(Durability))
declare @errorcount int
set @errorcount = 0

begin transaction    --if控制事物的提交
begin
    update T_StuInfo set age = age + 1 where gender = '男'
    set  @errorcount = @@error    --@@error无错误返回0, 有错误返回非0的数
    update T_StuInfo set age = age - 1 where cardno = '女'
    set @errorcount = @errorcount + @@error
    if(@errorcount = 0)
    begin
        commit
    end
    else
    begin
        rollback
    end
end

begin transaction    --异常控制事物提交, raiserror('XXXXX',16,1)用于抛出xxxx的异常
begin
    begin try
        update T_StuInfo set age = age + 1 where gender = '男'
        update T_StuInfo set age = age - 1 where cardno = '女'
        commit
    end try
    begin catch
        raiserror('性别字段输入了不合适的字符',16,1)    --1-15级为系统使用
        rollback
    end catch
end

--###################################################################################

--索引: 聚集索引(Clustered Index)或称物理所引,非聚集索引(Nonclustered Index)或称逻辑索引,唯一索引(Unique Index),主键索引(PK Index)
--优缺点: 查询快, 但增删改慢.
--何时用: 数据量特别大的情况适合建索引; 经常查找的字段建索引(聚集索引, 此时要求取消主键索引)
--注意事项: 
--      使用索引时, 需要注意查询时的where子句: 若有索引, 先查索引, 之后再根据索引查找原表记录位置, 拼接结果; 若无索引, 则不查索引, 直接拼结果.
--      如此, 针对索引字段, 若从带where的查询结果中去掉前5项(不带where), 则会出现错误(真分页有类似情况).
--      解决方案: 在子查询中也添加排序字段的永真where条件, 如: where sortfield > -1
select top 20 sortfiled, filed1 from T_S where sortfiled not in (select top 5 sortfiled from T_S where sortfiled > -1)
            
create clustered index idx_age on T_StuInfo(age)    --创建聚集索引(每表仅一份), 将对记录排序, 而且索引将会和表保存在一起(采用二分查找)
create nonclustered index idx_age on T_StuInfo(age)    --创建非聚集索引(任意多个), 不排序但会创建独立表(含2列: 原表中的位置索引,已排序的字段) 

--###################################################################################

--视图: 将会创建一张虚拟表, 且对视图的insert、delete和update操作会修改源数据, 但工作中禁止通过视图修改源数据.
--    视图就是个Sql语句, 也就是Select结果的虚表, 视图相当于虚表的别名而已.
--    注意: 视图的别名的使用.
--优点: 代码易读; 经过预编译(存储过程也是预编译的), 效率高; 屏蔽了表结构, 比较安全性; 缺点: 增加管理开销
if exists(select * from sysobjects where [name] = 'V_SnoName' and [type] = 'V')
    drop view V_SnoName
go
create view V_SnoName
as
    select [sid],sname from T_StuInfo
go

select * from V_SnoName
select * from T_StuInfo
insert into V_SnoName(sname) values('候八')

--###################################################################################

--存储过程(Stored Procedure): sp_help查看SP以及sp参数的信息, sp_helptext查看SP内部代码
if exists(select * from sysobjects where [name] = 'P_Triangle' and [type] = 'P')
    drop procedure P_Triangle
go
create procedure P_Triangle(
    @n int
) with encryption    --加密, 不影响编译但将无法查看SP内部代码(sp_helptext)
as    --局部变量
    declare @row int
    declare @col int
    declare @result nvarchar(2000)
begin
    set @row = 0
    set @col = 0
    set @result = ''

    while (@row < @n)
    begin
        set @col = 0    --复位
        set @result = ''
        while (@col < @n + @row)
        begin
            if (@col < @n - @row - 1)
            begin
                set @result = @result + ' '
            end
            else
            begin
                set @result = @result + '*'
            end
            set @col = @col + 1
        end
        print @result
        set @row = @row + 1
    end
end
go
exec P_Triangle 10
sp_help P_Triangle    --查看SP及其参数的信息
sp_helptext    P_Triangle    --查看SP内部代码

declare @result int    --以下代码证明, SP默认返回值为0
set @result = -1    
exec @result = P_Triangle 15
print @result

--存储过程 + 事物 + 输出参数
if exists(select * from sysobjects where [name] = 'P_InsertRecord' and [type] = 'P')
    drop procedure P_InsertRecord
go
create procedure P_InsertRecord(
    @sname    nvarchar(20),
    @gender    nchar(1) = '男',    --等号后边是默认值
    @age    tinyint,
    @status    nchar(2),
    @birdate    datetime,
    @retrunsid    int    output    --用以保存该记录的主键
)
as    --局部变量
begin transaction
begin
    begin try
        insert into T_StuInfo(sname,gender,age,[status],birdate)
            values(@sname,@gender,@age,@status,@birdate)
        set @retrunsid = @@identity
        commit
        return 0
    end try
    begin catch
        raiserror('插入数据异常',16,1)
        rollback
        return 1
    end catch
end
go
declare @sid    int    --保存输出参数
declare @return int    --保存返回值

exec P_InsertRecord '测试2','男',35,'毕业','1977-06-07',@sid output
exec @return = P_InsertRecord '测试2','男',35,'毕业','1977-06-07',@sid output    --用@return接受SP返回值

print @sid
print @return

--###################################################################################

--触发器: 执行时将自动创建inserted或deleted临时表(update, 同时创建两表), 且均是只读的; 因为无调用痕迹, 系统调试时增加困难
if exists(select * from sysobjects where [name] = 'TR_DelStu' and [type] = 'TR')
    drop trigger TR_DelStu
go
create trigger TR_DelStu    --级联删除
on T_StuInfo
instead of delete    --(for,after,instead of), 注意for和after效果是一样的
as
    declare @currentid    int
begin transaction
begin
    begin try
        --set @currentid = (select [sid] from deleted)    --insert和update会用到临时表inserted
        select @currentid = [sid] from deleted
        delete from T_StuScore where [sid] = @currentid
        delete from T_StuInfo where [sid] = @currentid
        commit
    end try
    begin catch
        raiserror('删除失败操作异常',16,1)
        rollback
    end catch
end
delete from T_StuInfo where [sid] = 3

--###################################################################################

--用户定义函数(User Defined Function): 标量函数、内嵌表值函数、多语句表值函数
--标量函数(Scalar Functions)
if exists(select * from sysobjects where [name]='GetCountByGender' and [type] = 'FN')
    drop function GetCountByGender
go
create function GetCountByGender
(
    @gender nchar(1)    --函数的参数列表
)
returns int    --函数的返回值类型
as
begin
    declare @count int    --返回值变量
    
    set @count = (
        select count([sid])
        from T_StuInfo
        where gender = @gender
    )

    return @count    --执行返回
end
go
select dbo.GetCountByGender('男') as 数量    --调用函数时, 必须加上所有者

--内嵌表值函数(Inline Table-valued Functions)
if exists(select * from sysobjects where [name]='GetInfoByStatus' and [type] = 'IF')
    drop function GetInfoByStatus
go
create function GetInfoByStatus
(
    @status nchar(2)    --参数列表
)
returns table    --返回值为数据表
as
return 
(
    select *
    from T_StuInfo
    where [status] = @status
)
go
select * from dbo.GetInfoByStatus('毕业')    --调用函数时, 必须加上所有者
go

--多语句表值函数(Multistatement Table-valued Functions)
if exists(select * from sysobjects where [name]='GetNameBySegAge' and [type] = 'TF')
    drop function GetNameBySegAge
go
create function GetNameBySegAge
(
    @firstage int,    --18岁
    @secondage int,    --18-30岁
    @thirdage int    --30岁以上
)
returns @infotable table    --定义返回值变量(table类型), 以及返回值表的字段
        (
            AgeSegment nvarchar(30),
            countnum int
        )
as
begin
    --局部变量, 用于填充返回值表
    declare @currentcount int        --当前年龄段的计数
    declare @currentdesc nvarchar(30)    --当前年龄段的描述

    set @currentcount = (select count([sid]) from T_StuInfo 
        where age<@firstage)
    set @currentdesc = '小于(含)-'+Convert(nvarchar(10),@firstage)+'岁'
    insert into @infotable(AgeSegment,countnum) values(@currentdesc,@currentcount)

    set @currentcount = (select count([sid]) from T_StuInfo 
        where age>=@firstage and age<@secondage)
    set @currentdesc = Convert(nvarchar(10),@firstage)+'岁(含)-'+Convert(nvarchar(10),@secondage)+'岁'
    insert into @infotable(AgeSegment,countnum) values(@currentdesc,@currentcount)

    set @currentcount = (select count([sid]) from T_StuInfo 
        where age>=@secondage and age<@thirdage)
    set @currentdesc = Convert(nvarchar(10),@secondage)+'岁(含)-'+Convert(nvarchar(10),@thirdage)+'岁'
    insert into @infotable(AgeSegment,countnum) values(@currentdesc,@currentcount)

    set @currentcount = (select count([sid]) from T_StuInfo 
        where age>=@thirdage)
    set @currentdesc = Convert(nvarchar(10),@thirdage)+'岁(含)-不限'
    insert into @infotable(AgeSegment,countnum) values(@currentdesc,@currentcount)

    return    --执行已定义的返回值表的返回操作
end
go
select * from dbo.GetNameBySegAge(20,30,40)    --调用函数时, 必须加上所有者

--###################################################################################

--游标:
begin transaction MoveUserInfoTrans
begin
    declare @errcount int
    set @errcount = 0

    declare MoveUserInfoTwo cursor        --声明游标
    for
        select userid,userpwd from UserInfoTwo

    open MoveUserInfoTwo    --打开游标,准备开始读取操作

        declare @userid nvarchar(20),@userpwd nvarchar(30)
        fetch next from MoveUserInfoTwo into @userid,@userpwd    --执行读取
        while(@@fetch_status=0)
        begin
            insert into UserInfoOne(userid,userpwd) values (@userid,@userpwd)
            
            if(@@error!=0)    --验证单次操作的是否成功
            begin
                set @errcount = @errcount + 1
                break
            end
            fetch next from MoveUserInfoTwo into @userid,@userpwd    --取下一条
        end
    close MoveUserInfoTwo    --完成游标操作,关闭游标
    deallocate MoveUserInfoTwo    --释放游标

    if(@errcount = 0)    --用if验证事务的操作过程
    begin
        commit transaction MoveUserInfoTrans
        print '事务已成功提交!'
    end
    else
    begin
        rollback transaction MoveUserInfoTrans
        print '执行过程出错,事务已回滚!'
    end
end
go

Sql Server从入门到精通 create proc | prodedure 存储名( [ {@参数 数据类型 } [ = 默认值 ] [ out | output ] , {@参数 数据类型 } [ = 默认值 ] [ out | output ] , . . . ] )asbeginendgo。 阅读详情

相关推荐

MSsql基础操作

6、修改完成后,点击下面的确定进行恢复,这时会出现一个进度条,提示恢复的进度,恢复完成后系统会自动提示成功,如中间提示报错,请记录下相关的错误内容并询问对SQL操作比较熟悉的人员,一般的错误无非是目录错误或者文件名重复或者文件名错误或者空间不够或者数据库正在使用中的错误,数据库正在使用的错误您可以尝试关闭所有关于SQL窗口然后重新打开进行恢复操作,如果还提示正在使用的错误可以将SQL服务停止然后重起看看,至于上述其它的错误一般都能按照错误内容做相应改动后即可恢复。备份和还原策略包含备份部分和还原部分。

2301_76901027的博客 1608

数据库--SqlServer详解

数据库–SQLServer详解 零、文章目录 一、数据库概念 1、数据库基本概念 (1)数据库(DataBase:DB) 数据库是是按照数据结构来组织、存储和管理数据的仓库。---->存储和管理数据的仓库 (2)数据库管理系统(Database Management System:DBMS) 是专门用于管理数据库的计算机系统软件。数据库管理系统能够为数据库提供数据的定义、建立、维护、查询和统计等操作功能,并完成对数据完整性、安全性进行控制的功能。 注意:我们一般说的数据库,就是指的DBMS 2、数据库

李宥的博客 6万+

SQLServer数据库基础教程

SQLServer数据库基础教程 SQLServer数据库基础教程【千锋软件测试】 (1.数据库技术&) 1.数据库技术& sqlserver2008安装 不设置密码 点击添加当前用户 不要勾选错误报告 不要修改配置文件地方,最多该盘符 (2.数据库技术续一) 2.数据库技术续一 (3.数据库技术续一) 3.数据库技术续一 DBMS (4.数据库技术续二) 4.数据库技术续二 (5.数据库技术续三) 5.数据库技.

悦分享 3万+

SQL Server的安装和简单使用

分享了SQL Server以及它的连接工具的安装和简单使用

xieyichun_的博客 3万+

鸿蒙OS开机动画背后的秘密:从代码到视觉的完整解析(附源码分析)

本文深入解析了鸿蒙OS开机动画的系统级实现原理。从bootanimation服务的启动配置、与Render Service的协同,到基于VSync信号的帧率同步与绘制流水线,结合OpenHarmony源码进行了逐行解读。文章揭示了其事件驱动的优雅退出机制,并探讨了视频支持、多屏适配等高级特性与定制化可能,为开发者理解鸿蒙图形子系统提供了硬核技术视角。

sony5的博客 983

Uni-App推送踩坑实录:从云函数URL化到后台推送失效,我都经历了什么

本文详细记录了Uni-App开发者在接入uni-push 2.0时遇到的推送失效问题,从云函数URL化到后台推送失效的完整排查过程。通过系统化分析和与官方沟通,最终找到解决方案,并分享了预防措施与最佳实践,帮助开发者避免类似问题。

weixin_42552716的博客 492

nodejs操作sqlserver数据_NodeJS中使用mssql模块连接SQLServer数据库

一、使用mssql连接SQLServer数据库1、创建数据库UserDB,再创建t_user表,为t_user表添加一些数据我在自己的Window10系统中已经安装了SQLServer 2012,本人还是习惯使用Navicat Premium 12这个数据库客户端软件,觉得SQL Server Management Studio相对笨重一些。在Navicat Premium 12中连接SQLSer...

weixin_34621186的博客 1433

DeerFlow 系列教程 第十六篇 | 实战案例一——用 DeerFlow 进行深度研究

在日常使用 AI Agent 时,最常见的需求之一就是对某个主题进行全面、系统的研究。多源信息采集——不能只依赖单次搜索,需要从多个角度搜索不同来源交叉验证——不同来源的信息需要相互印证,避免单一信源的偏差结构化输出——最终产出的不是零散的信息片段,而是有组织的研究报告传统做法是人工反复搜索、阅读、整理。传统手工研究: DeerFlow 深度研究:人工搜索 → 阅读页面 → 记笔记 提交研究主题│ │▼ ▼换关键词 → 再搜索 → 再阅读 ┌──────────────────┐。

weixin_43674738的博客 177

nodejs操作sqlserver数据_node.js 使用 mssql 开源库操作 sqlServer 数据库

node.js 连接 ms SqlServer ,你没有看错,就是这样.我们抛开一切操作系统的歧视, 当你需要用到的数据源存储在 SqlServer 中,那么就会遇到我们今天要说的情况.一. mssql 下载安装mssql 默认使用Tedious驱动.Tediousby Mike D Pilsbury (pure javascript - windows/osx/linux) 默认支持(wind...

weixin_42166623的博客 1135

建立时间与保持时间

建立时间与保持时间的计算 文章目录1、建立时间与保持时间概述及模型2、寄存器之间数据传输时建立时间与保持时间3、寄存器之间数据传输时建立时间余量与保持时间余量计算4、 FPGA允许的最大时钟频率或最小时钟周期5、计算FPGA时钟所允许的最大频率或最小时钟周期6、违反建立和保持时间会发生什么? 1、建立时间与保持时间概述及模型 建立时间(Tsu): 在时钟采样之前,输入必须稳定一段时间,该时间量称为建立时间。 理想最优建立时间: 保持时间(Th): 在时钟沿之后输入到触发器稳定所需的最短时间。 理想最优

Crazzy_M的博客 3万+

Coze工作流的代码无法安装第三方模块(依赖)怎么办?(如何解决Coze工作流代码缺少模块(依赖)的问题)

Coze工作流的代码无法安装第三方模块(依赖)怎么办?(如何解决Coze工作流代码缺少模块(依赖)的问题)

XiaoqiangClub的博客 4373

nodejs操作sqlserver数据_SQLServer数据库之Nodejs之mssql模块的封装

本文主要向大家介绍了SQLServer数据库之Nodejs之mssql模块的封装,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助。在nodejs中,mssql模块支持sqlserver数据库操作。今天将mssql模块的某些功能封装为一个类,方便以后调用。封装的功能有执行存储过程,执行查询语句操作等。如果本篇文章对大家有帮助,那就再好不过了!要使用mssql模块,请先用np...

weixin_39575775的博客 802

使用OpenSSL创建多级CA证书链签发证书并导出为pkcs12/p12/pfx文件

文章目录1. 生成根CA证书并自签2. 生成二级CA证书并使用CA证书签发3. 生成服务器证书并使用二级CA证书签发4. 制作CA证书链5. 打包为p12文件6. 注意事项7. 使用`openssl x509`和`openssl ca`签发CA证书的差别 操作步骤: 生成根CA证书并自签 生成二级CA证书并使用CA证书签发 生成服务器证书并使用二级CA证书签发 制作CA证书链 打包为p12文件 1. 生成根CA证书并自签 # 创建root-ca并自签名 openssl req -x509 -newkey

Laurence的技术博客 9579

mssql 操作 mysql_通过SQLServer 2008 操作 MySQL的方法

数据库测试环境1. SQL Server 20082. MySQL 5.1.36Database: TestTable: TestTable创建MySQL 测试表CREATE TABLE `testtable` (`id` int(11) DEFAULT NULL,`name` varchar(50) DEFAULT NULL,`age` int(11) DEFAULT NULL)创建Link ...

weixin_39574065的博客 247

国网DLT698.45协议——采集系统、数据交换(三)

物理层规定了通信单元或其他终端设备与多功能电表的通信接口,接口的物理与电气特性。常见的物理层传输方式有对于RS485、红外、载波通信等通信信道。将数据包装成可传输的帧格式。1、根据通信配置参数完成端口的初始化配置,通信状态初始化为准备接收状态,初始化相关计数器及缓冲区指针。2、链路层及控制子模块负责通信数据的收发控制,各类超时问题的处理。无通信数据接收时,定时初始化通信端口。3、完成协议帧的解析及应答。

一个bug是bug,多个bug能work 8061

提示工程ROI计算模板:架构师私藏Excel表(含自动计算)

花3个人力优化提示词,到底能带来多少实际收益?“这个提示工程项目值得做吗?投入10万成本,多久能回本?“不同LLM应用的提示词优化优先级怎么排?全凭经验拍脑袋?当前90%的企业在提示工程投入上缺乏标准化的量化评估方法,导致资源浪费(盲目投入无回报项目)或错失机会(低估高价值优化点)。ROI = (总收益 - 总成本) / 总成本 × 100%对于提示工程,总成本=显性成本+隐性成本,总收益=直接收益+间接收益(后文会详细拆解)。提示工程的价值不是“玄学”,而是可以用数据量化的投资行为。本文分享的。

AI开发架构师 1087

nodejs操作sqlserver数据_nodejs + express + mssql 封装数据操作

在网上看到有一些nodejs连接sqlserver的相关教程,但非常少,而且很多都有错,特别是操作数据库的语句,在这里我做了一番整理,搭建一个完整的nodejs后台,并封装sqlserver的操作。nodejs的安装和express的安装在这里就不多说,网上都有教程,不会的网上一搜都有。然后安装mssql,在terminal中进入你的项目文件夹,输入命令:npm install mssql,等待片...

weixin_42290401的博客 981

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

nodejs操作sqlserver数据_nodejs操作sqlserver数据库简单封装-node-mssql模块

一 安装node-mssql模块npm install node-mssql官方文档:https://www.npmjs.com/package/node-mssql二 封装连接数据库代码db.js:var node_mssql = require('node-mssql');/*** 连接数据库* */var queryObj = new node_mssql.Query({host: 'x.x...

weixin_30692969的博客 799

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

nodejs操作sqlserver数据库简单封装-mssql模块

注意:开启sqlserver服务器允许远程连接的步骤,自行百度,很多经验,nodejs连接sqlserver,最好把防火墙的入站规则关闭,或者允许入站.这个封装只是sql语句查询,看懂这个,其他的可以自行扩展,支持连接池. 一 安装mssql模块 npm install mssql 官方文档:https://www.npmjs.com/package/mssql 二 封装连接数据库代码...

Viote. 1965

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

php mssql操作实例,PHP用MSSQL扩展操作SQL Server

mssql适用于php版本<7,php版本在7以上的可以选择使用pdo,当然必须安装pdo_sqlsrv扩展才能使用。SELECTini_set('mssql.charset', 'UTF-8');$conn = mssql_connect(HOST, USER, PWD) or die ("connect failed");mssql_select_db(DB, $conn);$sql =...

weixin_34355337的博客 198

nodejs操作sqlserver数据_nodejs基于mssql模块连接sqlserver数据库的简单封装操作示例...

本文实例讲述了nodejs基于mssql模块连接sqlserver数据库的简单封装操作。分享给大家供大家参考,具体如下:注意:开启sqlserver服务器允许远程连接的步骤,自行百度,很多经验,nodejs连接sqlserver,最好把防火墙的入站规则关闭,或者允许入站.这个封装只是sql语句查询,看懂这个,其他的可以自行扩展,支持连接池.一 安装mssql模块npm install mssql二...

weixin_35990358的博客 354

nodejs操作sqlserver数据_实例分析nodejs基于mssql模块连接sqlserver数据库的简单封装操作...

本文主要介绍了nodejs基于mssql模块连接sqlserver数据库的简单封装操作,结合实例形式分析了nodejs中mssql模块的安装与操作sqlserver数据库相关使用技巧,需要的朋友可以参考下,希望能帮助到大家。注意:开启sqlserver服务器允许远程连接的步骤,自行百度,很多经验,nodejs连接sqlserver,最好把防火墙的入站规则关闭,或者允许入站.这个封装只是sql语句查...

weixin_35823067的博客 588

【PHP】php操作MSSQL

配置PHP操作MSSQL驱动 在指定目录安装PHP链接MSSQL驱动SQLSRV58.exe 新建一个文件夹,将驱动安装在该目录下,方便后边找。 也可以去微软官网找最新版驱动 复制对应版本的DLL至PHP,ext目录下 在步骤1安装驱动的目录下,选择对应版本的dll。 在PHP安装目录下找ext文件夹,并将对应版本的dll复制到改文件夹下: 以phpstudy的php为例: …/phpstudy_pro/Extensions/php/php7.3.4nts/ext/ 讲下列两个DLL 复制到上述目录

weixin_36576175的博客 355

PHP操作mssql的db类

  // Author: YangYu niceses@126.com // msn: yangyucn@live.cnclass cls_mssql{        var $link;        var $querynum = 0;         function connect($dbsn, $dbun, $dbpw, $dbname) {        if($this->l

再见伍德 2163

rust如何操作sqlserver

rust中操作mysql和pgsql的技术非常多,也很成熟。作为一个sql server多年的使用者,我总不能用rust之后,就放弃mssql吧。我尝试了一下,我们国人开发的rbatis,rbatis是支持mssql的。但是很遗憾,我使用的是兼容win7的rust 1.77.2这个版本,rbatis在这个环境并没有跑起来。但是这个库只有339星,热度非常低,我们来看看,如何使用tiberius。经过测试,并没有发现tiberius有什么问题,你可以愉快地用它来操作sql server。

mrdzhu的博客 1411
上一篇: SqlDbHelper数据库连接类
下一篇: windows和linux下编译draco
落魄的佩奇
博客等级 码龄10年 41粉丝 85原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值