2015年8月7日 星期五

以SQL Server發送郵件

sp_send_dbmail (Transact-SQL)

 Sends an e-mail message to the specified recipients. The message may include a query result set, file attachments, or both. When mail is successfully placed in the Database Mail queue, sp_send_dbmail returns the mailitem_id of the message. This stored procedure is in the msdb database.

 reference from : https://msdn.microsoft.com/en-us/library/ms190307.aspx

Sending an e-mail message with the results of a query

This example sends an e-mail message to Dan Wilson using the e-mail address danw@Adventure-Works.com. The message has the subject Work Order Count, and executes a query that shows the number of work orders with a DueDate less than two days after April 30, 2004. Database Mail attaches the result as a text file.
EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'Adventure Works Administrator',
    @recipients = 'danw@Adventure-Works.com',
    @query = 'SELECT COUNT(*) FROM AdventureWorks2012.Production.WorkOrder
                  WHERE DueDate > ''2004-04-30''
                  AND  DATEDIFF(dd, ''2004-04-30'', DueDate) < 2' ,
    @subject = 'Work Order Count',
    @attach_query_result_as_file = 1 ;



Note1. Enable the Database Mail extended stored procedures.

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Database Mail XPs', 1;
GO
RECONFIGURE
GO

reference from : https://technet.microsoft.com/en-us/library/ms191189(v=sql.110).aspx

Note2. Create database mail profile via wizard.

2015年8月4日 星期二

遞迴(Recursive)

遞迴(Recursion)


一個還沒執行完又呼叫自己,符合條件時才停止的副程式。
一個不停呼叫自己直到滿意為止的程式。

初學時,總覺得莫名其妙,明明迴圈就可以處理了,為何要改成這種沒寫好就會陷入無窮迴圈的模式...

遇過有人看到所寫的程式寫法太"單純",沒用到遞回就質疑學藝不精...

洪教授寫了一篇愚公移山與遞迴,清楚說明了這跟效率與程式可讀性有關,

除此之外,個人認為還有Programmer 習慣與思考迴路的影響...

舉例來說:
假設每個月存3000元,隔月比上個月多存100元,計算存到n個月後會有多少錢?
也就是第1個月存3000元、第2個月存3100、第3個月存3200...

迴圈寫法
///<param name="n">計算月數</param>
///<param name="inc">每月增額</param>
///<param name="deposit">存款</param>
///<returns></returns>
protected int recursive(int n, int inc, int deposit)
{
    int mny = 0;
    for (int i = 1; i <= n; i++)
    {
        mny = mny + deposit + inc * (i - 1);
    }
    return mny;
}

遞迴寫法
///<param name="n">計算月數</param>
///<param name="inc">每月增額</param>
///<param name="deposit">存款</param>
///<returns></returns>
protected int recursive(int n, int inc, int deposit)
{
    return (n == 1) ? deposit : deposit + recursive(n - 1, inc, deposit + inc);
}

2015年7月16日 星期四

VHD虛擬磁碟擴充

VHD 檔案,Virtual Machine Hard Disk 虛擬磁碟

實體硬碟遇到空間不足時,除了刪檔案外就是在買一顆更大的硬碟,

虛擬磁碟遇到空間不足,只要硬碟夠大,還可以額外擴充,

擴充方法一:

1.新增一個容量更大的虛擬磁碟
2.把原虛擬磁碟上的檔案搬到1
3.刪除原虛擬磁碟檔案
4.將新的虛擬磁碟重新命名為原名稱 balabalabala..............

擴充方法二:

Step1.擴充容量的配置
可以透過工具vhd resizer,若使用Hyper-V則可以透過Hyper-V管理員呼叫「編輯虛擬硬碟精靈」  精靈可以協助將指定的虛擬磁碟檔案「壓縮/轉換/擴充」

Step2.讓Step1配置生效,有兩種方式:
    I. 下指令(參照:孤影大的孤影棧 - [tips]擴充vhd空間)

     a. 開始,於執行輸入diskpart
     b. 輸入list disk
     c. 輸入select disk # (#為磁碟編號,從list disk所顯示的訊息中可取得)
     d. 輸入detail disk
     e. 輸入select Volume # (#為磁區編號,從detail disk所顯示的訊息中可取得)
     f. 輸入extend


    II. 使用磁碟管理員
         a. 掛接虛擬磁碟檔案(可以看到擴充的容量)
         b. 選擇虛擬磁碟後按滑鼠右鍵,執行「延伸磁碟區」並依精靈指示操作。

2015年6月21日 星期日

Restart


東忙西忙瞎忙了一陣,

近日又想起這個已經荒廢許久,可說從沒認真經營過的部落格...

費神費時才回憶起登入密碼 囧

登入一看,原來最近一次發文是在2012/3/30  XD

起初,

一方面是想為自己的工作歷程留下部份軌跡,

一方面是想找尋自己的其他可能性,

一方面是想讓自己能夠同時兼顧工作與生活,

有時機會必須自己去創造,

有時機會來的讓人措手不及,

當努力創造的機會在萌牙之初,碰上了突如其來的機會時,

幸運的是,生活踏實了許多,

不幸的是,腦神經錯亂了許多,

計畫永遠改不上變化,

不想讓自己的人生留下遺憾,重啟了這個部落格,

但在可見的未來數月,尚有諸多可能讓人措手不及的變化,

想做的事情太多、太雜,

這次可以持續多久呢 ~"~

我會努力的!!