I have this table
CREATE TABLE Dept
(
Deptno INT NOT NULL,
Dname VARCHAR(50) NOT NULL,
Loc VARCHAR(50) NOT NULL,
CONSTRAINT Dept_PK PRIMARY KEY(Deptno)
);
As you see Deptno
is not autoincremented.
But I need to create a procedure that inserts Dname
and Loc
, where Deptno
will be auto-generated.
What I have so far:
CREATE PROCEDURE InserNewDepartmentWithoutNumber
@name VARCHAR(50),
@location VARCHAR(50)
AS
IF EXISTS(SELECT * FROM Dept WHERE Dept.Dname = @name)
PRINT 'This department already exists'
ELSE
BEGIN
INSERT INTO Dept (Dname, Loc)
VALUES (@name, @location)
END