2008年3月31日 星期一

本機封裝DTS 備份 與 匯入

MSSQL2000 的 本機封裝DTS備份 與 匯入

本機封裝DTS備份
1. 開啟本機封裝以進入 封裝編輯頁
2. 點選 [封裝P] \ [另存新檔(A)…] 開啟【儲存DTS封裝】視窗
3. 在 「位置(L):」下拉清單 選 [結構化儲存體檔案];選好後下面的框即可直接輸入或以點 […] 的方式取得存檔路徑
4. 點選 [確定] 即完成備份作業

本機封裝DTS備份檔案匯入
1. 使用 SQL Server Enterprise Manager 資料庫路徑內的 [資料轉換服務]
2. 在 [資料轉換服務] 上 點 滑鼠右鍵 \ [開啟封裝(O)] 開啟【選擇封裝】視窗
3. 在 【選擇封裝】視窗中選取所要載入的封裝,完成後按 [確定] 以開啟封裝,進入 封裝編輯頁
4. 在 封裝編輯頁 內,點選 [封裝P] \ [另存新檔(A)…] 開啟【儲存DTS封裝】視窗
5. 在 「位置(L):」下拉清單 選 [SQL Server];選好後下面的框輸入要儲存至的Microsoft SQL Server 及 使用的 帳號、密碼
5. 點選 [確定] 即完成封裝DTS備份檔案匯入作業

2008年3月16日 星期日

SQL Server 變更物件擁有者的程序

轉載網址:http://support.microsoft.com/kb/275312/zh-tw



INF:SQL Server 變更物件擁有者的程序
如果您有許多的物件需要變更擁有權,可以使用下列的 SQL Server 預存程序來簡化程序:

if exists (select * from sysobjects where id = object_id(N'[dbo].[chObjOwner]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[chObjOwner]
GO

SET QUOTED_IDENTIFIER OFF SET ANSI_NULLS ON
GO

CREATE proc chObjOwner( @usrName varchar(20), @newUsrName varchar(50))
as
-- @usrName is the current user
-- @newUsrName is the new user

set nocount on
declare @uid int -- UID of the user
declare @objName varchar(50) -- Object name owned by user
declare @currObjName varchar(50) -- Checks for existing object owned by new user
declare @outStr varchar(256) -- SQL command with 'sp_changeobjectowner'
set @uid = user_id(@usrName)

declare chObjOwnerCur cursor static
for
select name from sysobjects where uid = @uid

open chObjOwnerCur
if @@cursor_rows = 0
begin
print 'Error: No objects owned by ' + @usrName
close chObjOwnerCur
deallocate chObjOwnerCur
return 1
end

fetch next from chObjOwnerCur into @objName

while @@fetch_status = 0
begin
set @currObjName = @newUsrName + "." + @objName
if (object_id(@currObjName) > 0)
print 'WARNING *** ' + @currObjName + ' already exists ***'
set @outStr = "sp_changeobjectowner '" + @usrName + "." + @objName + "','" + @newUsrName + "'"
print @outStr
print 'go'
fetch next from chObjOwnerCur into @objName
end

close chObjOwnerCur
deallocate chObjOwnerCur
set nocount off
return 0


GO
SET QUOTED_IDENTIFIER OFF SET ANSI_NULLS ON
GO


使用範例

若要變更所有物件的擁有者由使用者 test 到 dbo,請執行下列程式碼:
exec chObjOwner 'test','dbo'
此程式碼能找出由 test 擁有的所有物件,並且透過使用 sp_changeobjectowner 預存程序產生一系列 SQL 陳述式。您也可以在 SQL Server Query Analyzer 裡檢查及執行此陳述式。

如果新使用者擁有的物件名稱與現在使用者的相同,則警告訊息將會與物件名稱一起顯示。例如,如果 test 及 dbo 都擁有名為 world 的資料表,那下列錯誤訊息將會顯示:
警告 *** dbo.world 已經存在 ***
sp_changeobjectowner 'test.world','dbo'
go
(WARNING *** dbo.world already exists ***
sp_changeobjectowner 'test.world','dbo'
go)

如果試著將依然擁有資料庫物件的使用者卸除,則程序會失敗並產生下列錯誤訊息:
15183 :此使用者擁有資料庫裡的物件,因此無法卸除。
(The user owns objects in the database and cannot be dropped.)

當開機時發現 Boot.ini 遺失時

轉貼網址
http://support.microsoft.com/kb/330184/zh-tw



當您啟動電腦時出現 Invalid Boot.ini (無效的 Boot.ini) 或 Windows Could Not Start (Windows 無法啟動) 錯誤訊息
發生的原因
如果 Boot.ini 檔遺失、損壞或包含不正確的項目,則可能發生此問題。
解決方案
如果要解決此問題,請從 Windows XP CD 啟動電腦,並啟動「修復主控台」,然後使用 Bootcfg.exe 工具重建 Boot.ini 檔案。如果要執行這項操作,請依照下列步驟執行:
1. 將電腦設定為從 CD-ROM 光碟機或 DVD-ROM 光碟機啟動。如需有關如何執行這項操作的資訊,請參閱電腦的說明文件,或與您的電腦製造商連絡。
2. 將 Windows XP 光碟插入 CD-ROM 光碟機或 DVD-ROM 光碟機中,然後重新啟動電腦。
3. 當您看到 Press any key to boot from CD (請按任意鍵從光碟開機) 訊息時,請按任意鍵,以從 Windows XP 光碟啟動電腦。
4. 當您看見 Welcome to Setup (歡迎使用安裝程式) 訊息時,請按 R 以啟動「修復主控台」。
5. 如果您使用雙開機方式或多開機方式的電腦,請從「修復主控台」選取您所必須使用的安裝。
6. 當系統提示您時,請輸入系統管理員密碼,然後按 ENTER。
7. 在命令提示處輸入 bootcfg /list,再按 ENTER。畫面上會顯示目前 Boot.ini 檔中的項目。 (ps.遺失boot.ini的話應該會顯示找不到項目)
8. 在命令提示處輸入 bootcfg /rebuild,再按 ENTER。此命令會在電腦的硬碟上進行 Windows XP、Microsoft Windows 2000 或 Microsoft Windows NT 安裝的掃描,然後顯示掃描的結果。請依照畫面上的指示將 Windows 安裝程式新增到 Boot.ini 檔案中。例如,依照這些步驟將 Windows XP 安裝程式新增到 Boot.ini 檔案中:
a. 當您收到類似下列的訊息時,按下 Y:
Total identified Windows installs: (識別的 Windows 安裝總數:)1

[1] C:\Windows
Add installation to boot list? (新增安裝程式到開機清單?)(Yes/No/All) (是/否/全部)
b. 您會收到類似下列的訊息:
Enter Load Identifier (輸入載入識別碼)
此為作業系統的名稱。當您收到這項訊息時,請輸入作業系統的名稱,然後按 ENTER。此為 Microsoft Windows XP Professional (商用版) 或 Microsoft Windows XP Home Edition (家用版)。
c. 您會收到類似下列的訊息:
Enter OS Load options (輸入 OS 載入選項)
當您收到這項訊息時,請輸入 /fastdetect,然後按 ENTER。

注意 畫面上顯示的指示可能會根據電腦的設定而有所不同。

9. 鍵入 exit,再按下 ENTER,結束「修復主控台」。當您收到 Please select the operating system to start (請選取要啟動的作業系統) 訊息時,電腦便會重新啟動,並顯示更新的開機清單。

2008年3月6日 星期四

產生資料表序號的方式

產生資料表序號的方式
------------------------------------------------------------------------

print '建範例表 #tmp1'
select * into #tmp1
from (
select '1' no, '101' class, 'A' type,'66' a1,'88' a2,'55' a3
union select '2','101','B','77','88','77'
union select '3','101','C','88','78','65'
union select '4','102','A','66','50','56'
union select '5','102','C','77','78','67'
) a
go
print '顯示 #tmp1 '
select * from #tmp1
------------------------------------------------------------------------
-- 4.產生資料表序號的方式 ps.要產生序號 的條件必須是唯一,否則可能產生重覆的序號
------------------------------------------------------------------------

select * from #tmp1
/*
原表
no class type a1 a2 a3
1 101 A 66 88 55
2 101 B 77 88 77
3 101 C 88 78 65
4 102 A 66 50 56
5 102 C 77 78 67

*/
print '4.1 希望依 class, type 的大小 重新產生序號 -- 顯示 可用在 insert '
select a.class, a.type, count(*) no
from #tmp1 a, #tmp1 b
where (a.class+a.type)>=(b.class+b.type)
group by a.class, a.type
order by a.class, a.type
/*
print '4.1 更新序號編號欄位'
update #tmp1 set no = (
select count(*) no
from #tmp1 b
where (#tmp1.class+#tmp1.type)>=(b.class+b.type)
)

*/
select a.class, a.a2
from #tmp1 a
order by a.class, a.a2

print '4.2.1 希望依 class, a2 的大小 重新產生序號 '
select a.class, a.a2, count(*) no
from #tmp1 a, #tmp1 b
where (a.class+a.a2)>=(b.class+b.a2)
group by a.class, a.a2
order by a.class, a.a2
/*
-- class, a2 的大小 發生 重覆的情況,造成重新產生序號異常
101 78 1
101 88 6
102 50 4
102 78 5

*/
print '4.2.2 由於 class, a2 發生重覆 所以要加條件 type 以使其變成 條件唯一'
select a.class, a.a2, count(*) no
from #tmp1 a, #tmp1 b
where (a.class+a.a2+a.type)>=(b.class+b.a2+b.type)
group by a.class, a.a2, a.type
order by a.class, a.a2

-- ps.可以以 group by 先來做 是否 唯一的判斷
print '使用 group by 來判斷 4.2 的兩個例子'
print '4.2.1 檢查'
select a.class, a.a2, count(*) no
from #tmp1 a
group by a.class, a.a2
having count(*)>1
order by a.class, a.a2
/*
101 88 2 -- 重覆資料

*/

print '4.2.2 檢查' select a.class, a.a2, a.type, count(*) no from #tmp1 a group by a.class, a.a2, a.type having count(*)>1
order by a.class, a.a2, a.type
/*
沒有重覆資料

*/
/*
print '4.2.3 更新序號編號欄位'
update #tmp1 set no = (
select count(*) no
from #tmp1 b
where (#tmp1.class+#tmp1.a2+#tmp1.type)>=(b.class+b.a2+b.type)
)

*/
select *
from #tmp1 a
/*
no class type a1 a2 a3
2 101 A 66 88 55
3 101 B 77 88 77
1 101 C 88 78 65
4 102 A 66 50 56
5 102 C 77 78 67

*/

2008年3月5日 星期三

如何將 橫的資料顯示 轉為 直的資料顯示

將 橫表 轉為 直表,如 class 有很多 type 每個 type 僅有 一個值(a1),現在想列出 每個class type 的 a1 值 (這是有所限制的,必須有特定的資料才能轉)
------------------------------------------------------------------------
print '建範例表 #tmp1'
select * into #tmp1
from (
select '1' no, '101' class, 'A' type,'66' a1,'88' a2,'55' a3
union select '2','101','B','77','88','77'
union select '3','101','C','88','78','65'
union select '4','102','A','66','50','56'
union select '5','102','C','77','78','67'
) a
go
print '顯示 #tmp1 '
select * from #tmp1

go
------------------------------------------------------------------------
print '依 type 直列數值 a1, a2, a3'
select class,type,a1 from #tmp1

select class
, max(case when type='A' then a1 else '' end) A
, max(case when type='B' then a1 else '' end) B
, max(case when type='C' then a1 else '' end) C
from #tmp1
group by class

2008年3月3日 星期一

exists 範例使用

print '建範例表 #tmp1'
select * into #tmp1
from (
select '1' no,'A' type,'66' a1,'88' a2,'55' a3
union select '2','B','77','88','77'
union select '3','C','88','78','65'
union select '4','A','66','50','56'
union select '5','C','77','78','67'
) a
go
print '顯示 #tmp1 '
select * from #tmp1

go
------------------------------------------------------------------------
-- 1.想要 增加一個 no 但又不想有重複的 no 在資料表內(沒有使用table的方式)
------------------------------------------------------------------------
print 'exists 範例 MSSQL'
print '1.想要 增加一個 no 但又不想有重複的 no 在資料表內'
declare @no varchar(2) --變數使用
select @no ='6' -- <<--可以調整 @no 來試試

insert into #tmp1( no, type, a1, a2, a3 )
select @no,'D','77','88','77' -- <<-- 要增加的列(這是沒有使用table的方式)
where not exists (
select * from #tmp1 where no=@no
)
go

------------------------------------------------------------------------
print '顯示 #tmp1 看看 會可到 加了一筆 6 的資料,但之後再執行上面哪段並不會再多加一次 6 的資料'
select * from #tmp1

go
------------------------------------------------------------------------
-- 2.想要 增加一個 no 但又不想有重複的 no 在資料表內(使用table的方式)
------------------------------------------------------------------------
print '2.想要 增加一個 no 但又不想有重複的 no 在資料表內'
declare @no varchar(2) --變數使用
select @no ='7' -- <<--可以調整 @no 來試試

insert into #tmp1( no, type, a1, a2, a3 )
select top 1 @no,'D','77','88','77' -- <<-- 要增加的列(這是使用table的方式)
from #tmp1
where not exists (
select * from #tmp1 where no=@no
)
go

------------------------------------------------------------------------
print '顯示 #tmp1 看看 會可到 加了一筆 6 的資料,但之後再執行上面哪段並不會再多加一次 6 的資料'
select * from #tmp1

go