SQL Server DML Statements examples

------------------------------------- -- DML - Data Manipulation Language ------------------------------------- USE Northwind GO ------------------------------------- -- INSERT (Implicit) ------------------------------------- DROP TABLE emps CREATE TABLE emps (id int...

SQL Server Basic TSQL Examples

  ---------------------------------------- -- Basic TSQL ---------------------------------------- -- Ex1 DECLARE @var1 int BEGIN SET @var1 = 1 PRINT @var1 END -- Ex2 DECLARE @v_firstName varchar(25) , @v_lastName varchar(25) BEGIN SET @v_firstName = 'Ram' SET...

SQL Server WHILE & Cursors Examples

  --------------------------- -- WHILE LOOP --------------------------- -- Ex1 DECLARE @v_var int BEGIN SET @v_var = 0 WHILE @v_var < 10 BEGIN PRINT @v_var SET @v_var = @v_var + 1 END END -- Ex2 SELECT * FROM employees DECLARE @v_employees_counter int ,...

SQL Server Stored Procedures Examples

------------------------------- -- Annonymous Blocks ------------------------------- DECLARE @v_int int BEGIN SET @v_int = 5 PRINT @v_int END ------------------------------ -- Named Programs -- 1. Procedures -- 2. Functions -- 3. Triggers...

SQL Server Functions Examples

  ------------------------------------------- -- Functions ------------------------------------------- -- Create / Alter function ALTER FUNCTION dbo.tax_fun (@num_to_tax MONEY) RETURNS MONEY AS BEGIN RETURN @num_to_tax*1.155 END -- Calling it from SELECT statement...

SQL Server Merge Examples

USE HR GO DROP TABLE emp_source ; GO DROP TABLE emp_destination ; GO DROP TABLE merge_log GO SELECT TOP 20 employee_id , last_name , salary , department_id INTO emp_source FROM employees SELECT TOP 10 employee_id , last_name , 2000 AS SALARY, department_id INTO...