zoukankan      html  css  js  c++  java
  • sql server 删除所有表及所有存储过程、所有视图和递归查询、数字类型转为字符串

    1、删除所有表

    
    
    DECLARE c1 cursor for
    select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
    from sysobjects
    where xtype = 'F'
    open c1
    declare @c1 varchar(8000)
    fetch next from c1 into @c1
    while(@@fetch_status=0)
    begin
    exec(@c1)
    fetch next from c1 into @c1
    end
    close c1
    deallocate c1
     
    declare @sql varchar(8000)
    while (select count(*) from sysobjects where type='U')>0
    begin
    SELECT @sql='drop table ' + name
    FROM sysobjects
    WHERE (type = 'U')
    ORDER BY 'drop table ' + name
    exec(@sql)
    end

    2、删除所有存储过程

    declare @tname varchar(8000)
    set @tname=''
    select @tname=@tname + Name + ',' from sysobjects where xtype='P'
    select @tname='drop proc ' + left(@tname,len(@tname)-1)
    exec(@tname)
    go

    3、删除所有视图

    declare @tname varchar(8000)
    set @tname=''
    select @tname=@tname + Name + ',' from sysobjects where xtype='V'
    select @tname='drop view ' + left(@tname,len(@tname)-1)
    exec(@tname)
    go

    4、递归查询 使用关键字with as

     with temp ( [Id], [parentid])
        as
        (
            select Id, ParentId
            from SysLocation
            where ParentId = @ParentId
            union all
            select a.Id, a.ParentId
            from SysLocation a
            inner join temp on a.ParentId = temp.[Id]        
        )
        
        select s.Id, s.ParentId from SysLocation s where Id=@ParentId
        union all
        select * from temp

    5、数字类型转为字符串

    select convert(varchar(11),convert(decimal(11,0),mo)) as m  from test

     

    6、创建外键

    alter table ResolvedOrderNew 
    add constraint FK_ResolvedOrderNew_QuotaOrderNew
    foreign key (QuoteOrderNewId) references QuoteOrderNew(Id);
  • 相关阅读:
    Lucene教程
    ElasticSearch安装
    MySQL事务
    Java泛型
    Python学习笔记(1)
    @keyframs实现图片gif效果
    glup简单应用---gulpfile.js
    巧用CSS3伪类选择器自定义checkbox和radio的样式
    get传中文参数乱码解决方法
    自定义样式 实现文件控件input[type='file']
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/10734651.html
Copyright © 2011-2022 走看看