Quote:
|
Originally Posted by karthi_syb
declare @example varchar(15)
set @example = "welcome"
select *
from <tablename>
where column1 = @example
|
There are several ways to do this.
First of all, you'll definitely want single quotes around "welcome".
According to the reference manual (SC23-5862), you have the DECLARE VARIABLE statement inside a BEGIN ATOMIC (p. 213), the CREATE VARIABLE statement (p. 600), the "SET variable" statement (p. 954) and the "VALUES" statement (p.991) available.
Try one of the following, depending on the environment (embedded SQL, GUI, command line interface, inside a procedure definition) where you want this:
In the "script" GUI:
Code:
SET example = 'welcome' ;
SELECT *
FROM tablename
WHERE column1 = example ;
END
Inside a procedure, function, or trigger definition, or in a dynamic SQL statement (embedded in a host program):
Code:
BEGIN ATOMIC
DECLARE example VARCHAR(15) ;
SET example = 'welcome' ;
SELECT *
FROM tablename
WHERE column1 = example ;
END
or (in any environment):
Code:
WITH t(example) AS (VALUES('welcome'))
SELECT *
FROM tablename, t
WHERE column1 = example
or (although this is probably not what you want, since the variable needs to be created just once, but can be used thereafter by everybody although its content will be private on a per-user basis):
Code:
CREATE VARIABLE example VARCHAR(15) ;
SET example = 'welcome' ;
SELECT *
FROM tablename
WHERE column1 = example ;