Session 4 - Combine datasets, Stored Procs, Error Checking Flashcards

1
Q

What are the criteria for using UNION or UNION ALL?

A

Same Number of Columns

Same or compatible datatypes

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

What’s the difference between union and union all?

A

Union returns only unique values between two tables and union all returns all values between two tables

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

Where do you put the ORDER BY in a UNION, UNION ALL, INTERSECT, EXCEPT?

A

AT THE END OF THE STATEMENT

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

What does UNION show you?

A

It shows the unique rows in two tables

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

What does UNION ALL show you?

A

It shows all rows in two tables

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

What does INTERSECT show you?

A

It shows all rows that are exactly the same in both tables.

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

What does EXCEPT show you?

A

It shows all rows that DO NOT exist in the second table.

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

Where are the column names set in a UNION, INTERSECT, or EXCEPT statement?

A

In the first select statement

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

What are the two syntax for case?

A

SELECT CASE WHEN @myVar = ‘val’ THEN ‘option’
WHEN @myVar = ‘otherVal’ THEN ‘OtherOption’
ELSE ‘NoOptionsFound’
END

SELECT CASE @myVar WHEN ‘val’ THEN ‘option’
WHEN ‘otherVal’ THEN ‘OtherOption’
ELSE ‘NoOptionsFound’
END

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

What option is not required for a case statement?

A

ELSE

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

Will selecting ISNULL into a table create a nullable column?

A

No, it will create the column as NOT NULL

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

What is the syntax of ISNULL?

A
SELECT ISNULL(@myVar, 'No Option')
It will check to see if variable is null and if so, return the second parameter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the syntax of COALESCE?

A
select coalesce(@myVar1, @myVar2, @myVar3, ..., 'No Option')
It will go through each parameter until it comes to a non null which it will then return.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the datatype gotcha with coalesce?

A

It will go through the results of each parameter and return the highest order data type or error if it cannot return. Make sure that all parameters return compatible data types

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

Can you only return NULL from coalesce?

A

No, you are required to return a datatype other than null

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

What is the syntax of a try statement?

A
BEGIN TRY
STATEMENT
RETURN 0 --To return that it completed successfully
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS ErrorMessage
RETURN 1 --To return that it's an error
END CATCH
17
Q

What is ERROR_SEVERITY()?

A

The severity of the error that is caught:
0-10 for information (Can ignore)
16 is default (Logged in SQL Server log and Windows app log)
20-25 SEVERE stops database critical

18
Q

What is ERROR_LINE()?

A

The line where the error occurred. In a stored procedure, the line numbers start directly after GO

19
Q

What is ERROR_PROCEDURE()?

A

The procedure name where the error occurred.

20
Q

What is ERROR_NUMBER()?

A

The recorded error number that microsoft gives to the error. This can be searched after the fact.

21
Q

What is the syntax of THROW

A

You can use it as is to throw the original error, or the parameters are ERROR_NUMBER, ERROR_MESSAGE, ERROR_STATE (OR 1)

22
Q

What is the syntax of RAISERROR

A

RAISERROR(‘ERROR_MESSAGE’, ERROR_SEVERITY, STATE)

23
Q

In a MERGE statement, what are the two tables named?

A

TARGET (Original table)

SOURCE (New Table)

24
Q

What are the possibilities of MERGE statement?

A

When MATCHED [AND SOMETHING ELSE] then [UPDATE, DELETE]
WHEN NOT MATCHED [BY TARGET] THEN [INSERT]
WHEN NOT MATCHED BY SOURCE [AND SOMETHING ELSE] THEN [UPDATE, DELETE]

25
Q

What is the full syntax of a MERGE statement?

A
MERGE INTO [TARGET TABLE] AS ALIAS1
USING [SOURCE TABLE] AS ALIAS2
ON ALIAS1.COL1 = ALIAS2.COL1
WHEN MATCHED THEN 
    UPDATE SET COL1 = 'VALUE'
WHEN NOT MATCHED BY TARGET THEN
	INSERT (COL1, COL2, COL3)
	VALUES (ALIAS2.COL1, ALIAS2.COL2, ALIAS2.COL3)
[OUTPUT inserted.*, deleted.*]; -- To be able to show columns inserted and deleted
ALWAYS HAVE TO END IT WITH A SEMICOLON
26
Q

What is the syntax for creating a stored procedure?

A
CREATE PROCEDURE (
     @VARIABLE DATATYPE
) AS 
BEGIN
	SET NOCOUNT ON
	QUERY
END
27
Q

What is the syntax for a WHILE statement?

A
WHILE @var1 < @var2
BEGIN
    STATEMENT
    SET @var1 = @var1 + 1
END
28
Q

How do you get a return value from a stored procedure?

A

Add a variable with datatype and OUTPUT in the parameters section:

CREATE PROC ProcName (@var1 int, @OutputVar int OUTPUT) as …

Then when you call the SP, you have to put a variable and output in as well:
DECLARE @OutVar
EXEC ProcName 123, @OutVar OUTPUT

29
Q

How can you tell if a procedure has ended in success or failure?

A

use RETURN 0 for success and RETURN 1 for failure:

CREATE PROCEDURE DBO.MYPROCEDURE (@ABNUM INT, @OUTVAR INT OUTPUT) AS
BEGIN 
	IF EXISTS(SELECT * FROM JDE_PRODUCTION.PRODDTA.F0101 WHERE ABAN8 = @ABNUM)
	BEGIN
		SELECT * FROM JDE_PRODUCTION.PRODDTA.F0101 WHERE ABAN8 = @ABNUM
		SET @OUTVAR = @@ROWCOUNT
		RETURN 0
	END
	ELSE
		SET @OUTVAR = 0
		RETURN 1
END

DECLARE @OUTPT INT
EXEC @OUTPUT = DBO.MYPROCEDURE 999998
SELECT @OUTPT

30
Q

What are the functions for seeing the specifics of an error?

A
ERROR_MESSAGE()
ERROR_LINE()
ERROR_NUMBER()
ERROR_PROCEDURE()
ERROR_SEVERITY()
ERROR_STATE()