如何判断数据库是否存在

      这篇文章是接上一篇《如何编写程序使得数据库服务器变化时代码不需要重要编译》而写的,本来该写在上一篇中,但是因为这一部分很重要,所以决定单独写出来了。

      在创建数据库时,经常要判断服务器中是否存在某个数据库,然后再决定是不是要进行下一步操作。

      以下是一些简单地示例,希望会有用。

   1: --判断数据库是否存在
   2: if exists(select * from master.dbo.sysdatabases where name = 'SkyBusiness')
   3:     begin
   4:         drop database SkyBusiness
   5:         print 'SkyBusiness 已经存在,已被删除'
   6:     end
   7: else
   8:     begin 
   9:         create database SkyBusiness
  10:         on primary
  11:         (
  12:           name=SkyBusiness_mdf,
  13:           filename='c:\SkyBusiness.mdf',
  14:           size=10mb,
  15:           maxsize=50mb,
  16:           filegrowth=25%
  17:         )
  18:         log on
  19:         (
  20:           name=SkyBusiness_ldf,
  21:           filename='c:\SkyBusiness.ldf',
  22:           size=10mb,
  23:           maxsize=50mb,
  24:           filegrowth=25%
  25:         )
  26:         print 'SkyBusiness数据库创建成功'
  27:     end
  28:  
  29: --建聚集索引
  30: create clustered index index_yexinwinners
  31: on tablename(column_name)
  32: with fillfactor = 10
  33:  
  34: --建非聚集索引
  35: create nonclustered index index_yexinwinners
  36: on tablename(column_name)
  37: with fillfactor = 10
  38:  
  39: --建视图
  40: create view view_name
  41: as
  42: select * from pubs.titles(sql语句,任意)
  43:  
  44: --建检查视图
  45: create view view_name
  46: as
  47: select * from pubs.titles(sql语句,任意)
  48: with check option
  49:  
  50: --建加密视图
  51: create view view_name
  52: with encryption
  53: as
  54: select * from pubs.titles(sql语句,任意)
  55:  
  56: --建表
  57: create table tablename
  58: ( ---(字段名,字段类型 自己任意)
  59: stuID int not null identity(1,1) primary key,
  60: stuName varchar(30) not null,
  61: stuAge char(3) not null
  62: )
  63:  
  64: --添加check约束
  65: alter table tablename
  66: add check(stuAge >0 and stuAge <100)
  67:  
  68: --添加外键
  69: alter table tablename
  70: add foreign key (stuName)
  71: references Xtablename(stuName)
  72:  
  73: --添加唯一约束
  74: alter table tablename
  75: add unique(stuID)
  76:  
  77: ---建事务
  78: begin tranaction
  79:     update Xtable set Money = Money + 10000 where ID = 1
  80:     update Xtable set Money = Money - 10000 where ID = 3
  81:     if(@@error <> 0)
  82:     begin
  83:     print '转帐失败'
  84:     rollback transaction
  85:     end
  86:     else
  87:     begin
  88:     print '转帐成功'
  89:     commit transaction
  90: end
  91:  
  92: --建游标
  93: declare cursor_name Cursor
  94: for select * from northwind.product
  95:  
  96: --建更新游标
  97: declare cursor_name Cursor
  98: for select * from northwind.product
  99: for update
 100:  
 101: --建随意移动游标
 102: declare cursor_name Cursor scroll
 103: for select * from northwind.product
 104:  
 105: --使用游标
 106: open cursor_name
 107:  
 108: --关闭游标
 109: close cursor_name
 110:  
 111: --删除游标
 112: deallocate cursor_name
 113:  
 114: --移动游标
 115:  
 116: fetch next -- 下一条记录
 117: fetch last --最后一条记录
 118: fetch first --第一条记录
 119: fetch prior --上一条记录
 120: fetch ABSOLUTE n -- 绝对位置
posted @ 2009-10-22 00:24  陆止于此 海始于斯  阅读(2533)  评论(2编辑  收藏  举报