-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLAdvanced 8.sql
More file actions
132 lines (129 loc) · 4.37 KB
/
SQLAdvanced 8.sql
File metadata and controls
132 lines (129 loc) · 4.37 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
select * from Employee2
Create Procedure spGetTotalCountOfEmployees
@TotalCount int output
as
Begin
Select @TotalCount = COUNT(ID) from Employee2
End
--lets exec
Declare @TotalEMployee int
exec spGetTotalCountOfEmployees @TotalEMployee output
select @TotalEMployee
--use return in sp
Create Procedure spGetTotalCountOfEmployees2
as
Begin
return (Select COUNT(ID) from Employee2)
End
--execute again
Declare @TotalEmployees int
Execute @TotalEmployees = spGetTotalCountOfEmployees2
Select @TotalEmployees
--lets without return
create proc spGetNameById1
@id int,
@name nvarchar(20) output
as
begin
select Name=@name from Employee2 where ID=@id
end
-- exec sp
declare @EmployeeName nvarchar(20)
exec spGetNameById1 3,@EmployeeName output
print 'EmployeeName = ' + @EmployeeName
--lets check table
select * from Employee2
--lets check with return
Create Procedure spGetNameById2
@Id int
as
Begin
Return (Select Name from Employee2 Where ID = @Id)
End
--exec
Declare @EmployeeName nvarchar(20)
Execute @EmployeeName = spGetNameById2 1
Print 'Name of the Employee = ' + @EmployeeName
--So, using return values, we can only return integers, and that too,
--only one integer. It is not possible, to return
--more than one value using return values, where as output parameters, can return any
--datatype and an sp can have more than one output parameters.
--I always prefer, using output parameters, over RETURN values.
--In general, RETURN values are used to indicate success or failure of
--stored procedure, especially when we are dealing with nested stored procedures.
--Return a value of 0, indicates success, and any nonzero value indicates failure.
------------------------------Advantages of SQL SP Proc-------------- :-------------
--The following advantages of using Stored Procedures over adhoc queries (inline SQL)
--1. Execution plan retention and reusability --- Stored Procedures are compiled and their execution plan is
--cached and used again, when the same SP is executed again. Although adhoc queries also
--create and reuse plan, the plan is reused only when the query is textual match and the
--datatypes are matching with the previous call. Any change in the datatype or
--you have an extra space in the query then, a new plan is created.
--2. Reduces network traffic --- You only need to send, EXECUTE SP_Name statement,
--over the network, instead of the entire batch of adhoc SQL code.
--3. Code reusability and better maintainability---- A stored procedure can be reused with
--multiple applications. If the logic has to change, we only have one place to change,
--where as if it is inline sql, and if you have to use it in multiple applications,
--we end up with multiple copies of this inline sql. If the logic has to change,
--we have to change at all the places, which makes it harder maintaining inline sql.
--4. Better Security ------ A database user can be granted access to an SP
--and prevent them from executing direct "select" statements against a table.
--This is fine grain access control which will
--help control what data a user has access to.
select * from Employee2
select * from tblEmployee
create proc spGetNameByid
@id int
as
begin
select name from tblEmployee where id=@id
end
--exec proc
execute spGetNameByid 1
execute spGetNameByid 2
execute spGetNameByid 3
--adhoc sql queries
select name from tblEmployee where id=1
select name from tblEmployee where id=2
select name from tblEmployee where id=3
select Ascii('A')--65
select Ascii('a')--97
select Ascii('z')--122
select CHAR(65)--A
select CHAR(65)--A
declare @start int
set @start=65
while (@start<=90)
begin
print CHAR(@start)
set @start=@start+1
end
----lets check or small character
declare @start2 int
set @start2=97
while (@start2<=122)
begin
print CHAR(@start2)
set @start2=@start2+1
end
--0 to 9
select CHAR(0)--A
select Ascii(0)--48
select Ascii(9)--57
declare @start3 int
set @start3=48
while (@start3<=57)
begin
print char(@start3)
set @start3=@start3+1
end
--ltrim fun
select LTRIM(' ajman')
select RTRIM('ajman ')
create table test6(name nvarchar(20))
insert into test6 values(' ajman'),(' kalim'),
(' zeeshan'),('arif '),('akram '),
('zubair ')
--check table
select * from test6
select LTRIM(name) as ltrname from test6