- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第4章 Transact-SQL
作为查询条件一部分的查询称为子查询 * 表示为两步操作 * T-SQL允许SELECT多层嵌套使用,用来表示复杂的查询 * 子查询除了可以用在SELECT语句中,还可以用在INSERT、UPDATE及DELETE语句中。 * * insert into teacher(tno,tname,tsex) values(899,John,男) insert into student(sno,sname,sgender) select tno,tname,tsex from teacher select tno,tname,tsex into new_teacher from teacher 更改表中的现有数据。 update student set sname=null where sno=301 update student set sname=default where sno=301 * 删除的语法和选择的语法差不多 * select table from [table] * 用户自定义数据类型 :用户自定义数据类型依照基本数据类型定义。 * select getdate()+1 * 可以将ANY(SOME)或ALL关键字与比较运算符组合进行子查询。以“”比较运算符为例: ?ANY 表示至少大于一个值,即大于最小值。ANY(7 ,2 ,3) 表示大于2 ,因此,使用ANY 的子查询也可用MIN 集函数实现。 ?ALL 表示大于每一个值,即大于最大值。例如ALL(5 ,2 ,3) 表示大于5 ,使用ALL 的子查询也可用MAX 集函数实现。 ?=ANY 运算符与IN 等效。 ?ALL 与NOT IN 等效。 * * alter table student alter column sname varchar(50) update student set sname =rtrim(sname) select * from student where sname like 李_ Declare声明变量 * 关系数据库中的操作会对整个行集产生影响。由 SELECT 语句返回的行集包括所有满足该语句 WHERE 子句中条件的行。由语句所返回的这一完整的行集被称为结果集。 * GLOBAL:指定 cursor_name 指的是全局游标。 cursor_name 指的是局部游标。 cursor_variable_name:游标变量的名称,该名称引用一个游标。 * @cursor_variable_name 必须为 cursor 类型。 * declare @t_no varchar(50) declare t_cursor cursor for select tno from teacher open t_cursor fetch next from t_cursor into @t_no while @@fetch_status=0 Begin if(select count(*) from sc where cno in (select cno from course where tno=@t_no))3 begin delete from sc where cno in (select cno from course where tno=@t_no) update course set tno= where tno=@t_no end fetch next from t_cursor into @t_no End close t_cursor deallocate t_cursor * declare @s_no varchar(50),@t_no varchar(50),@s_count int,@t_count int select @t_no=tno from teacher where tname=白宏斌 select @t_count=count(*) from course where tno=@t_no declare s_cursor cursor for select sno from student open s_cursor fetch next from s_cursor into @s_no while @@fetch_status=0 Begin select @s_count=count(*) from sc where sno=@s_no and cno in (select cno from course where tno=@t_no) if @t_count=@s_count begin print @s_no+ +cast(@t_
文档评论(0)