經典SQL語句大全之技巧

2021-06-10 10:00:05 字數 4867 閱讀 6316

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

endelse

begin

set @strsql = 'select count(*) as total from [' + @tblname + ']'

end

我們可以直接寫成

錯誤!未找到目錄項。

set @strsql = 'select count(*) as totalfrom [' + @tblname + '] where 1=1

安定 '+ @strwhere

2、收縮資料庫

--重建索引

dbccreindex

dbcc indexdefrag

--收縮資料和日誌

dbccshrinkdb

dbcc shrinkfile

3、壓縮資料庫

dbcc shrinkdatabase(dbname)

4、轉移資料庫給新使用者以已存在使用者許可權

exec sp_change_users_login 'update_one','newname','oldname'

go5、檢查備份集

restore verifyonly fromdisk='e:\dvbbs.bak'

6、修復資料庫

alter database [dvbbs] set single_user

godbcc checkdb('dvbbs',repair_allow_data_loss) with tablock

goalter database [dvbbs] set multi_user

go7、日誌清除

set nocount on

declare @logicalfilename sysname,

@maxminutes int,

@newsize int

usetablename -- 要操作的資料庫名

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 notexpired

and @originalsize = (select size fromsysfiles 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

endexec (@trunclog)

endselect 'final size of ' + db_name() + ' log is ' +

convert(varchar(30),size) + ' 8k pagesor ' +

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 proceduredbo.user_changeobjectownerbatch

@oldowner as nvarchar(128),

@newowner as nvarchar(128)

asdeclare@name as nvarchar(128)

declare @owner as nvarchar(128)

declare @ownername as nvarchar(128)

declare curobjectcursor 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 fromcurobject into @name, @owner

endclose curobject

deallocate curobject

go10、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

zhangshan80

lishi 59

wangwu 50

songquan69

while((select

min(score)from tb_table)<60)

begin

updatetb_table set score =score*1.01

wherescore

<60

if (select

min(score) from tb_table)>60

break

else

continue

end

SQL語句經典大全(三)技巧

三 技巧 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 ende...

SQL語句大全 技巧

三 技巧 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 ende...

經典SQL語句大全(三)

三 技巧 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 ende...