Begin...end block : Code Block « Transact SQL « SQL Server / T-SQL Tutorial

SQL Server / T-SQL Tutorial
1. Query
2. Insert Delete Update
3. Table
4. Table Join
5. Data Types
6. Set Operations
7. Constraints
8. Subquery
9. Aggregate Functions
10. Date Functions
11. Math Functions
12. String Functions
13. Data Convert Functions
14. Analytical Functions
15. Sequence Indentity
16. View
17. Index
18. Cursor
19. Database
20. Transact SQL
21. Procedure Function
22. Trigger
23. Transaction
24. XML
25. System Functions
26. System Settings
27. System Tables Views
28. User Role
29. CLR
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL Tutorial » Transact SQL » Code Block 
20. 13. 2. Begin...end block
4>
5>
6CREATE TABLE Orders (
7>      OrderID int IDENTITY (11NOT NULL ,
8>      CustomerID nchar (5NULL ,
9>      EmployeeID int NULL ,
10>     OrderDate datetime NULL ,
11>     RequiredDate datetime NULL ,
12>     ShippedDate datetime NULL ,
13>     ShipVia int NULL ,
14>     Freight money NULL DEFAULT (0),
15>     ShipName nvarchar (40NULL ,
16>     ShipAddress nvarchar (60NULL ,
17>     ShipCity nvarchar (15NULL ,
18>     ShipRegion nvarchar (15NULL ,
19>     ShipPostalCode nvarchar (10NULL ,
20>     ShipCountry nvarchar (15NULL)
21> GO
1>
2>    create PROC spInsertDateValidatedOrder
3>       @CustomerID       nvarchar(5),
4>       @EmployeeID       int,
5>       @OrderDate        datetime     = NULL,
6>       @RequiredDate     datetime     = NULL,
7>       @ShippedDate      datetime     = NULL,
8>       @ShipVia          int,
9>       @Freight          money,
10>       @ShipName         nvarchar(40= NULL,
11>       @ShipAddress      nvarchar(60= NULL,
12>       @ShipCity         nvarchar(15= NULL,
13>       @ShipRegion       nvarchar(15= NULL,
14>       @ShipPostalCode   nvarchar(10= NULL,
15>       @ShipCountry      nvarchar(15= NULL,
16>       @OrderID          int      OUTPUT
17>    AS
18>    DECLARE   @Error               int
19>    DECLARE   @BadDate             varchar(12)
20>    DECLARE   @InsertedOrderDate   smalldatetime
21>    IF DATEDIFF(dd, @OrderDate, GETDATE()) OR @OrderDate IS NULL
22>    BEGIN
23>       SELECT @BadDate = CONVERT(varchar, @OrderDate)
24>       RAISERROR (60000,1,1, @BadDateWITH SETERROR
25>       RETURN @@ERROR
26>    END
27>    SELECT @InsertedOrderDate =
28>       CONVERT(datetime,(CONVERT(varchar,@OrderDate,112)))
29>       PRINT 'The Time of Day in Order Date was truncated'
30>    INSERT INTO Orders VALUES(@CustomerID,@EmployeeID,@InsertedOrderDate,@RequiredDate,
31>       @ShippedDate,@ShipVia,@Freight,@ShipName,@ShipAddress,@ShipCity,
32>       @ShipRegion,@ShipPostalCode,@ShipCountry)
33>    SELECT @Error = @@ERROR
34>    IF @Error != 0
35>    BEGIN
36>       IF @Error = 547
37>       BEGIN
38>          PRINT 'Supplied data violates data integrity rules'
39>          PRINT 'Check that the supplied customer number exists'
40>          PRINT 'in the system and try again'
41>       END
42>       ELSE
43>       BEGIN
44>          PRINT 'An unknown error occurred. Contact your System Administrator'
45>          PRINT 'The error was number ' + CONVERT(varchar, @Error)
46>       END
47>       RETURN @Error
48>    END
49>    SELECT @OrderID = @@IDENTITY
50>    RETURN
51>    GO
1>
2>
3>    drop PROC spInsertDateValidatedOrder;
4>    drop table Orders;
5>    GO
20. 13. Code Block
20. 13. 1. When multiple lines of code follow an IF statement it is best to wrap the lines in a BEGIN. . . END block.
20. 13. 2. Begin...end block
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.