SQL語句基礎教程 二

2021-06-01 20:53:55 字數 3936 閱讀 3686

三、技巧

1、1=1,1=2的使用,在sql語句組合時用的較多

「where 1=1」 是表示選擇全部 「where 1=2」全部不選,

如:if @strwhere !=''

begin

set @strsql = 'select count(*) as total from [' + @tblname + '] where ' + @strwhere

endelse

begin

set @strsql = 'select count(*) as total from [' + @tblname + ']'

end我們可以直接寫成

set @strsql = 'select count(*) as total from [' + @tblname + '] where 1=1 安定 '+ @strwhere

2、收縮資料庫

--重建索引

dbcc reindex

dbcc indexdefrag

--收縮資料和日誌

dbcc shrinkdb

dbcc shrinkfile

3、壓縮資料庫

dbcc shrinkdatabase(dbname)

4、轉移資料庫給新使用者以已存在使用者許可權

exec sp_change_users_login 'update_one','newname','oldname'

go5、檢查備份集

restore verifyonly from disk='e:\dvbbs.bak'

6、修復資料庫

alter database [dvbbs] set single_user

godbcc checkdb('dvbbs',repair_allow_data_loss) with tablock

goalter database [dvbbs] set multi_user

go7、日誌清除

set nocount on

declare @logicalfilename sysname,

@maxminutes int,

@newsize int

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**:

SQL語句建表基礎教程

create database if not exists 資料庫名 eg create database if not exists test use 資料庫名 eg use test create table if not exists 表名 欄位名1 資料型別 列級完整性約束條件 commen...

SQL基礎教程

sql是用於訪問和處理資料庫的標準的計算機語言。什麼是sql?sql指結構化查詢語言 sql是我們有能力訪問資料庫 sql是一種更ansi的標準計算機語言 sql能做什麼?sql面向資料庫執行查詢 sql可從資料庫取回資料 sql可在資料庫中插入新的記錄 sql可以更新資料庫中的資料 sql可以從資...

SQL基礎教程

1.sql 指的是?structured query language 2.哪個 sql 語句用於從資料庫中提取資料?select 3.哪條 sql 語句用於更新資料庫中的資料?update 4.哪條 sql 語句用於刪除資料庫中的資料?delete 5.哪條 sql 語句用於在資料庫中插入新的資料...