Using variable in select statement (php)
I am having difficulty using a variable in a select
statement.
The following manual query (using a static date) works fine:
$query_Recordset1 = "SELECT * FROM academyClasses WHERE
classDate > '2006-06-01' ";
However, If I use the following:
$dateVar = date('Y-m-d');
mysql_select_db($database_mw, $mw);
$query_Recordset1 = "SELECT * FROM academyClasses WHERE
classDate > $dateVar ";
ALL records are returned, which means the $dateVar variable
is not being recognized. I have compared the $dateVar values
<?php echo $datetime; ?> against the actual value of my date
field <?php echo $row_Recordset1['classDate']; ?> in my
results table and it seems that the values are indeed accurate in
terms of a date 2006-06-04 for example.
My date field "classDate" is of a "date" type within the
mysql database. I'm using Dreamweaver MX 2004 with Mac OSX 10.3.9.
I'm sure this is just a syntax problem, at least I hope it
is.
Any help is greatly appreciated.
On Wed, 7 Jun 2006 04:49:57 +0000 (UTC), "mgwaters"
<[email protected]> wrote:
>Thanks Gary. That did seem to get the select statement
working, but I had to
>enter a manual date as before $dateVar = '2006-06-01'; on
the previous line of
>code to actually get it to filter my records. So... it
looks like my setting of
>the date variable:
> $dateVar = date('Y-m-d');
> does not seem to be recognized within the SELECT
statement.
Try this:
$query_Recordset1 = "SELECT * FROM academyClasses WHERE
classDate>'$dateVar'";
print $query_Recordset1;
See what is in the SQL statement.
Gary
Similar Messages
-
Bind Variable in SELECT statement and get the value in PL/SQL block
Hi All,
I would like pass bind variable in SELECT statement and get the value of the column in Dynamic SQL
Please seee below
I want to get the below value
Expected result:
select distinct empno ,pr.dept from emp pr, dept ps where ps.dept like '%IT' and pr.empno =100
100, HR
select distinct ename ,pr.dept from emp pr, dept ps where ps.dept like '%IT' and pr.empno =100
TEST, HR
select distinct loc ,pr.dept from emp pr, dept ps where ps.dept like '%IT' and pr.empno =100
NYC, HR
Using the below block I am getting column names only not the value of the column. I need to pass that value(TEST,NYC..) into l_col_val variable
Please suggest
----- TABLE LIST
CREATE TABLE EMP(
EMPNO NUMBER,
ENAME VARCHAR2(255),
DEPT VARCHAR2(255),
LOC VARCHAR2(255)
INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (100,'TEST','HR','NYC');
INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (200,'TEST1','IT','NYC');
INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (300,'TEST2','MR','NYC');
INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (400,'TEST3','HR','DTR');
INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (500,'TEST4','HR','DAL');
INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (600,'TEST5','IT','ATL');
INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (700,'TEST6','IT','BOS');
INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (800,'TEST7','HR','NYC');
COMMIT;
CREATE TABLE COLUMNAMES(
COLUMNAME VARCHAR2(255)
INSERT INTO COLUMNAMES(COLUMNAME) VALUES ('EMPNO');
INSERT INTO COLUMNAMES(COLUMNAME) VALUES ('ENAME');
INSERT INTO COLUMNAMES(COLUMNAME) VALUES ('DEPT');
INSERT INTO COLUMNAMES(COLUMNAME) VALUES ('LOC');
COMMIT;
CREATE TABLE DEPT(
DEPT VARCHAR2(255),
DNAME VARCHAR2(255)
INSERT INTO DEPT(DEPT,DNAME) VALUES ('IT','INFORMATION TECH');
INSERT INTO DEPT(DEPT,DNAME) VALUES ('HR','HUMAN RESOURCE');
INSERT INTO DEPT(DEPT,DNAME) VALUES ('MR','MARKETING');
INSERT INTO DEPT(DEPT,DNAME) VALUES ('IT','INFORMATION TECH');
COMMIT;
PL/SQL BLOCK
DECLARE
TYPE EMPCurTyp IS REF CURSOR;
v_EMP_cursor EMPCurTyp;
l_col_val EMP.ENAME%type;
l_ENAME_val EMP.ENAME%type;
l_col_ddl varchar2(4000);
l_col_name varchar2(60);
l_tab_name varchar2(60);
l_empno number ;
b_l_col_name VARCHAR2(255);
b_l_empno NUMBER;
begin
for rec00 in (
select EMPNO aa from EMP
loop
l_empno := rec00.aa;
for rec in (select COLUMNAME as column_name from columnames
loop
l_col_name := rec.column_name;
begin
l_col_val :=null;
l_col_ddl := 'select distinct :b_l_col_name ,pr.dept ' ||' from emp pr, dept ps where ps.dept like ''%IT'' '||' and pr.empno =:b_l_empno';
dbms_output.put_line('DDL ...'||l_col_ddl);
OPEN v_EMP_cursor FOR l_col_ddl USING l_col_name, l_empno;
LOOP
l_col_val :=null;
FETCH v_EMP_cursor INTO l_col_val,l_ename_val;
EXIT WHEN v_EMP_cursor%NOTFOUND;
dbms_output.put_line('l_col_name='||l_col_name ||' empno ='||l_empno);
END LOOP;
CLOSE v_EMP_cursor;
END;
END LOOP;
END LOOP;
END;user1758353 wrote:
Thanks Billy, Would you be able to suggest any other faster method to load the data into table. Thanks,
As Mark responded - it all depends on the actual data to load, structure and source/origin. On my busiest database, I am loading on average 30,000 rows every second from data in external files.
However, the data structures are just that - structured. Logical.
Having a data structure with 100's of fields (columns in a SQL table), raise all kinds of questions about how sane that structure is, and what impact it will have on a physical data model implementation.
There is a gross misunderstanding by many when it comes to performance and scalability. The prime factor that determines performance is not how well you code, what tools/language you use, the h/w your c ode runs on, or anything like that. The prime factor that determines perform is the design of the data model - as it determines the complexity/ease to use the data model, and the amount of I/O (the slowest of all db operations) needed to effectively use the data model. -
Using procedure in SELECT statement
I have a select statement that currently uses 4 functions to receive necessary values. All the functions are recursive and returns values from the same row.
What I would like to do is replace these for function calls with 1 procedure. Does anybody know if it possible to use a procedure in this way inside a select statement?
If so, do you have the syntax for doing this?
E.g
SELECT
Mdbrd_Pkg.calculate_fixed_charge_fn(in_rc_id, ap.CONFIGSET_ID) AS FIXED_CHARGE,
Mdbrd_Pkg.calculate_charge_rate_fn(in_rc_id, ap.CONFIGSET_ID) AS CHARGE_RATE,
Mdbrd_Pkg.tax_liable_fn(in_rc_id, ap.CONFIGSET_ID) AS TAX_LIABLE,
Mdbrd_Pkg.charge_unit_fn( in_rc_id, ap.CONFIGSET_ID) AS CHARGEUNIT_ID
FROM .....This cannot be done. The part of the function used in the SELECT statement is the return value: procedures don't have return values (that's what makes tham procedures and not functions).
Obviously I don't know what your code does, but you should consider putting them into a single function that returns a TYPE with four attributes and then using the TABLE() function to cast them into something you could reference in the FROM clause of a correlated sub-query. Sounds a bit messy though.
Do these functions actually select data? Where does the recursion fit in?
Cheers, APC -
Getting error after putting variable in select statement
Hi All,
Kindly help me with Pl/sql code
here i'm trying to put variable in select statement ....
I successfully put the Yr and Mnth ..but getting error for loop variable "i"
Here i'm getting error that too because of " i" only-->TRIM(UPPER(TO_CHAR(TO_DATE(i||'-'||Mnth||'-'||Yr,'dd-Mon-YYYY'),'DAY')))
Please find the full code below.
Declare
Yr number not null:=2010;
Mnth varchar2(20) not null:='Jun';
v_val number:=0;
begin
for i in 1..2 loop
case i
SELECT count(*) into v_val
FROM DUAL
WHERE TRIM(UPPER(TO_CHAR(TO_DATE(i||'-'||Mnth||'-'||Yr,'dd-Mon-YYYY'),'DAY')))
in (UPPER('Monday'),UPPER('Tuesday'),UPPER('WEDNESDAY'),UPPER('THURSDAY'),UPPER('friday'))
end case;
end loop;
end;Declare
Yr number not null:=2010;
Mnth varchar2(20) not null:='Jun';
v_val number:=0;
begin
for i in 1..2 loop
SELECT count(*) into v_val
FROM DUAL
WHERE TRIM(UPPER(TO_CHAR(TO_DATE(i||'-'||Mnth||'-'||Yr,'dd-Mon-YYYY'),'DAY')))
in (UPPER('Monday'),UPPER('Tuesday'),UPPER('WEDNESDAY'),UPPER('THURSDAY'),UPPER('friday'));
end loop;
end; -
Why I Can't use procedure in select statement
Why I Can't use procedure in select statement
We can use function in select statement but we couldn't use procedure with one out parameters in select statement... You can use Function because they are designed for this but procedure are not. Functions can return value (without OUT parameter) which can be used in SELECT whereas procedures do not have such concept. As you can see in the above post you can not call even functions also if it has any out parameter.
I have just trying to use procedure in select statement ..for this I require technical answer..The technical answer is because conceptually procedure is for doing set of operation, performning DMLs on the tables , whereas functions are for processing and producing a single result. Functions are basically for not using INSERT/UPDATE/DELETE in it. That is the reason they are allowed to be used in SELECT because conceptually they are not supposed to do any data changes.
Regards,
Avinash -
How to use column name as variable in select statement
hi,
i want to make a sql query where in select statement using variable as a column name. but its not working plz guide me how can i do this.
select :m1 from table1;
regardsHi,
Is this what you want..
SQL> select &m1 from dept;
Enter value for m1: deptno
old 1: select &m1 from dept
new 1: select deptno from dept
DEPTNO
10
20
30
40
SQL> select &m1 from dept;
Enter value for m1: dname
old 1: select &m1 from dept
new 1: select dname from dept
DNAME
ACCOUNTING
RESEARCH
SALES
OPERATIONS
SQL> select &&m1 from dept;
Enter value for m1: loc
old 1: select &&m1 from dept
new 1: select loc from dept
LOC
NEW YORK
DALLAS
CHICAGO
BOSTON
SQL> select &&m1 from dept;
old 1: select &&m1 from dept
new 1: select loc from dept
LOC
NEW YORK
DALLAS
CHICAGO
BOSTONIf you use single '&' then each time you fire the query, It will ask for the new value..
But if you will use double '&&' the value of m1 will be persistent across the session..
Twinkle -
Variable in Select statement JDBC sender
Hi All,
I am working on JDBC sender and this scenario is working fine. Now the legacy system has different enviroment for development, quality and production so I need to change the enviroment for each system.
Is there any way I can make it as variable and the movment it system checks it's development of XI system then it should pick up right enviroment for legacy system and that can be used in select statement.
My select statement looks like and highlighted one is the enviroment which needs to be a variable.
select digits(HMOUTLT) as HMOUTLT, hmprodu as HMPRODU, HMCHKOQ as HMCHKOQ, HMCHKiQ as HMCHKIQ, HMWKPER as HMKPER, HMLSDAT as HMLSDAT from bi5filmm.bsimhp where hmprflg = ' ' and rrn(bsimhp) < 3000 and hmextdt = '2007-12-24'.
I am not sure if value mapping can be used here or not.
regards
Riasolved
-
Using function in select statement
Hi,
CREATE FUNCTION [dbo].[udf_testFunction]
@value int
RETURNS int
AS
BEGIN
-- Declare the return variable here
declare @returnValue int
set @returnValue = @value*2;
return @returnValue;
END
GO
create table #Temp
EmpID int,
EmpName Varchar(50)
insert into #Temp(EmpID,EmpName) values(1,'Name1');
insert into #Temp(EmpID,EmpName) values(2,'Name2');
insert into #Temp(EmpID,EmpName) values(3,'Name3');
insert into #Temp(EmpID,EmpName) values(4,'Name4');
insert into #Temp(EmpID,EmpName) values(5,'Name5');
select EmpID,EmpName, [dbo].[udf_testFunction](EmpID), [dbo].[udf_testFunction](EmpID)*EmpID,[dbo].[udf_testFunction](EmpID)+2 from #Temp
In the above select statement i used [dbo].[udf_testFunction]() function 3 times. But i want to use only once and reuse it.
Something like
select EmpID,EmpName, testfunctionvalue,testfunctionvalue*EmpID,testfunctionvalue+2 from #Temp
Please advise me.... Thanks in Advance...You can use this code:
WITH cte
AS ( SELECT EmpID ,
EmpName ,
[dbo].[udf_testFunction](EmpID) AS testfunctionvalue
FROM #Temp
SELECT EmpID ,
EmpName ,
testfunctionvalue ,
testfunctionvalue * EmpID ,
testfunctionvalue + 2
FROM cte
But using scalar functions in select clause can hurt the performance. Please see this link:
SQL Server Scalar User Defined Function Performance
T-SQL Articles
T-SQL e-book by TechNet Wiki Community
T-SQL blog -
PL SQL using variable in SQL statement
I am trying to execute several sql statements, that have the same format but different values. Does this
mean I need a bind variable?
ie
select TO_CHAR ( (SYSDATE - 2), 'YYYY_MM_DD') from dual
select TO_CHAR ( (SYSDATE - 4), 'YYYY_MM_DD') from dual
select to_char(add_months(sysdate,-2*1) from dual
When I try to put the values into a varaiable (date, varchar2 or number) I am getting a conversion
error.
Can somebody show me an example of how to do something like this? Or at least point me to the correct
part of the documentation that provides and example. Pardon my syntax as I know it is incorrect
val :=add_months(sysdate,-2*1
select to_char(val) from dual
Thanks in advance to all who answerHi,
840386 wrote:
I am trying to execute several sql statements, that have the same format but different values. Does this
mean I need a bind variable?No, you don't need a bind variable, though bind variables may be more efficient than using PL/SQL variables. I don't see where you're trying to use any varibables at all in your examples. Is it in place of the literals, such as 2 or 'YYYY_MM_DD'? You can use either bind varibales or PL/SQL variables in those places.
ie
select TO_CHAR ( (SYSDATE - 2), 'YYYY_MM_DD') from dual
select TO_CHAR ( (SYSDATE - 4), 'YYYY_MM_DD') from dual
select to_char(add_months(sysdate,-2*1) from dualIn PL/SQL, when you have a SELECT statement, you must specify what you want to do with the results. For example, a SELECT ... INTO statement:
SELECT AVG (sal)
INTO avg_salary
FROM scott.emp;There's usually no point in SELECTing from dual in PL/SQL. It's not an error, but it's simpler just to use an assignment statement.
When I try to put the values into a varaiable (date, varchar2 or number) I am getting a conversion
error.Post a complete procedure or anonymous block, including variable declarations, that shows exactly what you're trying to do.
>
Can somebody show me an example of how to do something like this? Or at least point me to the correct
part of the documentation that provides and example. Pardon my syntax as I know it is incorrect
val :=add_months(sysdate,-2*1Assuming val is a DATE, that's basically correct. You have unbalanced parentheses (there's a '(', but no matching ')' ), and you need a semicolon (';') at the end of the statement. Perhaps ');' just got cut off when you were posting this.
select to_char(val) from dualAgain, SELECTing from dual is unnecessary, but if you had some way to catch the returned value, that would work.
Usually, the reason why you need to call TO_CHAR is that you want a value in a particular format, which is specified in the 2nd argument to TO_CHAR. Calling TO_CHAR with only one argument is a possible mistake, but not something that would raise an error.
Here's an example that works:
SET SERVEROUTPUT ON
DECLARE
d DATE;
v VARCHAR2 (30);
BEGIN
d := ADD_MONTHS (SYSDATE, -2); -- -2 = (-2*1)
v := TO_CHAR (d, 'DD-Mon-YYYY');
dbms_output.put_line (v || ' = v');
END;
/Output (when run on March 13, 2011):
13-Jan-2011 = v -
Passing a variable to select statement
Hi there,
I have a selection screen on which you can choose which table do you want to display.
I store the name of the selected table in a variable tab_name.
I want to use something like that:
data: tab_name(40).
select * from tab_name
into gt_master.
endselect.
But it did not allowed because tab_name is not a database table.
Is it possible to pass the table name in a select statement using a variable?Hello
Yes it is posible you can use a dynamic select...
Try this:
DATA tabname(10).
DATA: BEGIN OF wa,
id TYPE scustom-id,
name TYPE scustom-name,
END OF wa.
tabname = 'SCUSTOM'.
SELECT id name INTO CORRESPONDING FIELDS OF wa FROM (tabname).
WRITE: / wa-id, wa-name.
ENDSELECT.
or
CONSTANTS: flight_tab_name(30) VALUE 'SPFLI'.
DATA: from_clause TYPE STRING.
DATA: BEGIN OF wa,
name(20) TYPE C,
connid TYPE spfli-connid,
END OF wa.
CONCATENATE flight_tab_name ' AS t1'
' JOIN scarr AS t2 ON t1carrid = t2carrid'
INTO from_clause.
SELECT t1connid t2carrname AS name
FROM (from_clause)
INTO CORRESPONDING FIELDS OF wa.
WRITE: / wa-name, wa-connid.
ENDSELECT
Hope this helps
Gabriel -
Storing variable from select statement
I need to store a variable from a select statement into a session. Before I was taken what was passed from another page and throwing it into a session.--
String Password= request.getParameter("Password");
String sql = "Select WEB_USER_ID FROM Web_User WHERE PASSWORD = '" + Password +"'";
session.setAttribute("USER",Password);
Now though I need to put in Web_User_Id and cannot find the proper syntax. Can someone help
session.setAttribute("User",???????);WEB_USER_ID is unidentified because it is a column name, not a variable name. You should first assign its value to a variable and then put the variable name into the setAttribute statement instead.
Your query may return more than one result based on the way it is written. It sounds like you are trying to find all users with a password that is equal to the value of Password. If this is correct, then are you trying to create an attribute for each user?
If your intention is to locate a user, then the user id and password should have been provided. A variable should be defined for both the password and user id. You should then add the user id to the where clause. After a successful search, the attributes can be created using the same syntax. -
Using Cursor in Select statements? How to do this?
I am getting an error whilt passing a cursor to a select clause:
SELECT dbms_xmlquery.getXML('select deptno, dname, '||
'cursor(select empno, ename, sal from emp e where e.deptno = d.deptno) employees '||
'from dept d where d.deptno in (10, 20)')
FROM dual;
DBMS_XMLQUERY.GETXML('SELECTDEPTNO'||'CURSORIS(SELECTEMPNOFROMEMPEWHEREE.DEPT=D.
<?xml version = '1.0'?>
<ERROR>oracle.xml.sql.OracleXMLSQLException: ORA-00923
: FROM keyword not found where expected
</ERROR
THIS IS DUE TO THE CURSOR AND ITS FROM STATEMENT?
CAN ANY ONE PLEASE GUIDE AS TO HOW TO USE CURSORS IN A SELECT STATEMENT PLEASE?Another duplicate thread. See my response Select CLAUSE error using CURSORS & XSU.Please SEE..
Cheers, APC -
Using TMVL in Select Statement
Dear All,
Is the usage of TMVL in Select Statement valid?
*SELECT(%VAR_YEAR%, YEAR, TIME, ID = TMVL(-12, %TIME_SET%))
I don't think I manage to get this statement running.
Thanks,
YLThanks All.
My requirement is:
Copy from Source to Destination using script logic:
Package Selections:
Category : Forecast
Time : 2025.12
Version : 2025
Source
Dimension Category : Plan
Dimension Time : 2025.12
Dimension Version : 2024
Destination
Dimension Category : Forecast
Dimension Time : 2025.12
Dimension Version : 2025
And question is how to derive Version 2024 which is 2025 - 1, without using a specific property for it, i.e. Previous Year?
Thank you very much.
Best regards,
Yen Li -
HI guys:
I need code three SQL statements.the returned field and selected table are all same,there is only a difference in their "where" statement.
Sample code:
select marcmatnr marcwerks
into table it_data
from MARC inner join MBEW on marcmatnr = mbewmatnr
where marcmatnr like 'A%' and mbewzplp1 = '001'.
second one........................ mbew~zplp2 = '001'
third one......................... mbew~zplp3 = '001'
Could I write a FORM gather them with transporting a parameter ZPLPX to determine which condiniton will be execute?
thank you very much.Hi tianli,
1. source text
This concept of dynamic where
condition is called source text.
2. use like this.
This is important in the code ---> WHERE (mywhere).
REPORT abc LINE-SIZE 80.
DATA : it_data LIKE TABLE OF mara WITH HEADER LINE.
QUOTES ARE IMPORTANT
PERFORM mysql USING 'mbew~zplp2 = ''001'''.
FORM mysql USING mywhere.
SELECT marcmatnr marcwerks
INTO TABLE it_data
FROM marc INNER JOIN mbew ON marcmatnr = mbewmatnr
WHERE (mywhere).
ENDFORM. "mysql
regards,
amit m. -
Want to use Index in Select statement
Hi,
I want to use the index tha is created for LIPS table for creation date. I want to use that INdex in the select statement to get the data from that table. Any one can tell me how can I right the select statement in report?
thanks.Like I mentioned earlier, the optimizer is smart enough to choose the correct index based on the WHERE clause. So if you created an index on field ERDAT for LIPS and your SELECT statement is like so.....
Select ERDAT WERKS MATNR
from LIPS
into table Itab
Where erdat in s_erdat "< - ERDAT First in WHERE Clause
and werks in s_werks
and Matnr in s_matnr.
Then the optimizer will choose your index to use to access the data. You can see these if you would do an SQL trace over your program and use the "Explain" button on ST05. It will tell you which index it used and why.
There is no need to use HINTs to force the use of the index.
Regards,
Rich Heilman
Maybe you are looking for
-
Is there a way to set up automatic form fill-in on iPad 2
Can anyone help me with my question? Thank you.
-
What are the daily activities of a BW Consultant - In brief?
What are the daily activities of a BW Consultant - In brief? What are the daily activities that an consultant has to perform when he is in Implementation and Support? E.g : <b>For Implementation</b> 1.Creating Business Requirement document 2.Creating
-
HT4946 Deleted photos by mistake can I get them back
I Have deleted my photos by mistake on my iPhone 4s is there a way to get them back.
-
How do i set up time capsule to work with airport extreme base to extend wireless network?
i have a airport extreme base, how do i set up a time machine to extend the wireless network i have with airport extreme? i have also 3 airport express's connected to the same network to extend my wifi all over my house. thanks in advance.
-
Hello, I am an art student trying to get my demo reel up and running. My footage is 100% digital art, no NTSC video. I exported my video at maximum quality from After Effects using Quicktime with no compression and it looks just as good as my origina