zoukankan      html  css  js  c++  java
  • sql server如何判断数据库是否存在

    如何判断数据库是否存在       执行下列的SQL,获得一张表,根据表的行数来判断。

    select * from master..sysdatabases where name=N'所查询的数据库名'

    if exists(select * from master.dbo.sysdatabases where name = 'yexinwinners')
    begin
    drop database yexinwinners
    print 'yexinwinners己存在,己被删除'
    end
    else
    begin
    create database yexinwinners
    on primary
    (
    name = yexinwinners_mdf,
    filename = 'c:yexinwinners.mdf',
    size = 10mb,
    maxsize = 50mb,
    filegrowth = 25%
    )
    log on
    (
    name = yexinwinners_ldf,
    filename = 'c:yexinwinners.ldf',
    size = 10mb,
    maxsize = 50mb,
    filegrowth = 25%
    )
    print 'yexinwinners 数据库创建成功'

    end

    --建聚集索引
    create clustered index index_yexinwinners
    on tablename(column_name)
    with fillfactor = 10
    --建非聚集索引
    create nonclustered index index_yexinwinners
    on tablename(column_name)
    with fillfactor = 10
    --建视图
    create view view_name
    as
    select * from pubs.titles(sql语句,任意)
    --建检查视图
    create view view_name
    as
    select * from pubs.titles(sql语句,任意)
    with check option
    --建加密视图
    create view view_name
    with encryption
    as
    select * from pubs.titles(sql语句,任意)

    --建表

    create table tablename
    ( ---(字段名,字段类型 自己任意)
    stuID int not null identity(1,1) primary key,
    stuName varchar(30) not null,
    stuAge char(3) not null
    )
    --添加check约束
    alter table tablename
    add check(stuAge >0 and stuAge <100)
    --添加外键
    alter table tablename
    add foreign key (stuName)
    references Xtablename(stuName)
    --添加唯一约束
    alter table tablename
    add unique(stuID) ---建事务
    begin tranaction
    update Xtable set Money = Money + 10000 where ID = 1
    update Xtable set Money = Money - 10000 where ID = 3
    if(@@error <> 0)
    begin
    print '转帐失败'
    rollback transaction
    end
    else
    begin
    print '转帐成功'
    commit transaction
    end

    --建游标
    declare cursor_name Cursor
    for select * from northwind.product
    --建更新游标
    declare cursor_name Cursor
    for select * from northwind.product
    for update
    --建随意移动游标
    declare cursor_name Cursor scroll
    for select * from northwind.product
    --使用游标
    open cursor_name
    --关闭游标
    close cursor_name
    --删除游标
    deallocate cursor_name
    --移动游标
    fetch next -- 下一条记录
    fetch last --最后一条记录
    fetch first --第一条记录
    fetch prior --上一条记录
    fetch ABSOLUTE n -- 绝对位置 name = yexinwinners
    EMail = yexin@cncmax.tj.cn
    blog = blog.sina.com.cn/yexins

    QQ = 306677309



    SQL Server中判断数据库是否存在:
    select * From master.dbo.sysdatabases where name='pubs'

    最初安装 SQL Server 时,sysdatabases 包含 master、model、msdb、mssqlweb 和 tempdb 数据库的项。该表只存储在 master 数据库中。

    但在实际使用中,需判断Status状态位:
    其中某些状态位可由用户使用 sp_dboption(read only、dbo use only、single user 等)进行设置: 
    1 = autoclose;使用 sp_dboption 设置。 数据库完全关闭,其资源在最后一个用户注销后释放。
    4 = select into/bulkcopy;使用 sp_dboption 设置。允许使用 Select INTO 语句和快速大容量复制。
    8 = trunc. log on chkpt;使用 sp_dboption 设置。如果数据库处于日志截断模式,则检查点将截断日志中非活动的部分。只能为 master 数据库设置此选项。16 = torn page detection,使用 sp_dboption 设置。可以检测残缺页。
    32 = loading。
    64 = pre recovery。
    128 = recovering。
    256 = not recovered。
    512 = offline;使用sp_dboption 设置。数据库将处于脱机状态。
    1024 = read only;使用 sp_dboption 设置。用户仅能读取数据库中的数据而无法对其进行修改。
    2048 = dbo use only;使用sp_dboption 设置。只有数据库所有者可以使用数据库。
    4096 = single user;使用 sp_dboption 设置。每次只能有一个用户访问数据库。
    32768 = emergency mode。
    4194304 = autoshrink。
    1073741824 = cleanly shutdown。
    可以同时打开多个位。

    譬如:判断一个数据库是否offline
    select * From master.dbo.sysdatabases where name='pubs' and status<>512

    SQL Server中判断表对象是否存在:
    select count(*) from sysobjects where id = object_id('数据库名.Owner.表名')

    if exists 
    (select count(*) from sysobjects where id = object_id('数据库名.Owner.表名'))
    print '存在'
    else
    print '不存在'

    SQL Server中判断表中字段是否存在:
    if exists(select * from syscolumns where name='colname1' and id=object_id('数据库名.Owner.表名'))
    print '存在'
    else
    print '不存在'
    代表表tablename1中存在colname1字段 
    例:
    select * from syscolumns where name='Test' and id=object_id('dbo.test')

    Access中判断表对象是否存在:
    其实,Access数据库也有系统表,存放有对象名
    Select Count(*) AS Qty FROM MSysObjects Where ((MSysObjects.Name) Like '表名');

    判断数据库和表是否存在  
     if not exists(select 1 From master.dbo.sysdatabases where name=N'JZKStarCfg')

    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    「网络流 24 题」太空飞行计划
    Wannafly挑战赛2D Delete (最短路好题)
    牛客 216 C 小K的疑惑
    Till I Collapse CodeForces
    bzoj 2734 集合悬殊 (状压dp)
    图写成一个类(2)
    写程序的易错点(不定期更新)
    强联通分量之kosaraju算法
    对各种lca算法的理解
    pb_ds的优先队列实现dijkstra
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834792.html
Copyright © 2011-2022 走看看