SQL Server 資料庫編程

來源:互聯網
上載者:User

標籤:

一、變數的定義與賦值

declare @變數名 類型

set @變數名=值

eg、

   查汽車表中名稱含有寶馬兩個字的
declare @name varchar(20)
set @name=‘寶馬‘
select * from car where Name like ‘%‘[email protected]+‘%‘

   查汽車表中所有汽車的平均值並輸出
declare @price decimal(10,4)
select @price = AVG(Price) from Car
print ‘所有汽車的平均價格為:‘+cast(@price as varchar(20))

二、語句

1、輸出語句  print ‘字串‘

2、分支語句

a、if else 

if 判斷條件
begin 
  要執行的語句
end
else
begin
  要執行的語句
end

b、case  語句(相當於C#的swtich case)(兩種用法)

1.簡單Case函數

CASE sex

         WHEN ‘1‘ THEN ‘男‘

         WHEN ‘2‘ THEN ‘女‘

ELSE

  ‘其他‘

END

2.Case搜尋函數

CASE

   WHEN sex = ‘1‘ THEN ‘男‘

     WHEN sex = ‘2‘ THEN ‘女‘

ELSE

  ‘其他‘

END

eg1、

declare @ccname varchar(20)
set @ccname = ‘寶馬‘
select * from Car where Name like 
case
  when @ccname=‘寶馬‘ then ‘%寶馬%‘
  when @ccname=‘奧迪‘ then ‘%奧迪%‘
else ‘%‘
end

1).兩者相比,Case搜尋函數功能更強。

2).Case函數類似於if……else if 語句,只返回第一個合格值,之後的部分會被忽略

注意:語句結束之後不要寫分號或逗號

三、預存程序

1、不帶參數的預存程序(跟 視圖作用相同)

eg

1 create procedure MyPRO --procedure為建立預存程序關鍵字,也可以簡寫proc,MyPRO為預存程序名2 as3 select * from authors4 --執行預存程序5 exec mypro  --exec表示執行預存程序

2、帶參數的預存程序

create procedure BuyFriut@Uid varchar(50),@Fcode varchar(50),@Sl intasbegin    declare @kc int,@price float    select @kc=Numbers,@price=Price from Fruit where Ids=@Fcode    if @Sl>@kc    begin        print ‘庫存不足!‘    end    else    begin        declare @ye float        select @ye=Account from Login where UserName=@Uid        if @ye>= @price*@Sl        begin            update Fruit set Numbers=Numbers-@Sl where Ids=@Fcode            update Login set Account=Account-@price*@Sl where UserName=@Uid            declare @sj int            set @sj = cast(rand()*10000    as int)                            insert into Orders values(@sj,@Uid,GETDATE())--新增內容的時候 先寫主鍵表,再寫外鍵表            insert into OrderDetails values(@sj,@Fcode,@Sl)                end        else        begin            print ‘餘額不足!‘                    end    end    endgo--使用預存程序declare @s intexec  @s = BuyFriut ‘wangwu‘,‘k001‘,20print @s --刪除預存程序drop proc BuyFriut

3、觸發器

     觸發器,是一種特殊的預存程序,可以用來對錶實施複雜的完整性條件約束,保持資料的一致性。當觸發器所保護的資料發生改變時,觸發器會自動被啟用,並執行觸發器中所定義的相關操作,從而保證對資料的不完整性條件約束或不正確的修改。

    用到的兩個表inserted表和deleted表,暫存資料表,作用是為了恢複資料

eg

select * from Loginone
select * from biandong
go
--建立觸發器
create trigger TR_LOGINONE_DELETE
on Loginone
a、for delete 觸發器 --刪除的時候執行(第一類)

eg

asbegindeclare @uid varchar(50),@name varchar(50)select @uid=UserName,@name=Name from deletedinsert into biandong values(@uid,@name,‘刪除‘)endgo--在對錶loginnoe進行刪除的時候觸發器執行delete from Loginone where UserName=‘aaa‘go

b、instead of delete 觸發器 --替代執行(第三類)

--刪除student裡面的資料的時候用另外兩條語句替代,先刪從表再刪主表create trigger TR_STUDENT_DELETEon Studentinstead of deleteasbegin    declare @sno varchar(20)    select @sno=sno from deleted    delete from score where sno=@sno    delete from student where sno=@sno    end--執行刪除的時候觸發delete from student where sno=‘101‘

c、after delete --刪除之後執行(第二類)

4、事務

a、開始事務 begin tran(transaction)
b、提交 commit
c、復原 rollback

select * from Carselect * from Brandbegin transaction --開始事務delete from Car where Code=‘c005‘delete from Brand where Brand_Code=‘b002‘ if @@ERROR=0--判斷是否出錯,如果執行沒錯@@ERROR為0begin    commit -- 提交事務endelsebegin    rollback--復原事務end--try catchbegin transaction --開始事務begin try --try裡面寫要執行的代碼    delete from Car where Code=‘c005‘    delete from Brand where Brand_Code=‘b002‘    commitend trybegin catch    rollbackend catch

 

SQL Server 資料庫編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.