SQL語句集錦

2021-04-15 06:09:40 字數 3734 閱讀 2937

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語句集錦

1.unix timestamp 自 1970 01 01 00 00 00 的到當前時間的秒數差 2.select unix timestamp 2012 06 08 獲取date 日期的時間戳 3.select if unix timestamp 1000 createtime select t...

Mysql 常用SQL語句集錦

查詢時間,友好提示 乙個sql 返回多個總數 sql select count all,sql count case when status 1 then status end status 1 num,sql count case when status 2 then status end sta...

sql語句編寫集錦 pgsql

1 查詢活動次數 1 活動次數查詢 select from select province,year,act num act people from bdd tmp ci order by year desc limit 5 as a1 order by act num desc 2 活動人數的查詢...