Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

High Level Programming Basics Variables are first declared, using the DECLARE statement and specifying the

variables'name (which has tovbegin with @) and data type. The syntax is DECLARE @variable_name datatype Then, a value is set to the variable using either SET or SELECT. When a variable is declared, its value is initialized to NULL until a value is assigned. Examples DECLARE @firstname VARCHAR(20) SET @firstname = 'Maria' SELECT @firstname -------------------------------------------------------------------------------------------------------------DECLARE @ln VARCHAR(20), @fn VARCHAR(20) SELECT @ln = lastname, @fn = firstname FROM Employees WHERE employeeid = 1 SELECT @fn, @ln -------------------------------------------------------------------------------------------------------------Concatenation operator Example DECLARE @first VARCHAR(10), @second VARCHAR(10) SET @first = 'SQL ' SET @second = 'Server' SELECT @first + @second ----------------------------------------------------------------------------------------------------------------Control Flow Statements MS-SQL does not provide a FOR statement, like many other programming languages. Instead, it provides a WHILE statement, which can expose basically the same functionality of the FOR statement.

The IF statement contains a condition that is evaluated by SQL Server; if it is true, the code right after IF is executed, and if it is not true, the code right after ELSE is executed. Notice that the ELSE statement is optional. If there is more than one statement to execute in IF or ELSE, these have to be delimited by the BEGIN and END statements. The sysntax is IF <condition> Begin ------End Example IF EXISTS (SELECT * FROM Shippers) BEGIN DECLARE @number_rows INT SELECT @number_rows = count(*) FROM Shippers PRINT 'There are '+ CAST(@number_rows AS VARCHAR(10)) + 'rows in the Shippers table' END ELSE PRINT 'This table does not contain any rows'

WHILE WHILE iterates (executing some statements) until a certain condition is true. If there is more than one statement to execute until the condition is true, enclose them between BEGIN and END. The syntax is While <condition> Begin ------end

Example DECLARE @a INT, @b INT, @result INT SET @a = 3 SET @b = 4 SET @result = 0 WHILE @b > 0 BEGIN SET @result = @result + @a SET @b = @b - 1 END SELECT @result BREAK BREAK is used inside a WHILE statement to exit unconditionally from the WHILE loop. When SQL Server finds a BREAK inside a WHILE, it continues the execution with the instruction right after the END of the WHILE.

CONTINUE CONTINUE is used inside a WHILE statement to transfer the execution to the beginning of the WHILE statement, restarting the loop.

Example SET @count = 0 WHILE @count < 10 BEGIN IF @count = 3 BREAK SET @count = @count + 1 PRINT 'This line is executed' CONTINUE PRINT 'This line is never executed' END

User-Defined Stored Procedures You create user-defined stored procedures in SQL Server to implement business logic. Any task, no matter how simple or complex, that comprises multiple statements and conditions can be programmed as a stored procedure, and then the calling application just needs to execute the stored procedure, instead of executing the whole set of statements separately. User-defined stored procedures are created using the CREATE PROCEDURE statement, and then SQL Server stores them in the current database. Stored procedures'names, like any other object's name, must be unique within the database and unique to the user who creates them (the owner). Hence, in a certain database, it is possible that two store procedures exist with the same name but with different owners. Syntax Create proc <procedure name> @param1 datatype, @param2 datatype., @paramn datatype AS -------------

Example CREATE PROC getemployeesbylastname @emplastname VARCHAR(40) AS SELECT * FROM Employees WHERE lastname LIKE '%'+ @emplastname + '%'

To run Exec getemployeesbylastname lok Parameter with default value CREATE PROC getemployeesbylastname @emplastname VARCHAR(40) = 'a' AS SELECT * FROM Employees WHERE lastname LIKE '%'+ @emplastname + '%' To run Exec getemployeesbylastname

There are two types of parameters, input and output: 1. An input parameter is similar to a variable passed by value. Therefore, the stored procedure gets a copy of the data and this doesn't affect the data outside the stored procedure. In other words, if you the stored procedure, this doesn't change the value of the variable outside the stored procedure. An output parameter is like a variable passed by reference. Hence, because the stored procedure gets a pointer to a variable, any changes made to it are reflected outside the scope of the stored procedure. Using this type of parameter, a stored procedure can send values back to the calling application. To take advantage of output parameters, and to distinguish them from input parameters, the OUTPUT keyword must be specified when creating the stored procedure, and also when it is executed.

2.

Procedure with output parameter example CREATE PROC getCustomerInfo @customerid NCHAR(10), @contact NVARCHAR(60) OUTPUT, @company NVARCHAR(80) OUTPUT AS SELECT @contact = contactname, @company = companyname FROM Customers WHERE customerid = @customerid ---------------------------------------------------------------------------------------------------------------DECLARE @customer_id NCHAR(10),@customer_name NVARCHAR(60), @customer_company NVARCHAR(80) SET @customer_id = 'SAMSP' EXEC getCustomerInfo @customer_id, @customer_name OUTPUT, @customer_company OUTPUT SELECT @customer_name + '- '+ @customer_company

You might also like