最新公告
  • 欢迎您光临编程知识分享网 加入我们
  • MySQL教程之技巧

    三、技巧
     
    1、1=1,1=2的使用,在SQL语句组合时用的较多
     
    “where 1=1” 是表示选择全部    “where 1=2”全部不选,
    如:
    if @strWhere !=”
    begin
    set @strSQL = ‘select count(*) as Total from [‘ + @tblName + ‘] where ‘ + @strWhere
    end
    else
    begin
    set @strSQL = ‘select count(*) as Total from [‘ + @tblName + ‘]’
    end
     
    我们可以直接写成
     
    错误!未找到目录项。
    set @strSQL = ‘select count(*) as Total from [‘ + @tblName + ‘] where 1=1 安定 ‘+ @strWhere 2、收缩数据库
    –重建索引
    DBCC REINDEX
    DBCC INDEXDEFRAG
    –收缩数据和日志
    DBCC SHRINKDB
    DBCC SHRINKFILE
     
    3、压缩数据库
    dbcc shrinkdatabase(dbname)
     
    4、转移数据库给新用户以已存在用户权限
    exec sp_change_users_login ‘update_one’,’newname’,’oldname’
    go
     
    5、检查备份集
    RESTORE VERIFYONLY from disk=’E:\dvbbs.bak’
     
    6、修复数据库
    ALTER DATABASE [dvbbs] SET SINGLE_USER
    GO
    DBCC CHECKDB(‘dvbbs’,repair_allow_data_loss) WITH TABLOCK
    GO
    ALTER DATABASE [dvbbs] SET MULTI_USER
    GO
     
    7、日志清除
    SET NOCOUNT ON
    DECLARE @LogicalFileName sysname,
    @MaxMinutes INT,
    @NewSize INT
     
    USE tablename — 要操作的数据库名
    SELECT  @LogicalFileName = ‘tablename_log’, — 日志文件名
    @MaxMinutes = 10, — Limit on time allowed to wrap log.
    @NewSize = 1  — 你想设定的日志文件的大小(M)
     
    Setup / initialize
    DECLARE @OriginalSize int
    SELECT @OriginalSize = size
    FROM sysfiles
    WHERE name = @LogicalFileName
    SELECT ‘Original Size of ‘ + db_name() + ‘ LOG is ‘ +
    CONVERT(VARCHAR(30),@OriginalSize) + ‘ 8K pages or ‘ +
    CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + ‘MB’
    FROM sysfiles
    WHERE name = @LogicalFileName
    CREATE TABLE DummyTrans
    (DummyColumn char (8000) not null)
     
    DECLARE @Counter    INT,
    @StartTime DATETIME,
    @TruncLog   VARCHAR(255)
    SELECT @StartTime = GETDATE(),
    @TruncLog = ‘BACKUP LOG ‘ + db_name() + ‘ WITH TRUNCATE_ONLY’
     
    DBCC SHRINKFILE (@LogicalFileName, @NewSize)
    EXEC (@TruncLog)
    — Wrap the log if necessary.
    WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) — time has not expired
    AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = @LogicalFileName)
    AND (@OriginalSize * 8 /1024) > @NewSize
    BEGIN — Outer loop.
    SELECT @Counter = 0
    WHILE   ((@Counter < @OriginalSize / 16) AND (@Counter < 50000))
    BEGIN — update
    INSERT DummyTrans VALUES (‘Fill Log’) DELETE DummyTrans
    SELECT @Counter = @Counter + 1
    END
    EXEC (@TruncLog)
    END
    SELECT ‘Final Size of ‘ + db_name() + ‘ LOG is ‘ +
    CONVERT(VARCHAR(30),size) + ‘ 8K pages or ‘ +
    CONVERT(VARCHAR(30),(size*8/1024)) + ‘MB’
    FROM sysfiles
    WHERE name = @LogicalFileName
    DROP TABLE DummyTrans
    SET NOCOUNT OFF
     
    8、说明:更改某个表
    exec sp_changeobjectowner ‘tablename’,’dbo’
     
    9、存储更改全部表
     
    CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch
    @OldOwner as NVARCHAR(128),
    @NewOwner as NVARCHAR(128)
    AS
     
    DECLARE @Name    as NVARCHAR(128)
    DECLARE @Owner   as NVARCHAR(128)
    DECLARE @OwnerName   as NVARCHAR(128)
     
    DECLARE curObject CURSOR FOR
    select ‘Name’    = name,
    ‘Owner’    = user_name(uid)
    from sysobjects
    where user_name(uid)=@OldOwner
    order by name
     
    OPEN   curObject
    FETCH NEXT FROM curObject INTO @Name, @Owner
    WHILE(@@FETCH_STATUS=0)
    BEGIN
    if @Owner=@OldOwner
    begin
    set @OwnerName = @OldOwner + ‘.’ + rtrim(@Name)
    exec sp_changeobjectowner @OwnerName, @NewOwner
    end
    — select @name,@NewOwner,@OldOwner
     
    FETCH NEXT FROM curObject INTO @Name, @Owner
    END
     
    close curObject
    deallocate curObject
    GO
     
    10、SQL SERVER中直接循环写入数据
    declare @i int
    set @i=1
    while @i<30
    begin
    insert into test (userid) values(@i)
    set @i=@i+1
    end
    案例
    有如下表,要求就裱中所有沒有及格的成績,在每次增長0.1的基礎上,使他們剛好及格:
     
        Name     score
     
        Zhangshan   80
     
        Lishi       59
     
        Wangwu      50
     
        Songquan    69
     
    while((select min(score) from tb_table)<60)
     
    begin
     
    update tb_table set score =score*1.01
     
    where score<60
     
    if  (select min(score) from tb_table)>60
     
      break
     
     else
     
        continue
     
    end
    赞赏

    微信赞赏支付宝赞赏

    编程知识分享网,一个有趣的平台!
    编程知识分享网|编程教程|资源下载|源码下载 » MySQL教程之技巧

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    关于编程知识分享网(www.ittce.com)
    编程知识分享网,一个有趣的平台,小心有毒!

    发表评论

    提供最优质的资源集合

    立即查看 了解详情
    升级SVIP尊享更多特权立即升级