AD Japan - Procedure

8 68 0
AD Japan - Procedure

Đang tải... (xem toàn văn)

Thông tin tài liệu

AD Japan - Procedure tài liệu, giáo án, bài giảng , luận văn, luận án, đồ án, bài tập lớn về tất cả các lĩnh vực kinh tế...

Bài 6: Stored Procedure and Advanced T-SQL Tác giả: Vovisoft.comTrong bài này chúng ta sẽ tìm hiểu một số cách import và export data trong SQL Server. Sau đó sẽ bàn qua các loại Stored Procedure và Cursor.Sử dụng bcp và BULK INSERT để import databcp là một command prompt dùng để import hay export data từ một data file (Text file hay Excel File) vào SQL Server hay ngược lại. Thường khi muốn chuyển một số lượng lớn data từ một database system khác như Oracle, DB2 .sang SQL Server trước hết ta sẽ export data ra một text file sau đó import vào SQL Server dùng bcp command. Một trường hợp thông dụng hơn là ta export data từ SQL Server sang một Microsoft Excel file và Excel file này có thể là input cho một program hay một database system khác.Chúng ta cũng có thể chuyển data vào SQL Server dùng câu lệnh BULK INSERT. Tuy nhiên BULK INSERT chỉ có thể import data vào trong SQL Server chứ không thể export data ra một data file như bcp.Ðể có thể insert data vào SQL Server Database, data file phải có dạng bảng nghĩa là có cấu trúc hàng và cột. Chú ý khi data được bulk copy (copy hàng loạt dùng bcp hay BULK INSERT) vào một table trong SQL Server thì table đó phải tồn tại và data được cộng thêm vào (append). Ngược lại khi export data ra một data file thì một file mới sẽ được tạo ra hoặc data file sẽ bị overwrite nếu nó tồn tại.Cú pháp đầy đủ của lệnh bcp có thể xem trong SQL Server Books Online. Ở đây chỉ trình bày một số ví dụ đơn giản về cách sử dụng bcp command và BULK INSERT.Ví dụ 1: Giả sử bạn muốn export data từ table Orders trong PracticeDB (đây là database được tạo ra trong bài tập số 1 ) ra một text file trong đó các cột được phân cách bằng dấu ";". Bạn có thể làm như sau: mở DOS command prompt và đánh vào dòng lệnh sau:bcp PracticeDB Orders out c:\Orders.txt -c –T –t;Trong ví dụ trên ta muốn bulk copy table Orders ra một text file trong đó : out: copy data từ table hay view ra một data file (c:\Orders.txt). Ngược lại ta có thể dùng switch in để import data từ text file vào SQL Server.-c: bulk copy dùng kiểu dữ liệu Character (Char) (nếu không chỉ rõ thì SQL Server sẽ dùng "TAB" character (\t) để phân định các cột và dùng new line character (\n) để phân định các hàng như các giá trị default).-t;: dấu ";" đi sau switch "t" cho biết ta muốn dùng ";" để phân định các cột (nếu không sẽ dùng giá trị mặc định như trên)-T: dùng (NT) Trust connection để kết nối với database. Nghĩa là nếu user đã authenticated (cho phép) vào được Windows system thì đương nhiên được sử dụng SQL Server mà không cần dùng thêm username và password nào khác.Ví dụ 2: Thay vì copy toàn bộ table ta có thể dùng query để select một phần data và export ra text file như sau:bcp "Select * From practiceDB Orders" queryout c:\Orders.txt -c -SVinhtai -Usa -PabcTrong ví dụ này ta select toàn bộ data trong Orders table ra một text file dùng query và SQL Server authentication.queryout : cho biết đây là một query chứ không phải là table.-S : tên của SQL Server (hay tên của một Instance) -U : SQL user name dùng để log on -P : password dùng để log on.Ví dụ 3 : dùng BULK INSERT để bulk copy data từ text file vào SQL Server database. Mở Query Analyser (BULK INSERT là một T-SQL command chứ không phải là một command prompt utility) và đánh vào các dòng sau :BULK INSERT PracticeDB Orders FROM 'c:\Orders.txt ' WITH (DATAFILETYPE = 'CHAR')Trong ví dụ trên DATAFILETYPE= 'CHAR' cho biết data được chứa dạng Char data type. Nêú muốn dùng data type dạng unicode thì dùng 'WIDECHAR'Chú ý: Các switch trong bcp command là case-sensitive. Nghĩa là chữ hoa và chữ thường sẽ có ý nghĩa khác nhau.Distributed Queries Ðôi khi chúng ta muốn select data từ những database system khác như MS Access, Oracle, DB2 . hay WORLD TRADE G/ADP/N/1/JPN/2/Corr.1* G/SCM/N/1/JPN/2/Corr.1* ORGANIZATION 25 July 1995 (95-2152) Original: English Committee on Anti-Dumping Practices Committee on Subsidies and Countervailing Measures NOTIFICATION OF LAWS AND REGULATIONS UNDER ARTICLES 18.5 AND 32.6 OF THE AGREEMENTS JAPAN The following communication, dated 14 July 1995, has been received from the Permanent Mission of Japan _ The following pages should be inserted between pages 46 and 47 of document G/ADP/N/1/JPN/2-G/SCM/N/1/JPN/2 _ * English only (Notice of the Initiation of Investigation) Article The Minister of Finance shall, when an investigation under paragraphs 5, 13 and 22 (including the cases where the said paragraphs shall apply mutatis mutandis to the provision of paragraph 31) or 27 of Article of the Law (hereinafter referred to as "investigation" except in Article 14, paragraph of Article 16 (other than the provisions of sub-paragraphs thereof Article 17 and Article 19 is decided to be initiated, promptly provide the following information in writing to the directly interested person (supplier of the product subject to such investigation or association thereof) a majority of the direct or indirect members of which would be supplier of the product subject to the investigation), importer of the product subject to the investigation or association thereof (a majority of the direct or indirect members of which must be importer of the product subject to such investigation), as well as applicant regarding such investigation (who made a request under paragraphs 4, 12, 21 of Article of the Law (including the cases where the provision of the said paragraph is applied to paragraph 31 of the said Article) or 26 of the Article 8), hereinafter in this paragraph referred to as the same) and shall give a public notice in the Official Gazette (1)The name and address of such applicant (2)The descriptions, name, type, model and features of the product subject to the investigation (3)Suppliers or supplying country of the product subject to the investigation (4)The date on which the investigation is initiated (5)Duration of investigation (6)A description of the subject to be investigated (7)The term of the submission of evidence and witness referred to in the former provision of paragraph of Article 10, reference of the evidences, etc referred to in paragraph of Article 11 and application under paragraphs of Article 12, respectively (8)Any other relevant matters The Minister of Finance shall, when informing the directly interested persons in accordance with the preceding paragraph, send in addition, writing and evidence (other than those parts found to be warranted to be confidential treated by nature or those parts presented as confidential information by applicant) to be presented in accordance with the provisions of paragraphs to of the preceding Article to the directly interested person other than the applicant The Minister of Finance shall promptly inform, with the reason therefore, the applicant to that effect in writing, when the request is made in accordance with the provisions of paragraphs 4, 12, 21 (including the case where the provision of the said paragraph shall apply mutatis mutandis in paragraph 31 of the said Article) or 26 of Article of the Law and accordingly the investigation is decided not to be initiated (Extension of the Period of Investigation) Article The Minister of Finance shall, if the period of investigation is decided to be extended in accordance with the proviso of paragraph of Article of the Law (including the case where the provision of the said paragraph is applied mutatis mutandis to the first sentence of paragraph 14 of the said Article) or with the proviso of paragraph 23 (including the case where the said paragraph shall apply mutatis mutandis to paragraphs 28 and 31 of the Article), promptly inform the directly interested parties to that effect in writing and of the extended period of investigation and reasons for the extension and shall give a public notice in the Official Gazette (Presentation of Evidences, etc.) Article 10 Where the investigation is initiated, the interested parties (the directly interested parties as well as interested producers, etc., and interested labour union (the majority of direct or indirect members of which must be engaged in domestic production of the like product of the imported product) other than directly interested parties hereinafter referred to as the same) may present to the Minister of Finance any evidence in writing or orally with regard to the fact referred to in paragraphs or 13 of Article of the Law, the change of situation under paragraph 22 of the said Article (including the case where the provision for the said paragraph shall apply mutatis mutandis in paragraph 31) or threat under paragraph 27 of said Article, within the term under paragraph of Article informed or made known to the public in accordance with paragraph of the said Article In this case, ...Hướng dẫn thực hành NMCNPM HƯỚNG DẪN THỰC HÀNH TUẦN 9Chủ đề: Gọi thực thi Stored Procedure1. Mục đích- Kết nối và thực hiện các thao tác trên CSDL SQL Server.- Biết cách tham số hóa cho câu lệnh SQL.- Hiểu và vận dụng được cách thực thi các câu lệnh SQL thông qua Stored Procedure.o Stored Procedure không có kết quả trả về (parameter input)o Stored Procedure có kết quả trả về (parameter output)2. Cơ sở dữ liệu- Tạo CSDL SQL Server mới, tên CSDL là QLHocSinh. Tạo và thiết kế database mới gồm hai bảng như sau:HOCSINHSTT Tên trường Kiểu dữ liệu Ghi chú1 MaHS Varchar(10) PrimaryKey2 TenHS Nvarchar(50)3 NgaySinh Date/Time4 DiaChi Nvarchar(250)5 DTB Real/float6 MaLop Varchar(10) ForeignKey (tham chiếu đến Lop(MaLop)LOPSTT Tên trường Kiểu dữ liệu Ghi chú1 MaLop Varchar(10) PrimaryKey2 TenLop Nvarchar(100)3 SiSo int- Hoặc import CSDL từ Access đã được tạo trong tuần trước (CSDL QLHOCSINH.mdb).1 Hướng dẫn thực hành NMCNPM - Hướng dẫn cách import CSDL Access vào CSDL SQL Server:o Trước hết ta tạo một Database mới với tên QLHocSinho Từ Database QLHocSinh chọn Import Data…o Chọn Next, Sau đó chọn dữ liệu nguồn (dữ liệu nguồn ở đây là Access)2 Hướng dẫn thực hành NMCNPM o Chọn Next, Chọn đích import (đã được mặc định là SQL Server)o Chọn Next3 Hướng dẫn thực hành NMCNPM o Chọn Nexto Chọn các table muốn import vào CSDL SQL Server. Sau đó chọn Next.4 Hướng dẫn thực hành NMCNPM 3. Sử dụng lại ứng dụng trong tuần trước hoặc tạo ứng dụng mới:- Để kết nối với CSDL SQL Server, ta phải khai báo namespace như sau:using System.Data.SqlClient;- Khai báo các đối tượng sau để thực hiện các thao tác với CSDL SQL Server:SqlConnection connection;SqlDataAdapter adapter;SqlCommand command = new SqlCommand();- Chuỗi kết nối với CSDL SQL Server, gồm bốn thông tin: tên server, tên databse, userid và password."server=dungta; database=QLHocSinh; user id=sa; password=";4. Áp dụng kỹ thuật tham số hóa cho câu lệnh SQLa. Thực hiện thêm mới thông tin một học sinh- Trước đây, chúng ta thiết lập câu lệnh insert như sau:string sql = "INSERT INTO HOCSINH VALUES('"txtMaHS.Text.Trim() + "', '" + txtTenHS.Text.Trim() + "', '" + dtNgaySinh.Value.ToString() + "', '" + txtDiaChi.Text.Trim() + "', " + txtDiemTB.Text.Trim() + ", '" + cboLop.SelectedValue + "')";command = new SqlCommand(sql, connection);command.ExecuteNonQuery();- Để tham số hóa các tham số cho câu lệnh SQL ta thực hiện các bước sau:o Tham số hóa câu lệnh: @[tên tham số]o Tạo các parameters tương ứng cho command.o Đặt giá trị cho các parameter mỗi khi dùng command thực hiện câu lệnh.- Như vậy, chúng ta có thể thay thế đoạn code trên bằng đoạn code như sau:string sql="INSERT INTO HOCSINH VALUES(@Ma,@Ten,@Ns,@Dc,@Diem,@Lop)";5 Hướng dẫn thực hành NMCNPM command.Connection = connection;command.CommandText = sql;command.Parameters.AddWithVa Tungvn40@yahoo.com Cong Minh CDROM 70 NCT F2 Q10SQL Server 2000Exercise 2: Manipulate Data and Stored Procedure-----Week 5-----Please follow those steps to practise:1. Use bcp to export all data from Orders table of PracticeDB to c:\Orders.txt (or to c:\Orders.csv)2. Change some data in the c:\Orders.txt and save. Then import to Orders table from the text file using bcp3. Import Orders.txt to Orders table using BULK INSERT 4. Create a Linked Server ‘LinkedPracticeDB’ which link to an Access database ‘PracticeDB.mdb’ (firstly you have to create an Access database similar to PracticeDB in SQL Server and input some data). Then do a select data using four-part name and OPENQUERY5. Using ad hoc computer name with OPENROWSET and OPENDATASOURCE functions to select data from ‘PracticeDB.mdb’ 6. Create the following CursorDECLARE @au_lname varchar(40), @au_fname varchar(20)DECLARE Employee_Cursor CURSOR FOR SELECT LastName, FirstName FROM Northwind.dbo.Employees OPEN Employee_Cursor FETCH NEXT FROM Employee_Cursor INTO @au_lname, @au_fname WHILE @@FETCH_STATUS = 0 BEGIN PRINT 'Author:' + @au_fname + ' ' + @au_lnameFETCH NEXT FROM Employee_Cursor INTO @au_lname, @au_fnameEND CLOSE Employee_Cursor DEALLOCATE Employee_Cursor 7. Create the following stored procedure and try to execute with some valuesCREATE PROCEDURE AddNewOrder @OrderID smallint,@ProductName varchar(50),@CustomerName varchar(50),@Result smallint=1 OutputTungvn40@yahoo.com Cong Minh CDROM 70 NCT F2 Q10 Tungvn40@yahoo.com Cong Minh CDROM 70 NCT F2 Q10ASDECLARE @CustomerID smallintBEGIN TRANSACTIONIf not Exists(SELECT CustomerID FROM Customers WHERE [Name]=@CustomerName)BEGINSET @CustomerID= (SELECT Max(CustomerID) FROM Customers)SET @CustomerID=@CustomerID+1INSERT INTO Customers VALUES(@CustomerID,@CustomerName)If Exists(SELECT OrderID FROM [Orders] WHERE OrderID=@OrderID)BEGINSELECT @Result=1ROLLBACK TRANSACTIONENDElseBEGININSERT INTO [Orders](OrderID,ProductName,CustomerID) VALUES(@OrderID,@ProductName,@CustomerID)SELECT @Result=0COMMIT TRANSACTIONENDEND ElseBEGINIf Exists(SELECT OrderID FROM [Orders] WHERE OrderID=@OrderID)BEGINSELECT @Result=1ROLLBACK TRANSACTIONENDElseBEGININSERT INTO [Orders](OrderID,ProductName,CustomerID) VALUES(@OrderID,@ProductName,@CustomerID)SELECT @Result=0COMMIT TRANSACTIONENDENDPrint @ResultTungvn40@yahoo.com Cong Minh CDROM 70 NCT F2 Q10 Tungvn40@yahoo.com Cong Minh CDROM 70 NCT F2 Q10Return9. Using VB 6 or VB.NET to execute the ‘AddNewOrder’ stored procedure 10. Using xp_cmdshell extended stored procedure to send a message (xp_cmdshell ‘net send Hello’)Tungvn40@yahoo.com Cong Minh CDROM 70 NCT F2 Q10 Khái niệm về thủ tục và hàmTH&NTKhái niệm về chương trình con:Trong lập trình, chúng ta thường gặp những công việc, những đoạn chương trìnhlặp đi lặp lại nhiều lần ở những chỗ khác nhau. Nhất là khi viết các chươngtrình lớn, việc sử dụng chương trình con (CTC) là hết sức cần thiết. Chúng tacó thể đưa vào chương trình một CTC thực hiện công việc này. Sau đó mỗi khi cầnphải thực hiện công việc, ta chỉ cần gọi CTC đó ra mà không phải viết lại cảđoạn chương trình. Như vậy, nếu phải viết một chương trình thực hiện nhiều côngviệc, thì ta nên đưa vào các CTC, mỗi CTC thực hiện một công việc. Và khi đóchương trình sẽ có cấu trúc rất dễ hiểu, dễ kiểm tra, dễ phát hiện ra lỗi vàsửa sai.Các bạn có thể hình dung việcchia công việc ra thành các CTC qua minh hoạ sau: Trong một dây chuyền sảnxuất, người ta phân chia thành nhiều khâu, bộ phận. Mỗi bộ phận thực hiện cáccông việc khác nhau. Để lắp ráp thành một sản phẩm, dây chuyền sản xuất phảilắp ghép các bộ phận vào nhau. Việc chia chương trình thành các CTC cũng vậy.Chúng ta có thể phân tách vấn đề phức tạp của bài toán thành các vấn đề nhỏ hơn(tương ứng với các CTC) để dễ kiểm tra, gỡ rối từng khối một và sau đó ghép lạithành chương chương trình lớn. CTC được dùng rất phổ biến. Vì vậy chúng ta cầnnắm vững các kỹ thuật lập CTC. Có hai loại CTC là thủ tục và hàm.Thủ tục và hàm có nhiềuđiểm giống nhau: có cấu tạo giống như một chương trình, đều chứacác tham số.Quy tắc viết thủ tục:PROCEDURE Tên_thủ_tục (Các tham số hình thức);Một thủ tục có thể không có thamsố, có một hoặc nhiều tham số. Tham số dùng để ghi các dữ liệu cần thiết đượcđưa vào thủ tục khi bắt đầu thực hiện thủ tục, hoặc ghi các dữ liệu là kết quảcủa việc thực hiện thủ tục. Nếu một tham số sẽ dùng để ghi lại các dữ liệu làkết quả của việc thực hiện thủ tục thì nó phải là tham số biến. Còn nếu khôngnó là tham số trị. Đồng thời, nếu một thủ tục có tham số thì các tham số phảiđược khai báo sau Tên_thủư_tục, trong dấu cặp ngoặc tròn và gọi là các tham sốhình thức.Cấu trúc của thủ tục:PROCEDURE Tên_thủ_tục (Khai báocác tham số);(*Khai báo các Label, Const,Type, Var của riêng Procedure nếu cần)Begin(*Thân chương trình con*) End;Cũng giống như thủ tục, hàm cóthể có một hoặc nhiều tham số. Đối với hàm, nói chung các tham số là tham số giátrị vì kết quả đã được gán cho tên hàm.Quy tắc viết tên hàm:FUNCTION Tên_hàm (Các tham số hình thức): Kiểu_hàm;Kết quả của việc thực hiện hàm làmột giá trị thuộc kiểu dữ liệu nào đó. Do đó trong phần đầu của hàm, sauTên_hàm và Các tham số, phải chỉ ra kiểu của hàm.Giá trị của hàm được lưu giữ vàotên hàm, nên tên hàm phải có mặt ít nhất một lần ở vế trái của một câu lệnh gántrong thân của hàm.Cấu trúc của hàm:FUNCTION Tên_hàm (Khai báo cáctham số): Kiểu dữ liệu;(*Khai báo các Label, Const, Type,Var của riêng Function nếu cần)Begin(*Thân chương trình con*)End; ... investigation was initiated, the Minister of Finance shall, by the time-limit noticed under sub-paragraph of paragraph of Article or made known to the public in accordance with the provision of paragraph... the matters regarding such investigation may, by the time-limit as provided for in sub-paragraph of paragraph of Article noticed or made known to the public in accordance with the provision of... representative consumer organizations of such products may, by the time-limit provided for sub-paragraph of paragraph of Article noticed or made known to the public in accordance with the provisions of

Ngày đăng: 20/10/2017, 19:38

Tài liệu cùng người dùng

Tài liệu liên quan