SQL Server T-SQL

use bbsDB
insert dbo.bbsUsers(Uname,Upassword,Uemail,Ubirthday,Usex,Uclass,Uremark,UregDate,Ustate,Upoint)
select '可卡','s007fdfd','[email protected]','1978-07-09',1,1,'我要去雲南','2008-10-06',1,200 union
select '心酸果凍','88fdfdf','[email protected]','1980-08-09',0,2,'走遍天涯海角','2008-10-10',2,600 union
select '冬天裏爾','007ppp','[email protected]','1979-07-09',1,3,'愛迷失子那裏','2005-10-06',4,1200 union
select 'Super','fsfjhj','[email protected]','1977-07-09',1,5,'BBs大版主','2007-10-06',1,5000

 

use bbsDB  
GO
if exists(select * from sysobjects where name='bbsSection')
print '存在'
else 
print '不存在'
--這段代碼最好要加上,否則在執行下面語句時候有可能會出現bbsUsers不存在的錯誤提示


print  'SQL Server版本號'+@@version
print  'SQL Server服務器名稱'+@@servername
update   dbo.bbsUsers set Upassword='123' where Uname='可卡'
print  '錯誤號'+convert(varchar(5),@@error)


declare @UID int
declare @Uname varchar(15)
declare @point int
set @Uname='可卡'


select @UID=UID from  dbo.bbsUsers where @Uname=Uname
print '個人資料如下'
select 暱稱=Uname,等級=Uclass,個人說明=Uremark,積分=Upoint from dbo.bbsUsers where UID=@UID

select @point=Upoint from dbo.bbsUsers where UID=@UID


print @Uname+'發帖發帖如下'
select 發帖時間=Ttime,點擊率=TclickCount,主題=Ttopic,內容=Tcontents from dbo.bbsTopic where TuID=@UID

print @Uname+'回帖如下'
select 回帖時間=Rtime,點擊率=RclickCouont,回覆內容=Rcontents from dbo.bbsReply where RuID=@UID


if(@point<50)
print+'可卡:無權發帖'
else
print+'可卡:有權發帖'

 


print+'上機二'
print+'開始提分,請稍後。。。'
declare @AVG_Point int
declare @finalAdd int
set @finalAdd=0


select @AVG_Point=avg(Upoint) from dbo.bbsUsers
while(@AVG_Point<=2000)
begin
update dbo.bbsUsers set Upoint=Upoint+50
select @AVG_Point=avg(Upoint) from dbo.bbsUsers
set @finalAdd =@finalAdd+50
end


print+'------------加分後的用戶級別情況--------------'

print+'提升分值'+convert(varchar(10),@finalAdd)
select 暱稱=Uname,星級=case
          when Upoint<499 then '*'
          when Upoint between 500 and 1000 then '**'
          when Upoint between 1001 and 2000 then '***'
          when Upoint between 2001 and 4000 then '****'
          when Upoint between 4001 and 5000 then '*****'
     
end
,積分=Upoint from dbo.bbsUsers


--階段三
use bbsDB
declare @Uid int   --要查詢的用戶的UID
declare @num_fatie int --發帖數
declare @num_huitie int --發帖數
declare @count int --帖數總計

select @Uid=UID from dbo.bbsUsers where Uname='心酸果凍'
select @num_fatie=count(*) from dbo.bbsTopic where TuID=@Uid
select @num_huitie=count(*) from dbo.bbsReply where RuID=@Uid


if (@num_fatie=0)
 print '心酸果凍發帖數爲'+convert(varchar(5),@num_fatie)
else
 begin
  print '心酸果凍發帖數爲:'+convert(varchar(5),@num_fatie)+'帖子如下:'
  select 發帖時間=Ttime,點擊率=TclickCount,主題=Ttopic,內容=Tcontents from dbo.bbsTopic where TuID=@Uid
 end

if (@num_huitie=0)
 print  '心酸果凍回帖數爲'+convert(varchar(5),@num_huitie)
else
 begin
  print '心酸果凍回帖數爲:'+convert(varchar(5),@num_fatie)+'帖子如下:'
  select 發帖時間=Rtime,內容=Rcontents from dbo.bbsReply where RuID=@Uid
 end


set @count=@num_fatie+@num_huitie
print '心酸果凍帖數總計:'+convert(varchar(5),@count)+'功臣級別:'+case
          when @count between 0 and 9   then '新手上路'
          when @count between 10 and 20 then '俠客'
          when @count between 21and 30  then '騎士'
          when @count between 31and 40  then '精靈士'
          when @count between 41 and 50   then '光明使者'
          when @count>50  then '法老'
end

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章