顯示具有 SQL Server 標籤的文章。 顯示所有文章
顯示具有 SQL Server 標籤的文章。 顯示所有文章

2016年5月18日 星期三

當 IDENTITY_INSERT 設為 OFF 時,無法將外顯值插入資料表 'TableName' 的識別欄位中

使用識別欄位(通常是自動編號)的資料表複製移動時,
出現當IDENTITY_INSERT 設為 OFF 時,無法將外顯值插入資料表 'TableName' 的識別欄位中

參照MSDN: SET IDENTITY_INSERT (Transact-SQL)

SET IDENTITY_INSERT [ database_name . [ schema_name ] . ] table { ON | OFF }

於Insert Into 前先執行
SET IDENTITY_INSERT TableName On;


結果出現另一個錯誤訊息:位於資料表 TableName 的識別欄位其外顯值只有當使用了資料行清單且 IDENTITY_INSERT 為 ON 時才能指定。


2016年5月1日 星期日

Microsoft SQL Server, 錯誤: 18456

通常是在執行SQL Server連線時才有機會看到18456這個錯誤訊息,這跟SQL Server授權驗證有關,也就是說看到這個訊息代表的是連線失敗。

SQL Server 的登入驗證方式有2種:[Windows驗證]以及[SQL Server驗證]。

1.Windows驗證:使用Windows本機端使用者帳戶登入SQL Server。
選擇這個驗證方式,並且看到錯誤:18456時,有可能是安裝SQL Server的過程中未將該帳戶加入存取權限,或是新建的使用者帳戶。因此,在能連線到SQL Server的狀況下,到SQL Server\安全性\登入中,加上該本機端使用者帳戶即可。
詳細設定可參考這篇:
以 Windows 驗證方式登入 SQL Server 時,遇到錯誤代碼為 18456 的錯誤,該如何排除?

2.SQL Server驗證:使用SQL Server帳號登入。
選擇這個驗證方式,在排除帳號、密碼輸入錯誤的情況下看到錯誤: 18456,絕對不是人品不佳,而是沒有開放SQL Server驗證模式。
怎麼開放呢?  開啟[伺服器屬性]->選取頁面[安全性],將驗證模式異動為"SQL Server 及 Windows 驗證模式"。
設定完成後,須重新啟動SQL Server服務(不是把Management Studio關掉重開喔)。



2016年4月21日 星期四

SQL Server無法附加資料庫? Microsoft SQL Server, 錯誤: 5120


SQL Server附加資料庫時出現存取被拒的錯誤訊息 (Microsoft SQL Server, 錯誤: 5120)

原因: 無該資料庫檔案存取權限、無存放該資料庫檔案資料夾存取權限...

解法1. 
增加資料庫檔案的必要存取權限,例如這篇

SQL Server附加資料庫失敗 錯誤碼 5120 作業系統錯誤 5 存取被


解法2. 
將資料庫檔案搬到有存取權限的資料夾,例如這篇

SQL Server無法附加資料庫? 錯誤碼 5120 / 作業系統: 5 權限不足



解法3.
用"以系統管理員身分執行"的方式開啟Microsoft SQL Server Management Studio,並執行附加作業。

有趣的是用這方式附加資料庫檔案成功以後,下次直接開啟Management Studio時還是可以正常存取資料庫。



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.