SET
Example
Section titled “Example”SET @name = 'John Doe';SELECT @name;Output
Section titled “Output”+-----------+| @name |+-----------+| John Doe |+-----------+Explanation
Section titled “Explanation”The SET command in the SQL statement is used to assign a value to the variable @name. The SELECT command is then executed to display the value of the @name variable, which in this case is ‘John Doe’.
Example
Section titled “Example”SET timezone TO 'America/New_York';SHOW timezone;Output
Section titled “Output” TimeZone--------------------------- America/New_YorkExplanation
Section titled “Explanation”The SET statement is used to change the current setting of a run-time configuration option for the duration of the current session. The example above is changing the timezone to ‘America/New_York’. The SHOW command is displaying the current set timezone.
Example
Section titled “Example”DECLARE @TestVariable INT;SET @TestVariable = 5;SELECT @TestVariable;Output
Section titled “Output”5Explanation
Section titled “Explanation”The variable @TestVariable is declared to be of type INT. Subsequently, the SET statement assigns the value 5 to @TestVariable. The SELECT statement then retrieves the value, outputting 5.
Example
Section titled “Example”SET SERVEROUTPUT ONDECLARE test_variable NUMBER(2);BEGIN test_variable := 15; dbms_output.put_line('The value of test_variable is ' || test_variable);END;/Output
Section titled “Output”The value of test_variable is 15Explanation
Section titled “Explanation”In the example code, the SERVEROUTPUT feature of SQL*PLUS is enabled with the SET SERVEROUTPUT ON command. This allows the DBMS_OUTPUT.PUT_LINE to function. Within a PL/SQL block, a variable named test_variable of NUMBER type is declared and set to 15. The dbms_output.put_line command prints the appended string and the value of test_variable which gives us our output.