- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
第9章 Trnsact-SQL程序设计
第9章 transact-sql程序设计
exec sp_addtype test_add,varchar(10),not null
declare @int_var int
select @int_var =12 /*给@int_var赋值*/
select @int_var /*将@int_var的值输出到屏幕上*/
在一条语句中可以同时对几个变量进行赋值,例如,
declare @lastname char(8),@firstname char(8),@birthdate datetime
select @lastname=smith,@firstname=david,@birthdate=1985-2-20
select @lastname,@firstname,@birthdate
【例9-1】 使用select语句从customer表中检索出顾客编号为c0002的行,再将顾客的名字赋给变量@customer。
declare @customer varchar(40),@curdate datetime
select @customer=customer_name,@curdate=getdate()
from customer
where customer_id=c0002
【例9-2】 将sell_order表中的transporter_id列值为“t001”、goods_id列值为“g00003”的order_num列的值赋给局部变量@order_num。
declare @order_num float
update sell_order
set @order_num=order_num*2
/*@order_num为局部变量,order_num为sell_order表中的列名称*/
where transporter_id=t001 and goods_id=g00003
【例9-3】 计算employee表的记录数并赋值给局部变量@rows。
declare @rows int
set @rows=(select count(*) from employee)
select @rows
【例9-4】 使服务器产生服务,并显示错误号。
raiserror(miscellaneous error message,16,1) /*产生一个错误*/
if @@error0
select @@error as last error
【例9-5】 捕捉例9-4中服务器产生的错误号,并显示出来。
declare @my_error int
raiserror(miscellaneous error message,16,1)
select @my_error=@@error
if @my_error0
select @my_error as last error
【例9-6】 使用@@spid返回当前用户进程的id。
select @@spid as id,system_user as login name,user as user name
【例9-7】 在sales数据库中创建jobs表,并在表中插入带有identity列的行,并且使用@@identity来显示新行中所用的identity值。
create table jobs (
[job_id] [smallint] identity (1, 1) not null ,
[job_desc] [varchar] (50) collate chinese_prc_ci_as not null ,
[min_lvl] [tinyint] not null ,
[max_lvl] [tinyint] not null
) on [primary]
insert into jobs(job_desc,min_lvl,max_lvl)
values(accountant,12,125)
select @@identity as identity
【例9-8】 创建两个过程:innerproc和outerproc。outerproc过程用来调用innerproc过程,innerproc过程用来显示@@nestlevel的设置。
① 定义innerproc为内嵌过程。
create procedure innerproc as
select @@nestlevel as inner level
go
② 定义outerproc为外层过程。
create procedure outerproc as
select @@nestlevel as outer level
exec innerpro
您可能关注的文档
- 第1节 化学验基本方法(自拟).doc
- 第1讲练习题参答案.doc
- 第1节 生物与生物练习.doc
- 第1课发达的古农业.doc
- 第20周第1课八上二元一次方程组回顾与思考.doc
- 第20讲(同名藏规则和课后习题解答).doc
- 第20讲:第五第四节:建程序和组织(2013年新版).doc
- 第22章 一元次方程全章教学案-.doc
- 第22课 引领代的思考.doc
- 第22课 时代点鲜明的明清文化(二):.doc
- 主题课程整理大班上.doc
- 2026人教版小学语文三年级上册期末综合试卷3套(打印版含答案解析).docx
- 2026人教版小学语文四年级下册期末综合试卷3套(打印版含答案解析).docx
- 2026人教版小学二年级上册数学期末综合试卷精选3套(含答案解析).docx
- 2026人教版小学语文四年级上册期末综合试卷3套(含答案解析).docx
- 2026人教版小学二年级下册数学期末综合试卷3套(打印版含答案解析).docx
- 2026年地理信息行业年终总结汇报PPT.pptx
- 板块四第二十一单元封建时代的欧洲和亚洲 中考历史一轮复习.pptx
- 中考历史一轮复习:板块四第二十单元古代亚、非、欧文明+课件.pptx
- 第二次工业革命和近代科学文化中考历史一轮复习.pptx
原创力文档


文档评论(0)