--声明变量
declare @age int
declare @name varchar(20)
--赋值(常量值)
set @age=18
select @name='张三'
--从数据库查出来并赋值
set @name=(select StudentName from Student where StudentNo=10000)
print @name
select @name=StudentName from Student where StudentNo=10000
print @name
go
--查找李文才相邻学号的学生
--1.找到李文才的学号
declare @StuNo int
select @StuNo=StudentNo from Student where StudentName='李文才'
print @StuNo
--2.李文才相邻学号的学生
select * from Student where StudentNo=@StuNo+1 or StudentNo=@StuNo-1
go
--set 和 select 区别
declare @address varchar(20)
--当查询语句返回多个值,set报错,select保留最后一个值
select @address=Address from Student
print @address
go
print '本地服务器的名称:'+@@servername
print 'sql的版本:'+@@version
select @@servername as '本地服务器的名称'
select @@version as 'sql的版本'
go
declare @StuNo int,@name varchar(20),@date datetime,@SubNo int,@score int
set @StuNo=10000 --学号赋值
set @date='2013-2-17' --日期赋值
--根据学号查姓名
select @name=StudentName from Student where StudentNo=@StuNo
--根据科目名称查科目号
select @SubNo=SubjectNo from Subject where SubjectName='java Logic'
--根据条件查询成绩
select @score=StudentResult from Result where StudentNo=@StuNo and SubjectNo=@SubNo and ExamDate=@date
print '姓名是:'+@name
print '成绩是:'+convert(varchar(20),@score)
print '成绩是:'+cast(@score as varchar(20))
go
--查询学号为20012学生的java考试成绩
declare @stuno int,@subno int,@score int
set @stuno=20012
select @subno=SubjectNo from Subject where SubjectName='java Logic'
select @score=StudentResult from Result where StudentNo=@stuno and SubjectNo=@subno
print '20012java成绩为:'+convert(varchar(20), @score)
go
--if-else
--查询java Logic的科目编号
declare @subno int,@date datetime,@avg decimal(5,2)
set @date='2013-2-17' --时间
select @subno=SubjectNo from Subject where SubjectName='java Logic'
print @subno
--查询2013-2-17java考试的平均分
select @avg=avg(StudentResult) from Result where ExamDate=@date and SubjectNo=@subno
if(@avg>=70)
begin
print '成绩优秀'
end
else
begin
print '成绩较差'
end
declare @age int
declare @name varchar(20)
--赋值(常量值)
set @age=18
select @name='张三'
--从数据库查出来并赋值
set @name=(select StudentName from Student where StudentNo=10000)
print @name
select @name=StudentName from Student where StudentNo=10000
print @name
go
--查找李文才相邻学号的学生
--1.找到李文才的学号
declare @StuNo int
select @StuNo=StudentNo from Student where StudentName='李文才'
print @StuNo
--2.李文才相邻学号的学生
select * from Student where StudentNo=@StuNo+1 or StudentNo=@StuNo-1
go
--set 和 select 区别
declare @address varchar(20)
--当查询语句返回多个值,set报错,select保留最后一个值
select @address=Address from Student
print @address
go
print '本地服务器的名称:'+@@servername
print 'sql的版本:'+@@version
select @@servername as '本地服务器的名称'
select @@version as 'sql的版本'
go
declare @StuNo int,@name varchar(20),@date datetime,@SubNo int,@score int
set @StuNo=10000 --学号赋值
set @date='2013-2-17' --日期赋值
--根据学号查姓名
select @name=StudentName from Student where StudentNo=@StuNo
--根据科目名称查科目号
select @SubNo=SubjectNo from Subject where SubjectName='java Logic'
--根据条件查询成绩
select @score=StudentResult from Result where StudentNo=@StuNo and SubjectNo=@SubNo and ExamDate=@date
print '姓名是:'+@name
print '成绩是:'+convert(varchar(20),@score)
print '成绩是:'+cast(@score as varchar(20))
go
--查询学号为20012学生的java考试成绩
declare @stuno int,@subno int,@score int
set @stuno=20012
select @subno=SubjectNo from Subject where SubjectName='java Logic'
select @score=StudentResult from Result where StudentNo=@stuno and SubjectNo=@subno
print '20012java成绩为:'+convert(varchar(20), @score)
go
--if-else
--查询java Logic的科目编号
declare @subno int,@date datetime,@avg decimal(5,2)
set @date='2013-2-17' --时间
select @subno=SubjectNo from Subject where SubjectName='java Logic'
print @subno
--查询2013-2-17java考试的平均分
select @avg=avg(StudentResult) from Result where ExamDate=@date and SubjectNo=@subno
if(@avg>=70)
begin
print '成绩优秀'
end
else
begin
print '成绩较差'
end