W3. SQL Stored Procedures Flashcards

1
Q

Q: What is a stored procedure in SQL?

A

A: It’s a prepared SQL code that can be saved and reused, allowing you to execute complex queries repeatedly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: Why would you use a stored procedure?

A

A: To avoid rewriting SQL queries and to execute complex SQL statements with a single call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: Can you pass parameters to a stored procedure?

A

A: Yes, parameters can be passed so the procedure can operate based on specific values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: Write the syntax to create a stored procedure.

A

CREATE PROCEDURE procedure_name
AS
sql_statement
GO;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: How do you execute a stored procedure?

A

A: Use EXEC procedure_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: Write a stored procedure to select all records from Customers, named “SelectAllCustomers”.

A

CREATE PROCEDURE SelectAllCustomers
AS
SELECT * FROM Customers
GO;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: How would you execute the stored procedure “SelectAllCustomers”?

A

EXEC SelectAllCustomers;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: How do you create a stored procedure with one parameter?

A

A: Define the parameter in the procedure, e.g.,

CREATE PROCEDURE procedure_name @Parameter datatype
AS
sql_statement;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: Write a stored procedure to select customers from a specific city, using @City as a parameter.

A

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30)
AS
SELECT * FROM Customers WHERE City = @City
GO;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: How would you execute the “SelectAllCustomers” procedure with @City = ‘London’?

A

EXEC SelectAllCustomers @City = ‘London’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: How do you create a stored procedure with multiple parameters?

A

A: Define each parameter with a datatype, separated by commas, e.g.,

CREATE PROCEDURE procedure_name @Param1 datatype, @Param2 datatype;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: Write a stored procedure to select customers from a specific city and postal code, using @City and @PostalCode as parameters.

A

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode = @PostalCode
GO;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: How would you execute the “SelectAllCustomers” procedure with @City = ‘London’ and @PostalCode = ‘WA1 1DP’?

A

EXEC SelectAllCustomers @City = ‘London’, @PostalCode = ‘WA1 1DP’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly