Checks for existing Product record : EXISTS « Select Query « SQL Server / T-SQL






Checks for existing Product record


12>
13> create table employee(
14>     ID          int,
15>     name        nvarchar (10),
16>     salary      int,
17>     start_date  datetime,
18>     city        nvarchar (10),
19>     region      char (1))
20> GO
1>
2> insert into employee (ID, name,    salary, start_date, city,       region)
3>               values (1,  'Jason', 40420,  '02/01/94', 'New York', 'W')
4> GO
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (2,  'Robert',14420,  '01/02/95', 'Vancouver','N')
3> GO
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (3,  'Celia', 24020,  '12/03/96', 'Toronto',  'W')
3> GO
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (4,  'Linda', 40620,  '11/04/97', 'New York', 'N')
3> GO
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (5,  'David', 80026,  '10/05/98', 'Vancouver','W')
3> GO
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (6,  'James', 70060,  '09/06/99', 'Toronto',  'N')
3> GO
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (7,  'Alison',90620,  '08/07/00', 'New York', 'W')
3> GO
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (8,  'Chris', 26020,  '07/08/01', 'Vancouver','N')
3> GO
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (9,  'Mary',  60020,  '06/09/02', 'Toronto',  'W')
3> GO
1>
2> select * from employee
3> GO
ID          name       salary      start_date              city       region
----------- ---------- ----------- ----------------------- ---------- ------
          1 Jason            40420 1994-02-01 00:00:00.000 New York   W
          2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
          3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
          4 Linda            40620 1997-11-04 00:00:00.000 New York   N
          5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
          6 James            70060 1999-09-06 00:00:00.000 Toronto    N
          7 Alison           90620 2000-08-07 00:00:00.000 New York   W
          8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
          9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W
1>
2> -- Creating a Stored Procedure
3>
4>
5> /* Checks for existing Product record
6~    If exists, updates the record.  If not,
7~    inserts new record
8~ */
9> CREATE PROCEDURE spInsertOrUpdateEmployee
10>     -- Input parameters --
11>     @Name nVarChar(50),
12>     @City nVarChar(25),
13>     @Salary Money
14> AS
15>     IF EXISTS(SELECT * From Employee Where Name = @Name)
16>        UPDATE Employee SET City = @City, Salary = @Salary
17>        WHERE Name = @Name
18>     ELSE
19>        INSERT INTO Employee (Name, City, Salary)
20>        SELECT @Name, @City, @Salary
21> GO
1>
2> EXEC spInsertOrUpdateEmployee @Name='a',@City='b',@Salary=999
3> GO
1> EXEC spInsertOrUpdateEmployee @Name='Mary',@City='b',@Salary=999
2> GO
1>
2> select * FROM employee
3> GO
ID          name       salary      start_date              city       region
----------- ---------- ----------- ----------------------- ---------- ------
          1 Jason            40420 1994-02-01 00:00:00.000 New York   W
          2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
          3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
          4 Linda            40620 1997-11-04 00:00:00.000 New York   N
          5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
          6 James            70060 1999-09-06 00:00:00.000 Toronto    N
          7 Alison           90620 2000-08-07 00:00:00.000 New York   W
          8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
          9 Mary               999 2002-06-09 00:00:00.000 b          W
       NULL a                  999                    NULL b          NULL
1>
2> drop procedure spInsertOrUpdateEmployee
3> drop table employee
4> GO
1>
2>
           
       








Related examples in the same category

1.NOT EXISTS function
2.Use NOT EXISTS to represent the ALL operator
3.EXISTS function can be used to represent the ANY and ALL operators
4.Using EXISTS Operator for a subquery