今天学习啦所谓的高级语言啦
???????????????? 今天又回到啦SQL Servel語言的學習啦,嘿嘿,感覺這些都是我迫不及待要學習的啦,在這學習的每一天感覺好多的知識都是我樂意去學習的啦,我來到這里就是因為知道自己有太多不會的東西啦,所以每當接受新的知識時間感覺自己可以很輕松的學習啦,不是逼迫的自己的感覺真好啦,我在我們上完課后練習啦一遍,然后感覺對著電腦時間太長啦,于是就出去飄啦一圈休息一下下啦。學習固然重要,但是疲勞的感覺好難受啦,我們還是要學會調節下自己的狀態啦,當然,以后工作啦也會是這樣的吧,當有項目工作啦,我們就忘記啦休息吧,或許那時間正在焦慮的思考怎么結局問題忘記啦休息,嘿嘿,一定要記得給自己片刻停留的氣息,讓自己心晴氣爽啦。好啦,下面總結下我今天學習的數據庫系統操作的高級語言啦。
?????????? 一.局部變量與全局變量的定義
?????????? 我們之前已經學習了C#的基礎,那么在Sql中局部變量的定義和C#中是一樣的啦,只是定義的格式區別很大啦,下面就寫下Sql中局部變量的定義啦
--局部變量 declare @str char(20) --定義參數 select @str ='hello word' --用select給參數賦值 set @str ='hell' --或者用set給參數賦值 print @str --輸出@str的值--全局變量 print @@version --全局變量一般我們使用系統里面給我們寫好的??????????????????
??????????????????? 二.流程控制語句的使用
????????????? 1.條件判斷語句(if...else...)
--條件判斷語句(if...else...) --某地到青島的郵政里程為1043,通過郵政局向青島城區交 "特征專遞"郵件, 24小時之內達到。計費每克0.12元 --但超過100克,超過數量每克0.05元,算出郵費declare @a real, @b intset @a=120 if @a <100begin --begin end 相當于C#中的花括號的使用方法set @b=@a*0.12endelsebeginset @b=0.12*100+(@a-100)*0.05endprint'距離是'++cast(@a as varchar(50))print '郵費是'+cast(@b as varchar(50))--定義一個用戶名和密碼,判斷是否正確 declare @user varchar(20),@pwd varchar(20) select @user ='admin',@pwd ='admin' if @user='zhangsan' begin if @pwd='123'print '用戶名或者密碼不正確,請重新輸入'elseprint '密碼錯誤' end else if @user='lisi' beginif @pwd='123'print '用戶名或者密碼不正確,請重新輸入'elseprint '密碼錯誤' end else if @user='admin' beginif @pwd='admin'print '輸入正確'elseprint '密碼錯誤' end else beginset @pwd='密碼不正確' end print @pwd?????????????????
? ? ? ? ? ? ? ? ? ? ? 2.while表達式
--while 條件表達式--begin--命令行或程序--end--求1-100的和declare @x int,@sum intselect @x=1,@sum=0while @x>0 and @x<=100beginset @sum=@sum+@xset @x=@x+1endprint '1-100的和:'+cast(@sum as varchar(20))??????????????????
? ? ? ? ? ? ? ? ? ? ? 3.關鍵字break的使用
--break的用法declare @i int,@sum0 intselect @i=1,@sum0=0while @i>0 and @i<=100beginset @sum0=@sum0+@iset @i=@i+1if @i>50 --當i>50時間跳出當前循環breakendprint '1-50的和:'+cast(@sum0 as varchar(20))?????????????????
? ? ? ? ? ? ? ? ? ? ? 4.關鍵字continue的使用方法
--continue的用法declare @num int,@sum1 intselect @num=1,@sum1=0while @num>0 and @num<=100beginset @num =@num+1if @num%2=1 continueelseset @sum1=@sum1+@numendprint '1-100間的偶數的和:'+cast(@sum1 as varchar(20))???????????
?????????????????? ? 5.case語句的使用
--case語句--case [表達式]-- when 條件表達式1 then 結果1-- when 條件表達式2 then 結果2-- ........-- else-- 結果表達式n-- end--輸入學生的成績 1-100之間,否則就會提示"您輸入的成績不對,成績應該在0-100之間" --成績 評語 --90-100 優秀 --80-90 優良 --70-80 中等 --60-70 及格 --60分一下 不及格declare @score float,@str varchar(50) select @score=53,@str= case when @score>100 or @score<0 then'您輸入的成績格式不對'when @score>=60 and @score<70 then '及格'when @score>=70 and @score<80 then '中等'when @score>=80 and @score<90 then '優良'when @score>=90 and @score<100 then '優秀'else'不及格' end print '評語是:'+@str--查詢不同倉庫的平均工資 select *,不同倉庫的平均工資= casewhen 倉庫號='wh1' then(select AVG(工資) from 職工 where 倉庫號='wh1') --在then后面是查詢出平均工資when 倉庫號='wh2' then(select AVG(工資) from 職工 where 倉庫號='wh2') --即在通過倉庫號查詢各個倉庫號的平均工資when 倉庫號='wh3' then(select AVG(工資) from 職工 where 倉庫號='wh3')when 倉庫號='wh4' then(select AVG(工資) from 職工 where 倉庫號='wh4') end from 職工?
? ? ? ? ? ? ? ? ?? 三.視圖的創建刪除以及修改
create view v1 as select 倉庫號,城市,面積 from 倉庫create view v2 as select 姓名,工資 from 職工 where 工資>1800create view v3 as select 倉庫.倉庫號,倉庫.城市,倉庫.面積,倉庫.創建時間,職工.姓名,職工.性別,職工.工資 from 倉庫,職工 where 倉庫.倉庫號=職工.倉庫號alter view v2 --修改視圖 as select 倉庫.倉庫號,城市,面積 from 倉庫drop view v3 --刪除視圖update test set 面積=面積+88 where 倉庫號='wh1' --修改視圖delete test where 倉庫號='wh1'?
????????????????? ?? 四.存儲過程的操作
--存貯過程 --create procedure name --創建存儲過程 --@parms.... --as --begin --end---execute name 參數1 參數2 --執行存儲過程 --1. create procedure proc_sql as begin declare @i int set @i=0; while(@i<10)beginprint char(ascii('a')+@i)+'的ascii碼'+cast(ascii('a')+@i as varchar(50))set @i=@i+1end end excute proc_sql--2. --無參數的存貯過程執行 create procedure proc2 as beginselect * from 職工 where 工資>2000 endexecute proc2--有參數的存貯過程 --3.求三個數中的最大值 create procedure proc4 @x1 int,@x2 int,@x3 int as begin declare @max int if @x1>@x2set @max=@x1 elseset @max=@x2 if @x3>@maxset @max=@x3print '3個數字中的最大值是'+cast(@max as varchar(50)) end execute proc4 15,18,39--3. create procedure sq7 @mingz int,@maxgz int as select * from 職工 where 工資 between @mingz and @maxgz execute sq7 1500,20004. create procedure sq8 @cangkuhao varchar(50), @maxgz int output, @avggz real output as begin select * from 職工 where 倉庫號=@cangkuhao select @maxgz=MAX(工資)from 職工 where 倉庫號=@cangkuhao select @avggz=AVG(工資) from 職工 where 倉庫號=@cangkuhao enddeclare @x1 int,@x2 real execute sq8 'wh2',@x1 output,@x2 output select @x1 as wh2職工最大工資,@x2 as wh2職工平均工資execute sp_rename sq7,rocky --重命名?
?????????????????? ? ?五.觸發器
--觸發器是一種特殊的存貯過程,他就相當于c#中的事件觸發器主要是通過事件觸發而被執行的 --create trigger 觸發器名稱 on 表 for insert[update,delete] as -- begin --程序塊 --end create trigger rockyR on 倉庫 for update --創建 as begininsert into 倉庫(倉庫號,城市,面積,創建時間) values('wh01','鄭州',1800,'2014-12-12'),('wh02','北京',1700,'2014-12-13'),('wh03','上海',1600,'2014-12-15') endupdate 倉庫 set 面積=面積-10 where 倉庫號='wh2' --修改create trigger rk on 倉庫 for insert,update,delete as beginexecute xp_sendmail '123456@qq.com' end--alter trigger rockyR on 表名 for insert[update,delete] --修改觸發器drop trigger rk --刪除觸發器?
?????????????? ?? ?? 六.索引的操作
????????????????? ?? 在sql中,講SQL語句組成一個事務,其目的是保證這一組sql語句能夠得到可靠地執行,如果系統中出現了錯誤阻礙sql的執行,并且只要其中任何一條語句出現錯誤,事務中所有的sql語句都不會被執行,即要么全部sql執行,要么全不執行,即創建索引的格式如下:
create unique index unique_index on 倉庫(倉庫號)?
???????????????? ? ? ?好啦今天就總結到這里啦,嘿嘿,今天的比較多吧,但是還是要好好練習的啦,嘿嘿。
總結
以上是生活随笔為你收集整理的今天学习啦所谓的高级语言啦的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 消息称英特尔决定取消 Meteor La
- 下一篇: 全球首个十万吨级甲醇工厂在安阳投产:吉利