SQL語句大全 技巧

2021-06-02 20:44:41 字數 4782 閱讀 7083

三、技巧

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 total from [' + @tblname + '] where 1=1 安定 '+ @strwhere

2、收縮資料庫

--重建索引

dbcc reindex

dbcc indexdefrag

--收縮資料和日誌

dbcc shrinkdb

dbcc shrinkfile

3、壓縮資料庫

dbccshrinkdatabase(dbname)

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

execsp_change_users_login 'update_one','newname','oldname'go

5、檢查備份集

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_usergo

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 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 procedure dbo.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 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

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

zhangshan   80

lishi       59

wangwu      50

songquan    69

while((selectmin(score) from tb_table)<60)

begin

updatetb_table set score =score*1.01

where score<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 endelse b...

《SQL語句大全》

語 句 功 能 資料操作 select 從資料庫表中檢索資料行和列 insert 向資料庫表新增新資料行 delete 從資料庫表中刪除資料行 update 更新資料庫表中的資料 資料定義 create table 建立乙個資料庫表 drop table 從資料庫中刪除表 alter table 修...