-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthor.sql
More file actions
66 lines (46 loc) · 1.42 KB
/
Author.sql
File metadata and controls
66 lines (46 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
create database LibraryDB
use LibraryDB
create Table Author(
Id int primary key identity(1,1),
Name nvarchar(50) not null,
CreateOn datetime not null constraint CK_Author_CreateOn check(CreateOn>'1900-01-01')
)
--
Alter Table Author drop constraint CK_Author_CreateOn
Alter Table Author Add Constraint DF_Author_CreateOn Default GETDATE() FOR CreateOn
-- Stored Procedure
alter procedure spGetAllAuthor
@SearchTerm NVARCHAR(50) = NULL
as
begin
select*from Author
where (@SearchTerm IS NULL OR Name LIKE '%' + @SearchTerm + '%')
order by Name
end
--get By Id
ALTER PROCEDURE spGetAuthorById
@AuthorId INT
AS
BEGIN
SELECT * FROM Author WHERE Id = @AuthorId
END
--Create Author
create procedure spCreateAuthor
@AuthorName nvarchar(50) ,@createOn datetime, @NewAuthorId int output
as
begin
set nocount on -- to prevent append this message (1 row(s) affected)
insert into Author(Name,CreateOn) values(@AuthorName,@createOn)
set @NewAuthorId=SCOPE_IDENTITY() -- to return last id was added , dint used @@Idntity becoues its global
-- can return last identity value added on aother execution
-- return null , 0 if operation dint success
end
--Update Author
create Procedure spUpdateAuthor
@AuthorName nvarchar(50) , @NewAuthorId int output
as
begin
set nocount on
update Author
set Name=@AuthorName where Id=@NewAuthorId
end