一、批處理
1、批處理時從客戶機傳遞到服務器上的一組完整的數據和SQL指令。
在一個批處理中可包含一條或多條SQL指令。
2、批處理是以GO關鍵字結束的。go並不是批處理命令,值用於標示批處理的結束。
3、批處理的示例
select 學生編號,姓名,年齡,性別from student
where 性別='男'
update student
set 年齡=年齡+1
where 姓名='小超'
select 學生編號,姓名,年齡,性別from student
where 性別='男'
go
4、某些特殊的命令是不能同其他命令出現在一個批處理文件中的。
create datebase、table、view
二、變量
1、變量是一個容器,由變量名、變量地址和變量值構成。
2、SQL變量包括兩種:
全局變量:應用於RDBMS的管理工作。
局部變量:應用於一個批處理操作中。
3、定義局部變量
局部變量名使用@開頭。
declare @變量名 數據類型
declare @變量名 數據類型,@變量名 數據類型,......
4、爲變量賦值
使用select語句爲變量賦值
select @變量名=變量值
select @a=20
select @變量名=變量值,@變量名=變量值,......
select @a=20,@b=30,@c=@a+@b
declare @a varchar(20)
select @a=姓名 from student
where 學費=23250
print @a
將學費最高的同學的姓名賦給變量@a
declare @a varchar(20),@b int
set @b=(select top 1 學費from student order by 學費desc )
select @a=姓名from student
where 學費=@b
print @a
使用set語句爲變量賦值
set @變量名=變量值
set @變量名=(查詢結果)
示例:declare @a varchar(20),@b varchar(20),@c varchar(20)
set @a='hello'
set @b=(select 姓名from student where 學費=23250)
set @c='你該繳費了'
print @a
print @b
print @c
5、變量的引用
利用select命令引用變量
select @變量名 ‘字段別名’
declare @a int
set @a=123456
select @a '本月工資'
利用print命令打印變量值
print @變量名
利用表達式引用變量
where 學費=@a
輸出***是年齡最大的同學
***是年齡最小的同學
declare @a int,@b int,@maxname varchar(20),@minname varchar(20)
select @a=max(年齡) from student
select @b=min(年齡) from student
select @maxname=姓名from student where 年齡=@a
select @minname=姓名from student where 年齡=@b
print @maxname+'是年齡最大的同學'
print @minname+'是年齡最小的同學'
declare @a varchar(20)
set @a=(select 姓名from student where 學生編號=(select 學生編號from res where 哲學=(select
max(哲學) from res)))
print @a+'的哲學成績最高'
if (select avg(聲樂) from res)>=80
print '你們的平均成績及格'
else
print '你們的平均成績不及格,一羣笨蛋'
if exists(select 姓名from student where 姓名='馬超')
print '查有此人'
else
print '查無此人'
如果學校的平均學費高於9000,輸出學校平均學費,輸出本校名師,教師姓 名,教師職稱
如果學校的平均學費低於9000,輸出本校價格優惠。
如果聲樂課的平均成績高於60,輸出平均成績及格,以及每個學生的聲樂課成績,和聲樂課不及格的同學。
如果聲樂課的平均成績低於60,輸出平均成績不及格。輸出聲樂課成績不及格而需要重新補考的學生姓名和聲樂課成績。
範例:
alter database cx add file(name=cxa,filename='c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\cxa.ndf', size=10,maxsize=200)
add file(name=cxb,filename='c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\cxb.ndf', size=10,maxsize=200,filegrowthe=10%) 更新數據
use test insert into table_3 values (2008001 ,'李','男',23,'滿','遼寧','音樂','大專',8000) create table tab1 (編號 int )
insert into tab1 values (2008001) select * from table_2
create table table_3 (學生編號 int, 姓名 varchar(16), 性別 char(2), 年齡 tinyint, 民族 varchar(12), 籍貫 varchar(50), 專業 varchar(20), 學歷 varchar(6), 學費 int) select * from table_3 insert into table_3 values (2008001,'張三','男',28,'漢族','北京','計算機','本科',8000)
擴充數據庫 alter database cx1 modify file (name=cx1_log, size=20,maxsize=200,filegrowth=20%) 創建表 create database test on (name=test,filename='C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\test.mdf', size=10,maxsize=100,filegrowth=5) alter database test add filegroup stu use test create table ss (學生編號 int identity not null primary key, 姓名 varchar(16), 性別 char(2), 年齡 tinyint, 民族 varchar(12), 籍貫 varchar(50), 專業 varchar(20), 學歷 varchar(6), 學費 int) sp_help ss sp_spaceused ss insert into ss values (2008001,'張三','男',28,'漢族','北京','計算機','本科',8000) insert into ss values (2008005,'張三','男',34,'滿','河北','音樂','大專',20000) update ss set 性別='男' where 學生編號=2008005 use test select *from ss select distinct 專業,學費 from ss 創建表 create table stu (學生編號 int,姓名 varchar(16),性別 char(2), 年齡 tinyint,民族 varchar(16),籍貫 varchar(16), 學歷 varchar(20), 專業 varchar(20),入學時間 datetime,學費 int, 教師編號 int)