zoukankan      html  css  js  c++  java
  • sql 语句——功能实现【持续更新】

    最近做项目用到一些数据操作,为了防止自己忘记,同时也供大家参考,特地将项目过程中写的sql语句发出来。

    1.清空数据库中数据且不破坏表的结构(因为有表约束所以不能用:TRUNCATE TABLE 表名)

    版本一:declare  cursor_alltablename cursor scroll dynamic for select name from sysobjects where xtype = 'U' and (not name like 'sys%') order by name desc;

    declare @tablename varchar(50);
    declare @strSql varchar(100);
    open cursor_alltablename;
    fetch next from cursor_alltablename into @tablename;
    while (@@FETCH_STATUS = 0)
    begin
     set @strSql='alter table ['+@tablename + '] nocheck constraint all'; --这里注意nocheck 和‘]’之间是有空格的,还有最好给@tablename加上'[]'
     exec (@strSql)
     fetch next from cursor_alltablename into @tablename
    end

    fetch first from cursor_alltablename into @tablename    --移到数据集的首部
    while (@@FETCH_STATUS = 0)
    begin
     set @strSql='delete ['+@tablename+']'
     print @strSql
     exec (@strSql)
     fetch next from cursor_alltablename into @tablename
    end
    fetch first from cursor_alltablename into @tablename
    while (@@FETCH_STATUS = 0)
    begin
     set @strSql='alter table ['+@tablename +'] check constraint all'
     exec (@strSql)
     fetch next from cursor_alltablename into @tablename
    end
    close cursor_alltablename
    deallocate cursor_alltablename

    改进版:

    declare  cursor_alltablename cursor scroll dynamic for select name from sysobjects where xtype = 'U' and (not name like 'sys%') order by name desc;

    declare @tablename nvarchar(50);
    declare @strSql nvarchar(500);
    declare @i int=1
    open cursor_alltablename;
    fetch next from cursor_alltablename into @tablename;
    while (@@FETCH_STATUS = 0)
    begin
     set @strSql=N'alter table ['+@tablename + N'] nocheck constraint all';
     exec (@strSql)
     set @i=@i+1
     fetch next from cursor_alltablename into @tablename
    end

    fetch last from cursor_alltablename into @tablename
    while (@i > 0)
    begin
     set @strSql=N'delete ['+@tablename+N']'
     print @strSql
     exec (@strSql)
     set @i = @i - 1
     fetch prior from cursor_alltablename into @tablename
    end

    declare @count int;
    fetch first from cursor_alltablename into @tablename
    while (@@FETCH_STATUS = 0)
    begin
     set @strSql=N'alter table ['+@tablename +N'] check constraint all'
     exec (@strSql)
     --set @strSql = N'select @countOut=count(*) from sysobjects where parent_obj= OBJECT_ID(N'''+@tablename+N''') and xtype = N''PK'''; --这个是查询所有的主键列
     set @strSql = N'SELECT @countOut=count(*) FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='''+@tablename+ --这个是查询自增列
      ''' AND (COLUMNPROPERTY(OBJECT_ID('''+@tablename+'''),COLUMN_NAME,''IsIdentity'')=1)'
     exec sp_executeSql @strSql,N'@countOut int Output',@countOut = @count Output;
     if(@count > 0) --将有主键自增的表的自增列置为0
      begin
       set @strSql = N'dbcc checkident(N'''+@tablename+N''',reseed,0)'
       exec (@strSql)
      end
     fetch next from cursor_alltablename into @tablename
    end
    close cursor_alltablename
    deallocate cursor_alltablename

    2.查询数据库中所有存在自增列的表

    方法一:declare @Table_name varchar(60)

    set @Table_name = '';

    if Exists(Select top 1 1 from sysobjects
               Where objectproperty(id, 'TableHasIdentity') = 1
                 and upper(name) = upper(@Table_name)
             )
         select 1
    else select 0

    -- or

    if Exists(Select top 1 1 from sysobjects so
               Where so.xtype = 'U'
                 and upper(so.name) = upper(@Table_name)
                 and Exists(Select Top 1 1 from syscolumns sc
                             Where sc.id = so.id
                               and columnproperty(sc.id, sc.name, 'IsIdentity') = 1
                           )
             )
           select 1
    else select 0

    方法二:

    SELECT COUNT(*) FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Orders'  AND (COLUMNPROPERTY(OBJECT_ID('Orders'),COLUMN_NAME,'IsIdentity')=1)

    3.将数据库中表中自增列重新排列

    exec sp_configure 'allow updates',1
    reconfigure with override
    GO

    update syscolumns set colstat = 0 where id = object_id('dbo.Customer') and colstat = 1
    GO


    update   dbo.Customer 
    set   ID=(select   count(1)   from   dbo.Customer   where   ID<=t.ID)   
    from   dbo.Customer   t
    GO


    ueclare @a int 
    set @a=(select count(*) from dbo.Customer)
    DBCC CHECKIDENT (Customer, RESEED, @a)
    GO


    update syscolumns set colstat = 1 where id = object_id('dbo.Customer') and name = 'ID'
    GO  
    exec sp_configure 'allow updates',0
    reconfigure with override

  • 相关阅读:
    解决 搭建Jekins过程中 启动Tomcat的java.net.UnknownHostException异常
    射手和农场主
    java 和 JS(javaScript)中的反斜杠正则转义
    分享修改密码的SharePoint Web part: ITaCS Change Password web part
    分享微软官方Demo用的SharePoint 2010, Exchange 2010, Lync 2010虚拟机
    Office 365 的公共网站的一些限制及解决的办法
    SharePoint 2013 关闭 customErrors
    安装 KB2844286 导致SharePoint 2010 XSLT web part 显示出现错误
    安装Office Web Apps Server 2013 – KB2592525安装失败
    如何将hyper-v虚拟机转换成vmware的虚拟机- 转换SharePoint 2010 Information Worker Demonstration and Evaluation Virtual Machine (SP1)
  • 原文地址:https://www.cnblogs.com/quandeboke/p/3023874.html
Copyright © 2011-2022 走看看