精妙SQL語句收集

2021-04-14 01:10:55 字數 3752 閱讀 9911

精妙sql語句收集

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)

精妙SQL語句收集

星期五 2007年08月17日 精妙sql語句收集 use tablename 要操作的資料庫名 select logicalfilename tablename log 日誌檔名 maxminutes 10,limit on time allowed to wrap log.newsize 1 你...

精妙SQL語句收集

use tablename 要操作的資料庫名 select logicalfilename tablename log 日誌檔名 maxminutes 10,limit on time allowed to wrap log.newsize 1 你想設定的日誌檔案的大小 m setup initia...

精妙SQL語句收集

use tablename 要操作的資料庫名 select logicalfilename tablename log 日誌檔名 maxminutes 10,limit on time allowed to wrap log.newsize 1 你想設定的日誌檔案的大小 m setup initia...