西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁(yè)西西教程數(shù)據(jù)庫(kù)教程 → 數(shù)據(jù)庫(kù)語(yǔ)法從基礎(chǔ)到精通

數(shù)據(jù)庫(kù)語(yǔ)法從基礎(chǔ)到精通

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:本站整理時(shí)間:2010/9/3 11:03:23字體大。A-A+

作者:佚名點(diǎn)擊:1196次評(píng)論:0次標(biāo)簽: 數(shù)據(jù)庫(kù)

  • 類型:辦公軟件大小:35KB語(yǔ)言:中文 評(píng)分:1.2
  • 標(biāo)簽:
立即下載
6 頁(yè) SQL數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程

USE tablename -- 要操作的數(shù)據(jù)庫(kù)名
SELECT @LogicalFileName = 'tablename_log', -- 日志文件名
@MaxMinutes = 10, -- Limit on time allowed to wrap log.
@NewSize = 1 -- 你想設(shè)定的日志文件的大小(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、說(shuō)明:更改某個(gè)表
exec sp_changeobjectowner 'tablename','dbo'

9、存儲(chǔ)更改全部表

CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch
@OldOwner as NVARCHAR(128),
@NewOwner as NVARCHAR(128)
AS

DECLARE @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
END

close curObject
deallocate curObject
GO


10、SQL SERVER中直接循環(huán)寫入數(shù)據(jù)
declare @i int
set @i=1
while @i<30
begin
insert into test (userid) values(@i)
set @i=@i+1
end

小記存儲(chǔ)過(guò)程中經(jīng)常用到的本周,本月,本年函數(shù)
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代碼只是一個(gè)時(shí)間段
Dateadd(wk,datediff(wk,0,getdate()),-1)
Dateadd(wk,datediff(wk,0,getdate()),6)
就是表示本周時(shí)間段.
下面的SQL的條件部分,就是查詢時(shí)間段在本周范圍內(nèi)的:
Where Time BETWEEN Dateadd(wk,datediff(wk,0,getdate()),-1) AND Dateadd(wk,datediff(wk,0,getdate()),6)
而在存儲(chǔ)過(guò)程中
select @begintime = Dateadd(wk,datediff(wk,0,getdate()),-1)
select @endtime = Dateadd(wk,datediff(wk,0,getdate()),6)
最后,再補(bǔ)充一些:
分組group
  常用于統(tǒng)計(jì)時(shí),如分組查總數(shù):
select gender,count(sno)
from students
group by gender
(查看男女學(xué)生各有多少)
  注意:從哪種角度分組就從哪列"group by"
  對(duì)于多重分組,只需將分組規(guī)則羅列。比如查詢各屆各專業(yè)的男女同學(xué)人數(shù) ,那么分組規(guī)則有:屆別(grade)、專業(yè)(mno)和性別(gender),所以有"group by grade, mno, gender"
select grade, mno, gender, count(*)
from students
group by grade, mno, gender
  通常group還和having聯(lián)用,比如查詢1門課以上不及格的學(xué)生,則按學(xué)號(hào)(sno)分類有:
select sno,count(*) from grades
where mark<60
group by sno
having count(*)>1
  6.UNION聯(lián)合
  合并查詢結(jié)果,如:
SELECT * FROM students
WHERE name like ‘張%’
UNION [ALL]
SELECT * FROM students
WHERE name like ‘李%’
  7.多表查詢
  a.內(nèi)連接
select g.sno,s.name,c.coursename
from grades g JOIN students s ON g.sno=s.sno
JOIN courses c ON g.cno=c.cno
(注意可以引用別名)
b.外連接
b1.左連接
select courses.cno,max(coursename),count(sno)
from courses LEFT JOIN grades ON courses.cno=grades.cno
group by courses.cno
  左連接特點(diǎn):顯示全部左邊表中的所有項(xiàng)目,即使其中有些項(xiàng)中的數(shù)據(jù)未填寫完全。
  左外連接返回那些存在于左表而右表中卻沒(méi)有的行,再加上內(nèi)連接的行。
  b2.右連接
  與左連接類似
  b3.全連接
select sno,name,major
from students FULL JOIN majors ON students.mno=majors.mno
  兩邊表中的內(nèi)容全部顯示
  c.自身連接
select c1.cno,c1.coursename,c1.pno,c2.coursename
from courses c1,courses c2 where c1.pno=c2.cno
  采用別名解決問(wèn)題。
  d.交叉連接
select lastname+firstname from lastname CROSS JOIN firstanme
  相當(dāng)于做笛卡兒積

 

 

 

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過(guò)難過(guò)
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)