IF and EXISTS function : If « Transact SQL « SQL Server / T-SQL

SQL Server / T-SQL
1. Aggregate Functions
2. Analytical Functions
3. Constraints
4. Cursor
5. Data Set
6. Data Type
7. Database
8. Date Timezone
9. Index
10. Insert Delete Update
11. Math Functions
12. Select Query
13. Sequence
14. Store Procedure Function
15. String Functions
16. Subquery
17. System
18. Table
19. Table Joins
20. Transact SQL
21. Transaction
22. Trigger
23. View
24. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL » Transact SQL » If 
IF and EXISTS function

 


13create table Billings (
14>     BankerID           INTEGER,
15>     BillingNumber      INTEGER,
16>     BillingDate        datetime,
17>     BillingTotal       INTEGER,
18>     TermsID            INTEGER,
19>     BillingDueDate     datetime ,
20>     PaymentTotal       INTEGER,
21>     CreditTotal        INTEGER
22>
23);
24> GO
1>
2INSERT INTO Billings VALUES (11'2005-01-22', 1651,'2005-04-22',123,321);
3> GO

(rows affected)
1INSERT INTO Billings VALUES (22'2001-02-21', 1651,'2002-02-22',123,321.);
2> GO

(rows affected)
1INSERT INTO Billings VALUES (33'2003-05-02', 1651,'2005-04-12',123,321);
2> GO

(rows affected)
1INSERT INTO Billings VALUES (44'1999-03-12', 1651,'2005-04-18',123,321);
2> GO

(rows affected)
1INSERT INTO Billings VALUES (55'2000-04-23', 1651,'2005-04-17',123,321);
2> GO

(rows affected)
1INSERT INTO Billings VALUES (66'2001-06-14', 1651,'2005-04-18',123,321);
2> GO

(rows affected)
1INSERT INTO Billings VALUES (77'2002-07-15', 1651,'2005-04-19',123,321);
2> GO

(rows affected)
1INSERT INTO Billings VALUES (88'2003-08-16', 1651,'2005-04-20',123,321);
2> GO

(rows affected)
1INSERT INTO Billings VALUES (99'2004-09-17', 1651,'2005-04-21',123,321);
2> GO

(rows affected)
1INSERT INTO Billings VALUES (00'2005-10-18', 1651,'2005-04-22',123,321);
2> GO

(rows affected)
1>
2>
3CREATE PROC spInsertBilling
4>        @BankerID    int,           @BillingNumber  varchar(50),
5>        @BillingDate smalldatetime, @BillingTotal   money,
6>        @TermsID     int,           @BillingDueDate smalldatetime
7> AS
8> IF EXISTS(SELECT FROM Bankers WHERE BankerID = @BankerID)
9>     BEGIN
10>         INSERT Billings (BankerID)
11>         VALUES (@BankerID)
12>     END
13> ELSE
14>     BEGIN
15>         RAISERROR('Not a valid BankerID!',1,1)
16>         RETURN -100
17>     END
18> GO
1>
2>
3> drop PROC spInsertBilling;
4> GO
1>
2>
3> drop table Billings;
4> GO

 
Related examples in the same category
1. IF and else IF
2. Conditional Logic IF
3. ELSE: execute another line of script when the condition is not met
4. If statement
5. If else statement
6. if and else if statement
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.