sql語句妙用

2021-04-13 07:35:17 字數 3735 閱讀 7013

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)

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小記儲存過程中經常用到的本週,本月,本年函式

dateadd(wk,datediff(wk,0,getdate()),-1)

dateadd(wk,datediff(wk,0,getdate()),6)

dateadd(mm,datediff(mm,0,getdate()),0)

dateadd(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0))

dateadd(yy,datediff(yy,0,getdate()),0)

dateadd(ms,-3,dateadd(yy, datediff(yy,0,getdate())+1, 0))

上面的sql**只是乙個時間段

dateadd(wk,datediff(wk,0,getdate()),-1)

dateadd(wk,datediff(wk,0,getdate()),6)

就是表示本週時間段.

下面的sql的條件部分,就是查詢時間段在本週範圍內的:

where time between dateadd(wk,datediff(wk,0,getdate()),-1) and dateadd(wk,datediff(wk,0,getdate()),6)

而在儲存過程中

select @begintime = dateadd(wk,datediff(wk,0,getdate()),-1)

select @endtime = dateadd(wk,datediff(wk,0,getdate()),6)

oracle instr 妙用 SQL 語句

這是存在此要求時可這樣使用 若有如下情情況 dm content 1 11 ertre 12 4345 13 fgs 14 435 15 345 可以用 select content from where instr dm,傳入的引數 1 獲取 當dm為1 時 可得 11 ertre 12 4345...

SQL語句注釋的妙用

mysql裡面的sql語句,裡面是可以加注釋的 例如 select from author where authorid 1 可以寫成 get a author select from author where authorid 1 照樣可以正常執行。有人說,這有鳥用啊?那好看下面 1 在php裡面...

SQL語句中,自定義變數的妙用

我們在按日期統計每天的資料時,有時候會想統計截止到每一天的總量。固然可以通過一張輔助表 儲存了所需要的所有日期 來達到目的,可效率太低,影響查詢速度。今天分享乙個效率高 用法簡單的方式 自定義變數!廢話少說,直接上碼 select a.day,a.num,total total num from s...