存储过程

-----------全选 create【创建】 procedure 【存储过程名】usp_GetAllStudents as  select【查询】 *【*代表所有的列】 from【表名】 student go -----------根据主键查询 create【创建】 procedure 【存储过程名】usp_GetStudentById ( @StudentId int )【括号里为参数,为用户提供值】 as  select【查询】 *【*代表所有的列】 from 【表名】student 【条件】where Studentid=@StudentId go ------------新增 create【创建】 procedure 【存储过程名】usp_InsertStudent ( @StudentName nvarchar(50), @StudentGender bit, @StudentBirthday Date )【括号里为参数,为用户提供值】 as  insert【插入】 into 【表名】student

 【需要插入的值】values(@StudentName,@StudentGender,@StudentBirthday)  select @@IDENTITY【用获取主键列的值】 go -------------修改 create【创建】 procedure 【存储过程名】usp_UpdateStudent ( @StudentId int, @StudentName nvarchar(50), @StudentGender bit, @StudentBirthday Date )【括号里为参数,为用户提供值】 as  update 【表名】student

 set 【要修改的信息】

studentname=@StudentName,

studentgender=@StudentGender,

StudentBirthday=@StudentBirthday 【修改的条件】

where studentid=@StudentId go -------------删除 create【创建】 procedure 【储存过程名】usp_DeleteStudent (  @StudentId int ) as  delete【删除】 from 【表】student 【删除条件】where studentid=@StudentId go ------知识补偿

1.实现多条数据插入 insert to 表名 select '值1','值2','值3' union select '值1','值2','值3' union select '值1','值2','值3'

 

2.实现表结构跟表数据拷贝(前提创建一个新表出来) select * into 新表 from 旧表

 

3.实现向一张表插入另一张表的数据 inert into 新表 (列1,列2,列3) select 列1,列2,列3 from 旧表 【注释】插入中文值建议数值前加N,可以防止乱码(针对Nvarchar类型) 如:N"张三"

 

4.Truancate删除和Delete删除的区别

【语法】:Truncate table 表名 【Truncate与Delete语句区别】1.delete 语句删除数据时候,自动编号没有恢复默认值,Trancate可以恢复默认

值                             2.delete 语句可以通过where条件进行删除,Trancate必须删除所有语句                             3.truncate语句删除数据时,速度跟性能比delete语句高得很多,好的多(Truncate删除不会记录日记,Delete可以记录日志)                             4.Truncate语句删除数据时,不会触发触发器,Delete语句可以

 

5.Union 和 UnionAll连接表的使用与区别

Union 【语法】:Select UName,Age from userInfo where age<=10   Union   Select UName,Age from UserInfo where age>=20 【注释】:Union会取消冗余项

 

Union all 【语法】:Select UName,Age from userInfo where age<=10   Union all   Select UName,Age from UserInfo where age>=20

【注释】:1.连接显示的字段个数必须一致,类型必须一致           2.Union All 不会取消冗余项           3.建议使用Union All

 

6.拷贝表结构但是没有数据 【语法】: select top 0* into 新表 from 旧表

insert into 表名 output inserted.列名 values('值'.'值')

7.Select语句先后顺序,还有游标

在SqlServer中有顺序的集合可以成为游标 在SqlServer中的表可以视为集合,可以Select,Ordey by 【注释】:Ordey by之后集合(表)就不能成为集合了,可以添加top实现集合效果 在SqlServer中语句执行顺序 form--->where--->groud by---->having--->select--->order by 【注释】:having 筛选的列名来至Select 映射列 【注释】:Select里面顺序Distinct,Top,Select

posted @ 2016-04-05 16:11  付出不亚于努力  阅读(233)  评论(0)    收藏  举报