【网学网提醒】:以下是网学会员为您推荐的SQLServer数据库编程基本语法汇总,希望本篇文章对您学习有所帮助。
一、定义变量--简单赋值declare@aintset@a=55print@a
--使用select语句赋值declare@user1nvarchar(5050)50select@user1='张三'print@user1declare@user2nvarchar(5050)50select@user2=NamefromST_UserwhereID=11print@user2
--使用update语句赋值declare@user3nvarchar(5050)50updateST_Userset@user3=NamewhereID=11print@user3二、表、临时表、表变量临时表、--创建临时表1createtable#DU_User1([ID][int]NOTNULL,[Oid][int]NOTNULL,
[Login][nvarchar](50NOTNULL,50)50[Rtx][nvarchar](4)NOTNULL,4[Name][nvarchar](5)NOTNULL,5[Password][nvarchar](max)NULL,[State][nvarchar](8)NOTNULL8);
--向临时表1插入一条记录insertinto#DU_User1(ID,Oid,[Login],Rtx,Name,[Password],State)values(1002,'LS','0000','临时','321','特殊');100,2100
--从ST_User查询数据,填充至新生成的临时表select*into#DU_User2fromST_UserwhereID<88
--查询并联合两临时表select*from#DU_User2whereID<3unionselect*from#DU_User13
--删除两临时表droptable#DU_User1droptable#DU_User2--创建临时表CREATETABLE#t([ID][int]NOTNULL,[Oid][int]NOTNULL,
[Login][nvarchar](50NOTNULL,50)50[Rtx][nvarchar](4)NOTNULL,4[Name][nvarchar](5)NOTNULL,5[Password][nvarchar](max)NULL,[State][nvarchar](8)NOTNULL,8)
--将查询结果集(多条数据)插入临时表insertinto#tselect*fromST_User
--不能这样插入--select*into#tfromdbo.ST_User
--添加一列,为int型自增长子段altertable#tadd[myid]intNOTNULLIDENTITY(1,1)11
--添加一列,默认填充全球唯一标识altertable#tadd[myid1]uniqueidentifierNOTNULLdefault(newid())select*from#tdroptable#t--给查询结果集增加自增长列--无主键时:selectIDENTITY(int,1,1)asID,Name,[Login],[Password]into#tfrom11ST_Userselect*from#t
--有主键时:select(selectSUM(1)fromST_UserwhereID<=a.ID)asmyID,*from1ST_UseraorderbymyID--定义表变量declare@ttable(idintnotnull,msgnvarchar(50null50)50)insertinto@tvalues(1,'1')1insertinto@tvalues(2,'2')2select*from@t三、循环--while循环计算1到100的和declare@aintdeclare@sumintset@a=11set@sum=00while@a<=100100beginset@sum+=@aset@a+=11
endprint@sum
四、条件语句--if,else条件分支if(1+1=2)112beginprint'对'endelsebeginprint'错'end--whenthen条件分支declare@todayintdeclare@weeknvarchar(3)3set@today=33set@week=casewhen@today=1then'星期一'1when@today=2then'星期二'2when@today=3then'星期三'3when@today=4then'星期四'4when@today=5then'星期五'5
when@today=6then'星期六'6when@today=7then'星期日'7else'值错误'endprint@week五、游标declare@IDintdeclare@Oidintdeclare@Loginvarchar(5050)50--定义一个游标declareuser_curcursorforselectI
D,Oid,[Login]fromST_User--打开游标openuser_curwhile@@fetch_status0@@fetch_status=0begin--读取游标fetchnextfromuser_curinto@ID,@Oid,@Loginprint@ID--print@Loginendcloseuser_cur--摧毁游标
deallocateuser_cur六、触发器触发器中的临时表:Inserted存放进行insert和update操作后的数据Deleted存放进行delete和update操作前的数据--创建触发器CreatetriggerUser_OnUpdateOnST_UserforUpdateAsdeclare@msgnvarchar(5050)50--@msg记录修改情况select@msg=N'从“'+Deleted.Name+N'”修改为“'+Inserted.Name+'”'fromInserted,Deleted--插入日志表insertinto[LOG](MSG)values(@msg)--删除触发器droptriggerUser_OnUpdate七、存储过程--创建带output参数的存储过程CREATEPROCEDUREPR_Sum
@aint,@bint,@sumintoutputASBEGINset@sum=@a+@bEND
--创建Return返回值存储过程CREATEPROCEDUREPR_Sum2@aint,@bintASBEGINReturn@a+@bEND
--执行存储过程获取output型返回值declare@mysumintexecutePR_Sum1,2,@mysumoutput2print@mysum
--执行存储过程获取Return型返回值
declare@mysum2intexecute@mysum2=PR_Sum21,22print@mysum2
八、自定义函数函数的分类:1)标量值函数2)表值函数a:内联表值函数b:多语句表值函数3)系统函数--新建标量值函数createfunctionFUNC_Sum1(@aint,@bint)returnsintasbeginreturn@a+@bend--新建内联表值函数
createfunctionFUNC_UserTab_1(@myIdint)returnstableasreturn(select*fromST_UserwhereID<@myId)--新建多语句表值函数createfunctionFUNC_UserTab_2(@myIdint)returns@ttable([ID][int]NOTNULL,[Oid][int]NOTNULL,[Login][nvarchar](50NOTNULL,50)50[Rtx][nvarchar](4)NOTNULL,4[Name][nvarchar](5)NOTNULL,5[Password][nvarchar](max)NULL,[State][nvarchar](8)NOTNULL8)
asbegininsertinto@tselect*fromST_UserwhereID<@myIdreturnend--调用表值函数select*fromdbo.FUNC_UserTab_1(1515)15--调用标量值函数declare@sintset@s=dbo.FUNC_Sum1(10050100,5010050)print@s--删除标量值函数dropfunctionFUNC_Sum1谈谈自定义函数与存储过程的区别:谈谈自定义函数与存储过程的区别:一、自定义函数:自定义函数:1.可以返回表变量2.限制颇多,包括·不能使用output参数;·不能用临时表;·函数内部的操作不能影响到外部环境;·不能通过select返回结果集;·不能update,delete,数据库表;
3.必须return一个标量值或表变量自定义函数一般用在复用度高,功能简单单一,争对性强的地方。二、存储过程1.不能返回表变量2.限制少,可以执行对数据库表的操作,可以返回数据集3.可以return一个标量值,也可以省略return存储过程一般用在实现复杂的功能,数据操纵方面。