SQL知識總結

2021-04-13 09:24:26 字數 3659 閱讀 8651

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知識總結

分頁查詢 在oracle中 分頁查詢通過隱含的rownum查詢 select from select rownum r,from slect from from tb where rownum 100 只能小於 where r 90 這裡注意外巢狀後r已經是表內列,通過巢狀確定要顯示的列 mysql...

sql 總結知識點

對於存放時間格式為2003090709 56 00 轉換為datetime型別方法 cast stuff a.cyrq,9,0,as datetime 結果為 2003 09 07 09 56 00.000 而將標準datetime型別轉換為字元型方法為 declare begin datetime...

SQL操作知識總結(一)

1 1,普通選擇語句 select name from products 在products表中檢索name的列。一般與where連用。如 select name from products where id 1 在products表中檢索列為id的的name,id 1的有幾條資料,就顯示幾條資料。...