1 / 31

Introduction to Programming

Introduction to Programming. SQL Server using Transact-SQL. Objective. สามารถเขียนกลุ่มคำสั่งใน T-SQL และคอมเมนท์ได้ สามาถกำหนดตัวแปร กำหนดค่าให้กับตัวแปร ชนิด Local ใน T-SQL ได้ สามารถใช้ T-SQL control-of -flow ได้ สามารถส่งข้อความ หรือข้อความแสดงความผิดพลาด และ ผลลัพท์กลับคืนมาได้.

lotte
Download Presentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming SQL Server using Transact-SQL

  2. Objective • สามารถเขียนกลุ่มคำสั่งใน T-SQL และคอมเมนท์ได้ • สามาถกำหนดตัวแปร กำหนดค่าให้กับตัวแปร ชนิด Local ใน T-SQLได้ • สามารถใช้ T-SQL control-of -flow ได้ • สามารถส่งข้อความ หรือข้อความแสดงความผิดพลาด และ ผลลัพท์กลับคืนมาได้

  3. Application Development Context • กำหนดกฎข้อบังคับต่าง ๆ • กำหนดคุณสมบัติของโปรแกรม • ออกแบบทาง logical และ physical • เริ่มเขียนโปรแกรม

  4. Batch Restrictions and Note • คำสั่ง SQL บางคำสั่งไม่สามารถรวมกันคำสั่งอื่น ๆ ได้ใน T-SQL • Stored procedures ใน batch ต้องขึ้นต้นด้วยคำว่า execหรือ execute , เว้นแต่ว่าเป็นคำสั่งแรก • คำสั่งต่อไปนี้ไม่สามารถรวมกับคำสั่งอื่น ๆ ใน batch create table create default create trigger create views declare cursor use

  5. Commenting T-SQL Code • /* นี่คือ Comment*/ • -- นี่คือ Comment

  6. Introduction Local Varialbles • ประกาศตัวแปรชนิด Local • การกำหนดค่าให้กับตัวแปรชนิด Local • ข้อจำกัดและ ความผิดพลาดทั่วไป

  7. Local Varialbles • ผู้ใช้เป็นผู้กำหนด • ใช้คำสั่ง declare ในการกำหนดตัวแปร • ต้องมีชื่อและชนิดของข้อมูล • สามารถถูกกำหนดค่าโดยผู้ใข้ได้ • จะถูกกำหนดให้เป็นค่า nullเมื่อมีการประกาศตัวแปร • จะใช้งานภายใต้ batch, stored procedure หรือ trigger ที่ถูกประกาศไว้เท่านั้น

  8. Local Varialbles • ต้องขึ้นต้นด้วยเครื่องหมาย“@” • ชื่ตัวแปรมีความยาวได้ถึง 30 ตัวอักษรรวมเครื่องหมาย “@” • รูปแบบ declare@variable_name datatype [, @variable_name datatype] • ตัวอย่าง declare @myqty int declare @myid char(40)

  9. Assigning Values to Local Varialbles with Select • ใช้คำสั่ง Selectเพื่อกำหนดค่าให้กับตัวแปร • รูปแบบ select @variable=expression [, @variable=expression]… [from ...] [where …] • Example declare@var1 int select@var1 = 99

  10. Assigning Values to Local Varialbles with Select declare@ytdqty int, @sellprice money, @var1 int, @var2 float --initialize variables select@var1 = -68 , @var2 = 97.564

  11. Assigning Values to Local Varialbles with Select select @ytdqty = total_sales , @sellprice = price from titles where title_id = “TC7777” declare @income money select @income = price*total_sales from titles where title_id = “BU1032”

  12. Restrictions on Local variables • ตัวแปรใช้สำหรับเก็บค่าข้อมูลเท่านั้น • ตัวแปรไม่สามารถใช้เก็บชื่อตาราง ชื่อคอลัมน์ หรือ database objects อื่น ๆ หรือคียเวอร์ต่าง ๆ ได้ • ตัวแปรเก็บได้เพียงค่าเดียวเท่านั้น • ถ้าไม่มีการกำหนดค่าโดยคำสั่ง Selectค่าข้อมูลในตัวแปรก็จะยังคงอยู่เหมือนเดิม • ถ้ามีการกำหนดค่าด้วยคำสั่งSelect หลาย ๆ ครั้ง ตัวแปรจะเก็บค่าที่มีการกำหนดครั้งหลังสุด

  13. Using Local variables : • โปรแกรมนี้ผิดตรงไหน Declare@myvariable int select@myvariable = title from titles where title_id = “BU2075” select @myvariable

  14. Using Local variables : • โปรแกรมนี้ผิดตรงไหน selectpub_id from publishers select@myvat = total_sales from titles where title_id = “BU2075” Declare@myvar int select@myvar

  15. Global Variables • เป็นตัวแปรที่ SQL Server เตรียมไว้ให้ โดยระบบจะเป็นผู้กำหนดค่าให้เอง • ขึ้นต้นด้วยเครื่องหมาย “@@” • ผู้ใช้ไม่สามารถกำหนดค่าให้กับ global variable ได้ • Server จะเป็นผู้กำหนดให้เองโดยอัตโนมัติ

  16. Global Variables

  17. Using Global Variables • @@rowcount ใช้สำหรับตรวจสอบจำนวนแถวที่เกิดจากคำสั่งล่าสุด • @@error จะเป็นหมายเลขแสดงความผิดพลาดที่เกิดขึ้น

  18. Example: Code using Global Variables select@@version declare@book_price money select book_price = price from titles where title_id = “BU1032” If @@rowcount = 0 -- norows returned by select print “No such title_id” else begin print “title_id exists with” select “Price of ” =@book_price end

  19. Control-of-Flow Keywords • IF…Else • Begin…End • While… • Return… • waitfor...

  20. Control-of-Flow : If…else declare @avg_price money select @avg_price = avg(price) from titles if@avg_price < $15.00 update titles set price = price * $2 else update titles set price = price * $1.1

  21. Control-of-Flow : begin…end declare@avg_price money select @avg_price = avg(price) from titles if @avg_price < $15.00 begin update titles set price = price * $2 printe “Prices have been doubles” end else if @avg_price >= $15 begin update titles set price = price * $1.1 Print “Prices increased 10%” end else Print ”Print Avg Price Is Null”

  22. Control-of-Flow : If exists and If not exists declare @lname varchar(40) select@lname = “Smith” if exists (select * from authors where au_name = @lname) select “There is a ” + @lname else select “Therer is no author called ”+ @lname

  23. Control-of-Flow : return • เป็นคำสั่งจบการทำงานของ batch, stored procedure to trigger • คำสั่งที่อยู่หลัง return จะไม่ถูกทำงาน if not exists (select * from titles where title_id =@t_id ) begin print “There is no title by this title id.” return-- exit the batch end

  24. Control-of-Flow : return if not exists (select * from titles where title_id =@t_id ) begin print “There is no title by this title id.” return-- exit the batch end insert salesdetail values (@s_id , @o_num , @t_id , @qty_sold, @desc) go

  25. Control-of-Flow : while Repeated Execution While (Select avg(price) from titles) < $40 begin select title_id , price from titles where price > $20 update tiltes set price = price + $2 end select title_id , price from titles print “Too much for the matrkey to bear.”

  26. Control-of-Flow : while Repeated Execute • break และ continue • break ใช้สำหรับออกจาก loop แต่ไม่ออกจาก batch • continue ให้กลับไปที่บนสุดของ loop

  27. Control-of-Flow : while Repeated Execute While (select avg(price) from titles) > $20 begin update titles set price = price / 2 if (select max(price) from titles) < $40 break else if (select avg(price) from titles) > $20 continue print “Average price still over $20” end select title_id, price from titles where price > $20 print “Not too Expensive”

  28. Control-of-Flow : Event-Driven Execute • Waitfor ใช้หน่วงเวลาการทำงานชั่วคราว • Syntax waitfor{delay time | time time | … } waitfor delay ‘0:30:00’ -- หยุดคอย 30 นาที • ส่วนมากใช้โดยโปรเซสของระบบ เพื่อการจัดการข้อมูล การจัดการกับ error การจัดการกับ event ต่าง ๆ

  29. Returning Message Using print • print “Hello” • print @msg -- @msg is a local variable • declare@table_name varchar(30) , declare@user_name varchar(30) select@table_name = “titles” , @user_name = “ezekiel” print “The table %1! is not owned but the user %2!.” , @table_name , @user_name

  30. Returning Message Using raiserror • raiserror ใช้สำหรับกำหนด error ที่เหมือนกับ error ที่ถูกกำหนดโดย system-generated raiserror error_number { “error message” | @local variable} [ , arg_list ] error_number จะถูกบรรจุลงใน @@error @local_variable ต้องมี datatype เป็น char หรือ varchar • ใช้ raiserror เพื่อสร้างข้อความแสดงความผิดพลาดของเราเอง • ข้อจำกัดคือ • Error message มีความยาวไม่เกิน 255 ตัวอักษร • Error number ต้องมากกว่า 20000

  31. Returning Message Using raiserror • ตัวอย่างที่ 1 Message with no arg_list raiserror 30250 “The title dose not exist” • ตัวอย่างที่ 2 Message with arg_list declare@tab_name varchar(30) select@tab_name = “titles” raiserror 99999 “Table %1! Not found.”, @tab_name • ตัวอย่างที่ 3 declare@err_msg char(50) select@err_msg = “I can’t do that, ” + user_name raiserror 20100 @err_msg

More Related