**自:不忘過往
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
endexec (@trunclog)
endselect '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常用語句
use myoa select from delete from department where departmentid 1 insert department departmentid,departmentname values 1,技術部 update department set depa...
sql常用語句
在sqlserver,簡單的組合sp spaceused和sp msforeachtable這兩個儲存過程,可以方便的統計出使用者 資料表的大小,包括記錄總數和空間占用情況,非常實用,在sqlserver2k和sqlserver2005中都測試通過。1.exec sp spaceused 表名 sq...
sql常用語句
第一種 行列互換思想,外層group by,使用case when then 我有乙個表,有兩個字段 收費專案 唯一值 收費金額。我想用sql按收費專案不同生成不同的字段,對應值是金額。如 房租 100 水電費 50 雜費 50 生成後的格式是 房租 水電費 雜費 100 50 50 請問,如何寫這...