posts - 4,  comments - 13,  trackbacks - 0


          /**
            *刪除數(shù)據(jù)庫中已經(jīng)存在的表
           **/
          if exists(select * from sys.objects where name='tb_grade')
          drop table tb_grade--刪除tb_grade表
          go
          if exists(select * from sys.objects where name='tb_student')
          drop table tb_student--刪除tb_grade表
          go

          /**
            *創(chuàng)建數(shù)據(jù)表
           **/
          create table tb_student--創(chuàng)建tb_student
          (
             student_id int identity(1,1),--學生編號(主鍵,自動增長)
             student_name nvarchar(30) not null,--學生姓名(不能為空)  
             student_sex char(10) not null,--學生性別(不能為空)
             student_age int default(18),--學生年齡(默認為18)
             constraint pk_student_id primary key (student_id)
          )
          go
          create table tb_grade--創(chuàng)建tb_grade
          (
            grade_id int identity(1,1),--成績編號(主鍵,自動增長1)
            student_id int,--外鍵(引用學生表student_id)
            english float,--英語成績
            math float,--數(shù)學成績
            constraint pk_grade_id primary key (grade_id),--為表tb_student創(chuàng)建主鍵
            constraint fk_student_id foreign key(student_id) references tb_student(student_id)--創(chuàng)建外鍵關系
          )
           
          select * from tb_student
          select * from  tb_grade

          /**
            *創(chuàng)建視圖
           **/

          /**
           *創(chuàng)建視圖的語法
           *
           CREATE VIEW  view_name
           AS
           select_statement
          *
          *
          */

          if exists (select * from sys.objects where name='vw_student')
          drop view vw_student --刪除已經(jīng)存在的vw_student視圖
          go
          create view vw_student--創(chuàng)建視圖vw_student
          as
          select * from tb_student inner join tb_grade on tb_student.student_id=tb_grade.student_id
          go

          /**
            *創(chuàng)建索引
           **/

          /**
           *創(chuàng)建索引的語法
           *
          CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name
              ON { table | view } ( column [ ASC | DESC ] [ ,...n ] )
          [ WITH < index_option > [ ,...n] ]
          [ ON filegroup ]

          < index_option > ::=
              { PAD_INDEX |
                  FILLFACTOR = fillfactor |
                  IGNORE_DUP_KEY |
                  DROP_EXISTING |
              STATISTICS_NORECOMPUTE |
              SORT_IN_TEMPDB 
          }
          *
          *
          */

          if exists (select * from sys.indexes where name='index_student_id')
          drop index index_student_id on tb_student --刪除tb_student中的索引student_id
          --創(chuàng)建索引
          create index index_student_id on tb_student(student_id)
          go

          /**
            *創(chuàng)建存儲過程
           **/

          /**
           *
           *創(chuàng)建存儲過程的語法
           *
          CREATE PROC [ EDURE ] procedure_name [ ; number ]
              [ { @parameter data_type }
                  [ VARYING ] [ = default ] [ OUTPUT ]
              ] [ ,...n ]

          [ WITH
              { RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]

          [ FOR REPLICATION ]

          AS sql_statement [ ...n ]
          *
          */
          --不帶參數(shù)的存儲過程
          if exists (select * from  sys.objects where name='find_student')
          drop procedure find_student
          go
          create procedure find_student
          as
          select * from tb_student
          go
          ---調(diào)用存儲過程
          exec find_student
          go
          --帶輸入?yún)?shù)的存儲過程
          if exists (select * from  sys.objects where name='add_student_grade')
          drop procedure add_student_grade
          go
          create procedure add_student_grade
          --定義輸入?yún)?shù)
          @studentName nvarchar(30),
          @studentSex char(10),
          @studentAge int=18,
          @gradeEnglish float,
          @gradeMath float
          as
              declare @studentId int
              declare @n int
              set @n=0
              begin transaction tran_add --開啟事務
              insert into tb_student(student_name,student_sex,student_age) values(@studentName,@studentSex,@studentAge)
              set @n=@@error   
           select @studentId=max(student_id) from tb_student
           set @n=@@error 
              insert into tb_grade (student_id,english,math) values(@studentId,@gradeEnglish,@gradeMath)
           set @n=@@error 
              if(@n<>0)
                 begin
                     rollback transaction tran_add --回滾事務
                 end
              else
                 begin
               commit transaction tran_add  --提交事務
                 end
          go
          ---調(diào)用存儲過程
          exec add_student_grade '張三','男',20,80,88
          go
          --帶輸出參數(shù)的存儲過程
          if exists (select * from  sys.objects where name='getCount')
          drop procedure getCount
          go
          create procedure getCount
          @n int output
          as
          select @n=count(*) from tb_student
          go
          --調(diào)用帶輸出參數(shù)的存儲過程
          declare @n int
          execute getCount @n output
          select @n
            
          /**
           *創(chuàng)建觸發(fā)器
           **/
          /**
           *
           *創(chuàng)建觸發(fā)器的語法
           *
           *
          CREATE TRIGGER trigger_name
          ON { table | view }
          [ WITH ENCRYPTION ]
          {
              { { FOR | AFTER | INSTEAD OF } { [ INSERT ] [ , ] [ UPDATE ] }
                  [ WITH APPEND ]
                  [ NOT FOR REPLICATION ]
                  AS
                  [ { IF UPDATE ( column )
                      [ { AND | OR } UPDATE ( column ) ]
                          [ ...n ]
                  | IF ( COLUMNS_UPDATED ( ) { bitwise_operator } updated_bitmask )
                          { comparison_operator } column_bitmask [ ...n ]
                  } ]
                  sql_statement [ ...n ]
              }
          }
          *
          *
          **/

          IF EXISTS (SELECT name FROM sysobjects
          WHERE name = 'reminder' AND type = 'TR')
          DROP TRIGGER reminder --刪除觸發(fā)器reminder
          GO
          --創(chuàng)建觸發(fā)器reminder(如果對表tb_student進行添加和更新信息時出發(fā))
          CREATE TRIGGER reminder
          ON tb_student
          FOR INSERT, UPDATE
          AS
          RAISERROR (50009, 16, 10)
          GO
          ---創(chuàng)建DELETE觸發(fā)器
          IF EXISTS (SELECT name FROM sysobjects
          WHERE name = 'sendemail' AND type = 'TR')
          DROP TRIGGER sendemail--刪除觸發(fā)器sendemail
          GO
          --創(chuàng)建觸發(fā)器
          CREATE TRIGGER sendemail
          ON tb_grade
          FOR DELETE
          AS
          EXEC master..xp_sendmail' MaBin',
          'Don''t forget to print a report for the distributors.'
          GO

          posted on 2009-11-24 17:54 FOG 閱讀(1110) 評論(1)  編輯  收藏


          FeedBack:
          # re: SQLSERVER DLL的基本操作
          2009-11-24 17:57 | 雪山飛鵠
          雄起  回復  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          <2009年11月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿

          隨筆檔案

          MY LINK

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 伊金霍洛旗| 东明县| 屏山县| 女性| 钟山县| 东兴市| 宜良县| 顺平县| 洛宁县| 怀仁县| 长宁区| 格尔木市| 哈密市| 土默特右旗| 永寿县| 泰州市| 精河县| 攀枝花市| 南丹县| 治县。| 大邑县| 汉寿县| 建宁县| 林西县| 体育| 雷山县| 永福县| 上虞市| 翼城县| 临沧市| 颍上县| 抚顺县| 大方县| 高密市| 南投市| 松阳县| 岳池县| 平塘县| 广南县| 凭祥市| 左贡县|