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

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

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

1: --判断数据库是否存在
'SkyBusiness')
begin
database SkyBusiness
'SkyBusiness 已经存在,已被删除'
end
else
begin 
database SkyBusiness
primary
  11:         (
  12:           name=SkyBusiness_mdf,
'c:\SkyBusiness.mdf',
size=10mb,
  15:           maxsize=50mb,
  16:           filegrowth=25%
  17:         )
on
  19:         (
  20:           name=SkyBusiness_ldf,
'c:\SkyBusiness.ldf',
size=10mb,
  23:           maxsize=50mb,
  24:           filegrowth=25%
  25:         )
'SkyBusiness数据库创建成功'
end
  28:  
  29: --建聚集索引
index index_yexinwinners
on tablename(column_name)
fillfactor = 10
  33:  
  34: --建非聚集索引
index index_yexinwinners
on tablename(column_name)
fillfactor = 10
  38:  
  39: --建视图
view view_name
as
from pubs.titles(sql语句,任意)
  43:  
  44: --建检查视图
view view_name
as
from pubs.titles(sql语句,任意)
option
  49:  
  50: --建加密视图
view view_name
with encryption
as
from pubs.titles(sql语句,任意)
  55:  
  56: --建表
table tablename
  58: ( ---(字段名,字段类型 自己任意)
key,
null,
null
  62: )
  63:  
  64: --添加check约束
table tablename
and stuAge <100)
  67:  
  68: --添加外键
table tablename
key (stuName)
references Xtablename(stuName)
  72:  
  73: --添加唯一约束
table tablename
unique(stuID)
  76:  
  77: ---建事务
begin tranaction
where ID = 1
where ID = 3
if(@@error <> 0)
begin
'转帐失败'
transaction
end
else
begin
'转帐成功'
transaction
end
  91:  
  92: --建游标
Cursor
from northwind.product
  95:  
  96: --建更新游标
Cursor
from northwind.product
update
 100:  
 101: --建随意移动游标
scroll
from northwind.product
 104:  
 105: --使用游标
open cursor_name
 107:  
 108: --关闭游标
close cursor_name
 110:  
 111: --删除游标
deallocate cursor_name
 113:  
 114: --移动游标
 115:  
-- 下一条记录
last --最后一条记录
first --第一条记录
prior --上一条记录
ABSOLUTE n -- 绝对位置

相关文章: