1、1=1,1=2的使用 ,在SQL语句组合时用的较多

“where 1=1” 是表示选择全部    “where 1=2 ”全部不选,
如:
if @strWhere !='' 
begin
set @strSQL = 'count() as Total from [' + @tblName + '] where ' + @strWhere 
end
else 
begin
set @strSQL = 'count() as Total from [' + @tblName + ']' 
end

经典SQL语句大全—技巧 修复 数据库 经典 棋牌技术  第1张

我们可以直接写成

错误!未找到目录项 。
set @strSQL = 'count() as Total from [' + @tblName + '] where 1=1 安定 '+ @strWhere 2 、收缩数据库
--重建索引
DBCC REINDEX
DBCC INDEXDEFRAG
--收缩数据和日志
DBCC SHRINKDB
DBCC SHRINKFILE

3、压缩数据库
dbcc shrinkdatabase(dbname)

4、转移数据库给新用户以已存在用户权限
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 NOON
@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
@OriginalSize int
@OriginalSize = size 
 FROM sysfiles
 WHERE name = @LogicalFileName
'Original Size of ' + db_name() + ' LOG is ' + 
 CONVERT(VAR30),@OriginalSize) + ' 8K pages or ' + 
 CONVERT(VAR30),(@OriginalSize8/1024)) + 'MB'
 FROM sysfiles
 WHERE name = @LogicalFileName
CREATE TABLE DummyTrans
 (DummyColumn char (8000) not null)


@Counter    INT,
 @StartTime DATETIME,
 @TruncLog   VAR255)
@StartTime = GETDATE(),
 @TruncLog = 'BACKUP LOG ' + db_name() + ' WITH _ONLY'

DBCC SHRINKFILE (@LogicalFileName, @NewSize)
(@TruncLog)
-- Wrap the log if necessary.
WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) -- time has not expired
 @OriginalSize = (size FROM sysfiles WHERE name = @LogicalFileName)  
 (@OriginalSize 8 /1024) > @NewSize  
 BEGIN -- Outer loop.
@Counter = 0
 WHILE   ((@Counter < @OriginalSize / 16) (@Counter < 50000))
 BEGIN -- update
 DummyTrans VALUES ('Fill Log') DummyTrans
 @Counter = @Counter + 1
 END
 (@TruncLog)  
 END
'Final Size of ' + db_name() + ' LOG is ' +
 CONVERT(VAR30),size) + ' 8K pages or ' + 
 CONVERT(VAR30),(size8/1024)) + 'MB'
 FROM sysfiles 
 WHERE name = @LogicalFileName
DROP TABLE DummyTrans
SET NOOFF

8、说明:更改某个表
sp_changeobjectowner 'tablename','dbo'

9 、存储更改全部表

CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch
@OldOwner as NVAR128),
@NewOwner as NVAR128)
AS

@Name    as NVAR128)
@Owner   as NVAR128)
@OwnerName   as NVAR128)

curObject CURSOR FOR 
'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)
   sp_changeobjectowner @OwnerName, @NewOwner
end
-- @name,@NewOwner,@OldOwner

FETCH NEXT FROM curObject INTO @Name, @Owner
END

close curObject
deallocate curObject
GO


10、SQL SERVER中直接循环写入数据
@i int
set @i=1
while @i<30
begin
    into test (userid) values(@i)
    set @i=@i+1
end
案例
有如下表,要求就裱中所有沒有及格的成績 ,在每次增長0.1的基礎上 ,使他們剛好及格:

    Name     score

    Zhangshan   80

    Lishi       59

    Wangwu      50

    Songquan    69

经典SQL语句大全—技巧 修复 数据库 经典 棋牌技术  第2张

while((select min(score) from tb_table)<60)

begin

tb_table set score =score1.01

where score<60

if  (select min(score) from tb_table)>60

  break

 else

    continue

end

未经允许不得转载! 作者:棋牌源码网,转载或复制请以超链接形式并注明出处棋牌源码网

原文地址:《经典SQL语句大全—技巧》发布于:2021-08-06