-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoredProcedure.sql
More file actions
177 lines (149 loc) · 4.42 KB
/
StoredProcedure.sql
File metadata and controls
177 lines (149 loc) · 4.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*1. Write a SP that would accept StartRange and EndRange of BusinessEntityID and return all
the records that fall in that specific range and JobTitle of an employee that exactly
falls in the middle of the range
(Columns Requested : BusinessEntityID, JobTitle, FirstName, LastName)
Also, SP must be encrypted
*/
use AdventureWorks2017
GO
CREATE OR ALTER PROC sp_rangeEmployee (@StartRange INT, @EndRange INT) WITH ENCRYPTION , RECOMPILE
AS
BEGIN
SELECT e.BusinessEntityID, e.JobTitle, p.FirstName, p.LastName
FROM HumanResources.Employee e
JOIN Person.Person p
ON e.BusinessEntityID = p.BusinessEntityID
WHERE e.BusinessEntityID > @StartRange AND e.BusinessEntityID < @EndRange
END
EXEC sp_rangeEmployee @StartRange = 200, @EndRange = 500;
GO
/*
2. Create a SP that will accept a table Name and get me all the column names along with their data-types and sizes.
--(Hints - sys.tables, sys.columns, sys.types, sys.schemas)
select * from sys.tables
select * from sys.columns
select * from sys.schemas
select * from sys.types
*/
CREATE OR ALTER PROC sp_tableWithColumns
AS
BEGIN
SELECT s.name, t.name, c.name, tp.name,
CASE WHEN tp.name IN ('varchar', 'nvarchar', 'char', 'nchar') THEN c.max_length * IIF(t.name IN ('nvarchar', 'nchar'), 2, 1)
WHEN tp.name IN ('decimal', 'numeric') THEN CASE WHEN c.precision > 29 THEN 17 WHEN c.precision > 19 THEN 13 WHEN c.precision > 9 THEN 9 ELSE 5 END
WHEN tp.name = 'money' THEN 8
ELSE c.max_length
END as size
FROM sys.tables t
JOIN sys.schemas s
ON t.schema_id = s.schema_id
JOIN sys.columns c
ON t.object_id = c.object_id
LEFT JOIN sys.types tp
ON c.system_type_id = tp.system_type_id
END
EXEC sp_tableWithColumns
GO
/*
3. Create a SP that will accept Schema Name and Table Name and
return me the corresponding info of their columns based on what user passes the input.
*/
CREATE OR ALTER PROC sp_tableWithColumnsDynamic (@SchemaName VARCHAR(50), @TableName VARCHAR(50))
AS
BEGIN
SELECT s.name, t.name, c.*
FROM sys.tables t
JOIN sys.schemas s
ON t.schema_id = s.schema_id
JOIN sys.columns c
ON t.object_id = c.object_id
WHERE s.name = @SchemaName AND t.name = @TableName
END
EXEC sp_tableWithColumnsDynamic @SchemaName = 'HumanResources', @TableName = 'Employee'
GO
/*
4. Write a SP to print first 50 Fibonacci numbers.
*/
CREATE OR ALTER PROC sp_FibonacciSequence
AS
BEGIN
DECLARE @n1 INT = 0;
DECLARE @n2 INT = 1;
DECLARE @next INT;
DECLARE @c INT = 1;
PRINT @n1
SET @c = @c + 1;
PRINT @n2
WHILE @c < 50
BEGIN
SET @next = @n1 + @n2;
SET @c = @c + 1;
PRINT @next;
SET @n1 = @n2;
SET @n2 = @next;
END;
END;
EXEC sp_FibonacciSequence;
GO
/*
5. Write a SP take a string as an input and check if its Palindrome or not.
*/
CREATE OR ALTER PROC sp_CheckPalindrome (@input VARCHAR(100))
AS
BEGIN
IF LOWER(@input) = REVERSE(LOWER(@input))
BEGIN
PRINT 'Palindrome';
END
ELSE
BEGIN
PRINT 'Not Palindrome';
END
END;
EXEC sp_CheckPalindrome 'Madam'
EXEC sp_CheckPalindrome 'Hello'
GO
/*
6. Write a SP take a number as an input and check if its Armstrong number or not.
*/
CREATE PROCEDURE sp_CheckArmstrong @input INT
AS
BEGIN
DECLARE @original INT = @input;
DECLARE @inputLenght INT = LEN(CAST(@input AS VARCHAR(20)));
DECLARE @sum BIGINT = 0;
DECLARE @digit INT;
WHILE @input > 0
BEGIN
SET @digit = @input % 10;
SET @input = @input / 10;
SET @sum = @sum + POWER(@digit, @inputLenght);
END
-- Check if Armstrong number
IF @Sum = @original
PRINT 'Armstrong';
ELSE
PRINT 'Not Armstrong';
END;
EXEC sp_CheckArmstrong 153;
/*
7. Create a SP that will accept a table Name and get me all the column names along with their data-types and sizes.
*/
GO
CREATE OR ALTER PROC sp_tableWithColumnsQues7 @tableName Varchar(50)
AS
BEGIN
SELECT c.name, tp.name,
CASE WHEN tp.name IN ('varchar', 'nvarchar', 'char', 'nchar') THEN c.max_length * IIF(t.name IN ('nvarchar', 'nchar'), 2, 1)
WHEN tp.name IN ('decimal', 'numeric') THEN CASE WHEN c.precision > 29 THEN 17 WHEN c.precision > 19 THEN 13 WHEN c.precision > 9 THEN 9 ELSE 5 END
WHEN tp.name = 'money' THEN 8
ELSE c.max_length
END as size
FROM sys.tables t
JOIN sys.columns c
ON t.object_id = c.object_id
LEFT JOIN sys.types tp
ON c.system_type_id = tp.system_type_id
WHERE t.name=@tableName
END
EXEC sp_tableWithColumnsQues7 'Employee'