Advanced SQL Tutorial | Stored Procedures + Use Cases

Advanced SQL Tutorial | Stored Procedures + Use Cases

Alex The Analyst

3 года назад

397,333 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Bryan
Bryan - 28.09.2023 18:51

It's funny how most SQL tutorials I've encountered use the employee names from The Office haha

Ответить
Anupam naidu
Anupam naidu - 24.09.2023 15:57

Thanks buddy very informative

Ответить
Rajkumar Palle
Rajkumar Palle - 19.09.2023 11:47

It was nice video and easy to understand. Thank you so much

Ответить
PRAKYA SRI CHILUKURI
PRAKYA SRI CHILUKURI - 14.09.2023 23:06

You have not mentioned that after modifying the stored procedure we need to execute it in order to ensure the modified stored procedure is the one that runs when we do EXEC Test_Employee

Ответить
Natalia Henke da Cruz
Natalia Henke da Cruz - 13.09.2023 15:55

deus te abençoe

Ответить
Islam Mounier
Islam Mounier - 10.09.2023 16:56

this video was a little bit complicated for me :(

Ответить
didi
didi - 30.08.2023 16:14

ok. so immediately after creating my stored procedure, i ran into the problem of it not being in the right database even though i did everything correctly and without syntax errors. if you also have this problem, i'm writing this comment to (hopefully) help. sorry, it'll be a long read.
so! like i said my stored procedure was not in my SQL Tutorial database as intended and after some searching through my own files still nothing. i read a comment saying that someone found their stored procedure in the master database, so i checked in there and there it was! so if you haven't been able to locate your own, yours may be there. after locating it, i looked up how to move the stored procedure to the right database and the steps i followed are here with my notes in brackets:

1. Go the server in Management Studio
2. Select the database, right click on it Go to Task.
3. Select generate scripts option under Task
4. and once its started select the desired stored procedures you want to copy [for this step i chose to just open the script in a new query window so i didn't have to download a file and work more quickly, but choose the method that is best for you)


now once you have the script of the stored procedure pulled up, look to the very top at (what i guess to be) the USE statement. the database in which the stored procedure is in will be in brackets. go to the USE and change the name of the database to the one in which you want to be the location of the stored procedure. once you change the database as needed, execute the whole thing and you should be good to go! be sure to check for the location and refresh as needed to make sure everything went smoothly before moving on. please note that these steps may not be helpful if you made a typo or anything, so check your query that created the stored procedure to see where you may have an issue. i suppose in the future, for creating objects such as stored procedures and anything else that needs a location specified, placing that USE statement when you first write the query may be the best course of action. i'm still new to this so there may be better ways to do this. at any rate, i hope this helps!! :)

edit: an additional note! i learned that to specify the database location, you must use GO directly after the USE statement, otherwise you'll get an error saying that the CREATE statement must be first (not totally sure what the GO does in this case, if anyone knows please share! :) ) so when you're creating your stored procedure for the temp table, write this right before your CREATE statement:
USE [ ]
GO

your desired database will go in the brackets of course :) then you should be good to go to continue writing your query!

edit 2: I double checked in my master database and saw i had a copy of my TEST stored procedure, so i just deleted them once i made sure i moved them to my desired database. so do with these copies as you see fit!

Ответить
Veronika Sherriuble
Veronika Sherriuble - 28.08.2023 21:29

I was slightly struggling with it to follow along so here is the steps to actually get the same result:
1) You write a stored procedure: drop table if exists, create table, inset into and select - finish with GO (you can also wrap the whole thing in: BEGIN (all queries) END
2) Run what you wrote (all of it)
3) Exec [store procedure name]
4) Modify - add parameter
5) Run the whole thing again ( I mean the modify query)
6) Exec [store procedure name] [your parameter]

Ответить
Ezeh Confidence Adaeze
Ezeh Confidence Adaeze - 20.08.2023 09:36

Thank you Alex. Your videos are literally the best. You came down to the beginners level and walked us from ground to top.

Ответить
beyond
beyond - 07.08.2023 16:23

After creating the procedure, it doesn't show on stored procedures.

Ответить
GoodLv
GoodLv - 16.07.2023 07:51

Why is it that my stored procedures didn't result in a "table" but it did show that the command was successful?
Any thoughts?

Ответить
Sheila Wanindya
Sheila Wanindya - 10.07.2023 17:47

Hello. Can someone explain what '@' means? when, how and where Do we need to attached it? Thanks before.

Ответить
aaronl03
aaronl03 - 04.07.2023 02:56

Hey Alex great video! I was following along and wondered what the differences are when using Postgresql as I tried to run the stored procedure and it didnt work. So how would I be able to create a procedure temp table in postgre or would I just have to create a function instead?

Ответить
Karina
Karina - 02.07.2023 10:11

How could I do to alter the procedure using mysql? I didn't manage to adjust the syntax.

Ответить
Tinah
Tinah - 29.06.2023 17:36

Hello! When I create 'Stored Procedure', it is created in the Master Database, is there any way to stop this?

Ответить
EastLhama
EastLhama - 22.06.2023 04:12

Love the database from The Office

Ответить
Víctor Díaz Cortés
Víctor Díaz Cortés - 20.06.2023 02:45

Awesome! Thank you!

Ответить
Edima Thomas
Edima Thomas - 17.06.2023 00:23

Great tutorial. Practicing this on azure and I keep getting this error: 'CREATE PROCEDURE' must be the only statement in the batch. I don't know what it means. Can anyone help?

Ответить
subi v
subi v - 13.06.2023 20:05

hey, thanks for sharing this video, I've did all the steps, but still after refreshing I can't able to find the Test in stored procedures, is there anything I can do to rectify.

Ответить
FAUZAN
FAUZAN - 08.06.2023 13:01

Thanks. Nice explaination

Ответить
Simon Hsieh
Simon Hsieh - 08.06.2023 04:56

I typed your initial create procedures code exactly, but it gives me this message. I can't seem to figure out the issue. How would you resolve it?

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS Select* From employeedemographics' at line 2

Ответить
Che Fernandez
Che Fernandez - 02.06.2023 14:32

i have no idea what you did to execute the stored procedure lol when i exec my procedure it shows that it is somewhere on my pc its just not showing

Ответить
💫Zroburn💫
💫Zroburn💫 - 31.05.2023 23:25

This has been an amazing tool to help fortify what I have already learned. My question would be what would cause the opposite to happen? I pulled the code from your GitHub, ran what you ran, and made the same changes. I did this under SQLTutorial instead of Master. Would that cause this to happen?

Ответить
Aaron Wong
Aaron Wong - 25.05.2023 16:36

Thank you so much for the tutorial. I really appreciate it. I have a question. After I add a parameter to the procedure and execute the procedure again, the result is the same as the procedure without a parameter. No error or warning. It seems that my parameter didn't come into effect. What should I do? Thank you!

Ответить
Niklas Martinsson
Niklas Martinsson - 23.05.2023 00:02

Thank you so much, this video was very helpful. I think Stanley feels a little young however. 10/10

Ответить
Aldrin Palcotilo
Aldrin Palcotilo - 14.04.2023 12:21

even i refresh the stored procedures there is no data appear in stored procedure where did the data go also i created another data temp _employee i cant see the data in stored procedures what shoud i do?

Ответить
Anthony D
Anthony D - 06.04.2023 00:53

Thanks Alex

Ответить
Juniors Pakistan
Juniors Pakistan - 03.04.2023 19:18

I am following this series from beginning and now this is the first video that i find difficult to understand 😐 still learning....

Ответить
wolfganghammerfistheywood
wolfganghammerfistheywood - 28.03.2023 18:16

Jesus these things are a walk in the park in SQL and an absolute nightmare in Teradata. Theres pretty much zero syntax documentation.

Ответить
JustAnalyze
JustAnalyze - 28.03.2023 10:05

Why are my stored procedures not showing, please help. :(

Ответить
wolfganghammerfistheywood
wolfganghammerfistheywood - 23.03.2023 03:35

Huh, its literally just a function. Kinda figured there'd be more to these than that.

Ответить
Partha Sarker
Partha Sarker - 22.03.2023 13:13

Im using Postgresql -

I can create the following function to retrieve all the data from the EmployeeDemographics table:

CREATE OR REPLACE FUNCTION test()
RETURNS TABLE (EmployeeID integer, FirstName varchar(50), LastName varchar(50), Age integer, Gender varchar(50))
AS $$
BEGIN
RETURN QUERY SELECT * FROM EmployeeDemographics;
END;
$$ LANGUAGE plpgsql;

But when I try to create a Stored procedure like this:

CREATE OR REPLACE PROCEDURE TEST()
RETURNS TABLE (EmployeeID integer, FirstName varchar(50), LastName varchar(50), Age integer, Gender varchar(50))
AS $$
BEGIN
RETURN QUERY SELECT * FROM EmployeeDemographics;
END;
$$ LANGUAGE plpgsql;

It fails to execute.

ChatGPT states since I'm using Postgres:

"The code for creating a stored procedure in PostgreSQL is correct, except that in PostgreSQL, stored procedures are called functions, not procedures."

What am I doing that is syntactically that is not allowing me to create a stored procedure in Postgres?

Ответить
Andrew Oyarero
Andrew Oyarero - 12.03.2023 01:40

Hello Alex - I couldn’t modify the storage procedure because I couldn’t find it under programmability, though it executes just fine.

Ответить
Afghanistanomy
Afghanistanomy - 07.03.2023 22:04

Hi everyone,

I created the procedure, however it is not stored in stored procedures although I refreshed it again and again but it did not work. I searched online for solution but could not find it. I would appreciate any help here.

Ответить
William Huynh
William Huynh - 05.03.2023 03:07

Will you be doing Triggers at all?

Ответить
Johan Cetre
Johan Cetre - 02.03.2023 21:08

For those using SQLite, this DBMS doesn’t support Stored Procedures. For people using MySQL, the Stored Procedures is quite different. Below is the way how I did the first and second examples. Finally, there is no way to modify the parameters or the body stored procedure in MySQL, so you must drop it and create another one using DROP PROCEDURES IF EXISTS

Example 1
DELIMITER &&
CREATE PROCEDURE TEST ()
BEGIN
SELECT * FROM Employee;
END &&
DELIMITER ;
CALL TEST;

Example 2
DELIMITER &&
CREATE PROCEDURE Test ()
BEGIN
DROP TABLE IF EXISTS Temp_Employee;
CREATE TEMPORARY TABLE Temp_Employee (JobTitle VARCHAR (50), EmployeeperJob VARCHAR (50), Avg_Age INT, Avg_Salary INT );
INSERT INTO Temp_Employee
SELECT JobTittle, COUNT(JobTittle), AVG (Age), AVG (Salary)
FROM Employee emp INNER JOIN EmployeeSalary sal ON emp.EmployeeID = sal.EmployeeID
GROUP BY JobTittle;
SELECT * FROM Temp_Employee;
END &&
DELIMITER ;
CALL Test;

EXEC is replaced by CALL here.

Ответить
Queen Sam
Queen Sam - 26.02.2023 22:38

Stored Procedures are somewhat complex. In what context would we need to use it?

Ответить
Content Room
Content Room - 21.02.2023 19:10

So far partition and stored procedures are the 2 videos that I found difficult. But I've learned a lot from this series. Thanks, Alex!

Ответить
Samuel Raj
Samuel Raj - 13.02.2023 14:18

Done

Ответить
Coder
Coder - 12.02.2023 20:15

I'm looking to optimize where clause using udt and if I can switch from using (not exists(select 1 from @param) or exists (select 1 from @param where value = mainqueryvalue)). Any suggestions?

Ответить
Braimah Fadilat
Braimah Fadilat - 11.02.2023 16:25

Your Tutorials are amazing

Ответить
A Wimbley
A Wimbley - 02.02.2023 01:40

I will have to review and practice this one many times. I was asked in an interview if I wrote stored procedures. It was not something I did. Now I will practice based on what you have shown here. Also, how do you reference more than one parameter. Do you add another Where clause, use AND, ?????

Ответить
Rudeboy
Rudeboy - 14.01.2023 16:06

What the... this went a little faster than I expected. Lost track after you wanted to create the Temp_Employee 😅

Time to rewatch

Ответить
bijay kumar Pokhrel
bijay kumar Pokhrel - 02.12.2022 06:25

Thank you very much for your excellent tutorial video clips. I regularly watch these clips, and they are incredibly beneficial. I am new to the store procedure. Any help creating an SQL server store procedure connecting two servers would be highly appreciated.

Ответить
123k
123k - 21.11.2022 21:54

Thank you Alex.

Ответить