on ‎2022 Dec 07 9:42 PM
I am trying to build a SQL query on a table where I use a variable for the table fieldname in teh where clause e.g,
var1 VARCHAR(10) ;
var1 := '"FIELD1"';
select * from TABLE1 where :var1 = 'X' ;
It's not recognising var1 as FIELD1 from TABLE1, can anyone help?
Request clarification before answering.
derekjones
This is not possible in SQL. You cannot use variables in the WHERE clause. You will need to use dynamic SQL (i.e. using EXECUTE IMMEDIATE) to achieve this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Michael above has suggested this, but I cannot get it working in my SQl Calc view..
/********* Begin Procedure Script ************/
ZZBLKRSNT varchar(16);
BEGIN
ZZBLKRSNT := '"ZZBLK_RSN_01"';
EXECUTE IMMEDIATE 'LT_ROOT = SELECT "MANDT", "VBELN", "ERDAT" from "SAPEED"."VBAK" WHERE ' || :ZZBLKRSNT || ' = ''X'' ';
LT_FINAL =
SELECT * from :LT_ROOT ;
var_out = SELECT * from :LT_FINAL;
END
/********* End Procedure Script ************/
It fails at SELECT * from :LT_ROOT as it cannot find LT_ROOT.
Any assistance/advice would be most appreciated
| User | Count |
|---|---|
| 8 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.