ตอนที่ 13 : การ Debug Stored Procedure บน Visual Studio 2010,2012,2013,2015 และอื่น ๆ |
ตอนที่ 13 : การ Debug Stored Procedure บน Visual Studio 2010,2012,2013,2015 และอื่น ๆ สำหรับฟีเจอร์นี้ถือว่าเป็นจุดเล่นและความสามารถน่าสนใจมาก เพราะเหตุลผก็คือใน SQL Server รุ่นใหม่ ๆ มันสามารถใช้ Visual Studio ทำการ Debug ตัว Stored Procedure ร่วมกับการเขียนโปรแกรมอื่น ๆ บน Visual Studio ได้เลย ซึ่งปัญหาในการเขียน Stored ยุคแรก ๆ ก็คือแทบจะไม่มีวิธีการ Debug ตัว Stored Procedure ได้เลย การหาข้อผิดพลาดระหว่าง Runtime นั้นเป็นไปได้ยากมา นอกจากการใช้วิธี PRINT เพื่อดูค่าทาง Result ด้วยเหตุนี้เอง Stored Procedure จึงไม่ค่อยได้รับความนิยมมากนัก และถือว่าเป็นจุดเด่นมาก ๆ ที่ใน Visual Studio รุ่นใหม่ ๆ มีฟีเจอร์นี้ขึ้นมาด้วย โดยเราสามารถที่จะ Debug ได้เฉพาะ Stored Procedure ก็ได้ หรือจะ Debug โปรแกรมตั้งแต่ Application จนมาถึงการเรียกใช้งาน Stored Procedure
Stored สำหรับทดสอบในการ Debug โปรแกรม
CREATE PROCEDURE [dbo].[myStoredProcedure]
@pRow INT
AS
BEGIN
-- Declare Table
DECLARE @temp TABLE
(
RowID INT,
Name VARCHAR(50),
LastName VARCHAR(50)
)
-- Variable
DECLARE @i INT
SET @i = 1
-- Loop Insert Table
WHILE (@i <= @pRow)
BEGIN
INSERT INTO @temp (RowID,Name,LastName) VALUES ((@i),'Weerachai','Nukitram')
SET @i = @i + 1
END -- WHILE
-- Variable
DECLARE @j INT
DECLARE @iRow INT
DECLARE @sName VARCHAR(50)
DECLARE @sLastName VARCHAR(50)
-- Set Variable
SET @j = 1
SELECT @iRow = COUNT(*) FROM @temp
-- Loop Display Row
WHILE (@j <= @iRow)
BEGIN
-- Select from table
SELECT @sName = Name , @sLastName = LastName
FROM @temp WHERE RowID = @i
-- Print output
PRINT ' Row : ' + CONVERT(VARCHAR,@j) + ', Name : ' + @sName + ', LastName : ' + @sLastName
SET @j = @j + 1
END -- WHILE
END
Note!! Visual Studio 2010 สามารถ Debug ได้แค่ SQL Server 2008 ลงไป ส่วน รุ่นอื่น ๆ จะต้องใช้ให้สูงกว่า Version ที่โปรแกรม Limit ไว้
Example 1 การ Debug Stored Procedure บน Visual Studio 2010

เลือกที่ Server Explorer คลิกขวา Add Connection

ข้อมูลที่จะติดต่อกับ SQL Server

รายการ SQL Server แสดงใน Server Explorer ของ Visual Studio

เลือกชื่อ Stored Procedure ที่ต้องการ

คลิกขวาเลือกที่ Open

หลังจากที่แสดง Stored Procedure เราสามารถกำหนด Breakpoints ที่จะ Debugได้

คลิกขวที่ Stored Procedure -> Step Info Stored Procedure

ในกรณีที่ Stored มี Parameters จะแสดง Dialog ให้ทำการ Input ค่าต่าง ๆ

จากนั้นตัว Cursor จะวิ่งมายัง Breakpoints และหยุดให้เราตรวจสอบค่าต่าง ๆ ซึ่งสามารถ Debug Step ได้เหมือนกับ Visual Studio เช่น F10 , F11 , F5 เป็นต้น

การ Control Step ต่าง ๆ
Example 2 การ Debug Stored Procedure บน Visual Studio 2012,2013 และอื่น ๆ

ในการ Debug ตัว Stored Procedure ตั้งแต่ Visual Studio 2012 ขึ้นไป

ให้คลิกที่เมนู VIEW -> SQL Server Object Explorer

ในกรณีที่แสดงข้อความ
Untable to connect a database and browse object.
Incompatible version of sql server data tools and database runtime components are installed on this computer. Wen I click on Lear more... and Get the latest version of SQL Server Data Tools..., these two page are not available.
แสดงทำการดาวน์โหลดและติดตั้งตัว SQL Server Data Tools. ให้เรียบร้อยเสียก่อน
Microsoft SQL Server Data Tools
ล่าสุด
https://msdn.microsoft.com/en-us/library/mt204009.aspx
สำรอง
http://www.microsoft.com/en-us/download/details.aspx?id=36843
http://www.microsoft.com/en-us/download/details.aspx?id=42313

เลือก Version ให้ตรงกับ Visual Studio ที่กำลังใช้งาน

ติดตั้ง Microsoft SQL Server Data Tools

แสดงรายการของ SQL Server

คลิกขวาที่ SQL Server -> Add SQL Server

ทำการ Authen ตัว SQL Server ที่จะเชื่อมต่อ

แสดงรายการของ SQL Server ที่ทำการ Add เข้ามา

เลือกที่ Stored Procedure ที่จะทำการ Debug

คลิกวาเลือก Debug Procedure

ใส่ค่า Parameters ในกรณีที่ต้องการให้มีค่าส่งไปด้วย

โปรแกรมจะสร้าง New Query ให้ใหม่ โดย Input ค่าต่าง ๆ ไปให้ด้วย

เราสามารถ Debug เข้าไปใน Step ย่อยของ Procedure ด้วยการกด F11

ถ้ามี Breakpoint จะหยุดและแสดงค่าต่าง ๆ ของตัวแปร
|