这篇文章是接上一篇《如何编写程序使得数据库服务器变化时代码不需要重要编译》而写的,本来该写在上一篇中,但是因为这一部分很重要,所以决定单独写出来了。
在创建数据库时,经常要判断服务器中是否存在某个数据库,然后再决定是不是要进行下一步操作。
以下是一些简单地示例,希望会有用。
1: --判断数据库是否存在'SkyBusiness')begindatabase SkyBusiness'SkyBusiness 已经存在,已被删除'endelsebegindatabase SkyBusinessprimary11: (12: name=SkyBusiness_mdf,'c:\SkyBusiness.mdf',size=10mb,15: maxsize=50mb,16: filegrowth=25%17: )on19: (20: name=SkyBusiness_ldf,'c:\SkyBusiness.ldf',size=10mb,23: maxsize=50mb,24: filegrowth=25%25: )'SkyBusiness数据库创建成功'end28:29: --建聚集索引index index_yexinwinnerson tablename(column_name)fillfactor = 1033:34: --建非聚集索引index index_yexinwinnerson tablename(column_name)fillfactor = 1038:39: --建视图view view_nameasfrom pubs.titles(sql语句,任意)43:44: --建检查视图view view_nameasfrom pubs.titles(sql语句,任意)option49:50: --建加密视图view view_namewith encryptionasfrom pubs.titles(sql语句,任意)55:56: --建表table tablename58: ( ---(字段名,字段类型 自己任意)key,null,null62: )63:64: --添加check约束table tablenameand stuAge <100)67:68: --添加外键table tablenamekey (stuName)references Xtablename(stuName)72:73: --添加唯一约束table tablenameunique(stuID)76:77: ---建事务begin tranactionwhere ID = 1where ID = 3if(@@error <> 0)begin'转帐失败'transactionendelsebegin'转帐成功'transactionend91:92: --建游标Cursorfrom northwind.product95:96: --建更新游标Cursorfrom northwind.productupdate100:101: --建随意移动游标scrollfrom northwind.product104:105: --使用游标open cursor_name107:108: --关闭游标close cursor_name110:111: --删除游标deallocate cursor_name113:114: --移动游标115:-- 下一条记录last --最后一条记录first --第一条记录prior --上一条记录ABSOLUTE n -- 绝对位置
相关文章: