What is the best practice for defining variables in sql? I've seen some of the other posts here and other places, but nothing I've tried as of yet has worked.
I'm using the below syntax, but the horse doesn't like the hay.
declare @variablename number(10) set @variablename = (select COLUMN_NAME from TABLE_NAME where ANOTHER_COLUMN_NAME='something') select MORE_COLUMN_NAMES from "NAME" where ANOTHER_NAME=@variablename;
This gives me some errors starting at "declare."
Error report - ORA-06550: line 1, column 9: PLS-00103: Encountered the symbol "@" when expecting one of the following: begin function pragma procedure subtype type "an identifier" "a double-quoted delimited-identifier" current cursor delete exists prior The symbol "@" was ignored.
I'm just starting out with very little database knowledge in so far as the application of things. I'm trying to become more versed in syntax and use.
Any help would be appreciated.
A PL/SQL block with variable declaration should be something like:
declare
var number;
var2 number;
begin
select count(1)
into var
from dual;
var2 := var * 2;
dbms_output.put_line('Var2 = ' || var2);
end;
In SQLPlus you can use bind variables:
variable var number
select count(1) into :var from dual;
select * from dual where rownum = :var;
or even substitution variables:
column var new_value valueFor_var
select count(1) as valueFor_var from dual;
select * from dual where rownum = &var;