SQL邏輯判斷(begin end)


use master
--創建文件夾
exec xp_cmdshell 'md d:project'
exec xp_cmdshell 'dir d:'
--判斷數據庫是否存在
if exists(select * from sysdatabases where name='stuDB')
--如果存在先刪除
drop database stuDB
--創建數據庫
create database stuDB
--創建主數據庫文件
  on primary(
 name='stuDB_data_1',
 filename='d:\project\stuDB_data_1.mdf',
 size=5mb,
 maxsize=100mb,
 filegrowth=10%
),

--創建次要數據庫文件
(
 name='stuDB_data_2',
 filename='d:\project\stuDB_data_2.ndf',
 size=5mb,
 maxsize=100mb,
 filegrowth=10%
 )
--創建日志文件
  log on(
 name='stuDB_log_1',
 filename='d:\project\stuDB_log_1.ldf'
),
  --創建其他日志文件
(
 name='stuDB_log_2',
 filename='d:\project\stuDB_log_2.ldf'
)
--批處理
go

use stuDB

select * from sysdatabases
select * from sysobjects

創建約束 constraint
主鍵 primary key
唯一約束 unique
檢查約束 check
莫認約束 default
外鍵約束 foreign
引用  references
創建約束 alter table 表名
 add constraint 為約束起名字
刪除約束 alter table 表名
 drop constraint 約束名


use stuDB
drop table stuInfo
drop table stuMarks

create table stuInfo(
 stuName varchar(20) not null,
 stuNo char(6) not null,
 stuSex char(2) not null,
 stuAge int not null, 
 stuSeat int identity (1,1),
 stuAddress varchar(50) not null
)
go

create table stuMarks(
 examNo char(7),
 stuNo char(6) not null,
 writtwenExam int not null,
 labeExam int not null
)
go

alter table stuInfo --主鍵約束
 add constraint PK_stuNo primary key (stuNo)

alter table stuInfo --唯一約束
 add constraint UK_stuId unique (stuId)

alter table stuInfo --默認約束
 add constraint DK_stuAddress default('地址不詳') for stuAddress

alter table stuInfo --檢查約束
 add constraint CK_stuAge check (stuAge between 15 and 40)

alter table stuMarks--外鍵約束
 add constraint FK_stuNo foreign key (stuNo)references stuInfo(stuNo)

alter table stuMarks --檢查約束
 add constraint CK_writtwenExam check(writtwenExam between 0 and 100)

alter table stuMarks--檢查約束
 add constraint CK_labeExam check(labeExam between 0 and 120)

alter table stuMarks--刪除約束
 drop constraint FK_stuNo

 


insert into stuInfo values('張秋麗','s25301','',18,'北京海淀')

insert into stuInfo values('李文才','s25302','',28,default)

insert into stuInfo values('李斯文','s25303','',22,default)

insert into stuInfo values('歐陽俊雄','s25304','',34,default)

insert into stuInfo values('梅超風','s25318','',23,default)


select * from stuInfo

declare @name varchar(20)--定義一個局部變量

set @name='李文才'--為變量賦值

declare @seat int--定義個變量

select @seat=stuSeat from stuInfo where stuName=@name--查找stuName的編號並且為stuSeat賦值

select * from stuInfo where (stuSeat=@seat+1)or(stuSeat=@seat-1)--查詢姓名並且stuSeat+1 or stuSeat-1

set nocount on --不顯示影響的行數

print @@error

select * from stuInfo
--常用的全局變量
print '最后一個T-SQL錯誤的錯誤號:'+@@error

print '最后一次插入的標識值'+ @@identity

print '本系統的版本號為:'+@@version

print '本地服務器的名稱:'+@@servername
--不常用的全局變量
print '當前使用的語言的名稱:'+@@language

print '可以創建的同時連接的最大數目:'+@@max_connections

print '受上一個SQL語句影響的行數:'+@@rowcount

print '當前連接打開的事物數:'+@@trancount


insert into stuMarks values('s271811','s25303',80,58)

insert into stuMarks values('s271813','s25302',50,90)

insert into stuMarks values('s271816','s25301',77,82)

insert into stuMarks values('s271818','s25318',45,65)

select * from stuMarks
select * from stuInfo

declare @avg int
select @avg=avg(writtwenExam) from stuMarks
print '本班成績為:'+convert(varchar(4),@avg)
if(@avg<70)
 begin
  print '本班成績較差:'
         select top 3 * from stuMarks order by writtwenExam
 end
    else
 begin
  print '成績優秀:'
   select top 3 * from stuMarks order by writtwenExam desc
 end
--先統計未及格人數
while(1=1)
  begin
 declare @noopass int
 select @noopass=count(*) from stuMarks where writtwenExam<60
 if(@noopass>0)
  begin
   update stuMarks set writtwenExam=writtwenExam+2
  end
 else
  begin
  break;
 end
end
select * from stuMarks

--筆試成績有兩個成績未及格的狀態
while(1=1)
 begin
  declare @noopass int 
 select @noopass=count(*) from stuMarks where writtwenExam<60
 if(@noopass=2)
  begin
  break;
     end
 else
  begin
  update stuMarks set writtwenExam=writtwenExam-2
 end
end

--采用美國的ABCDE等級來評定(筆試成績)
select *, 成績=case
 when writtwenExam<60 then 'E'
 when writtwenExam between 61 and 70 then 'D'
 when writtwenExam between 71 and 80  then 'C'
 when writtwenExam between 81 and 90 then 'B'
 else
  'A'
end
from stuMarks
--增加一列平均分並采用美國的ABCDE等級來評定(筆試成績)
select *,平均成績=(writtwenExam+labeExam)/2, 等級=case
 when writtwenExam<60 then ''
 when writtwenExam between 61 and 70 then '一般'
 when writtwenExam between 71 and 80  then ''
 when writtwenExam between 81 and 90 then ''
 else
  ''
end
from stuMarks

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM