xml地图|网站地图|网站标签 [设为首页] [加入收藏]
当前位置: www8029com > 澳门新葡8522最新网站 > 正文

Sql的基础知识技巧

时间:2019-10-23 23:06来源:澳门新葡8522最新网站
三、技巧 1、1=1,1=2 的选择,在 SQL 语句组适那时候用的非常多 “where 1=1” 是意味选拔任何 “where 1=2”整体不选, 如: if @strWhere !='' begin set @strSQL = 'select count(*) as Total from [' @tblName

三、技巧
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 SH昂科威INKDB DBCC SHLANDINKFILE

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

4、转移数据库给新客户以已存在顾客权限
exec sp_change_users_login 'update_one','newname','oldname' go

5、检查备份集 RESTORE VE福睿斯IFYONLY 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 = @Logical FileName)
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
ELECT '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 SE奥迪Q7VE锐界 中央行政机关接循环写入数据
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

编辑:澳门新葡8522最新网站 本文来源:Sql的基础知识技巧

关键词: www8029com