Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grabbing external variables into SQL statement (create view) 1

Status
Not open for further replies.

bingbing

Technical User
Jul 14, 2003
12
MY
Hi, I'm having a problem with inputting values of external variables into an SQL statement which functions to create a view.
FYI, I'm using Oracle 9i Database Enterprise Edition. The following is my code:

<%
Dim recordSetLING
Dim connectionToDatabase
Dim KW

'In my webpage, I have a combobox named Keyword. Values chosen in combobox will be stored in the external variable KW
KW = &quot; ' &quot; & Request.Form(&quot;Keyword&quot;) & &quot; ' &quot;


%>
'To check whether KW contains the correct value from combobox
<br>You have chosen
<br>Keyword: <%=KW%>

<%
Set connectionToDatabase=Server.CreateObject(&quot;ADODB.Connection&quot;)
connectionToDatabase.ConnectionTimeout=60
connectionToDatabase.Open &quot;PROVIDER=OraOLEDB.Oracle; Data Source=databasename; User ID=user; PASSWORD=password;&quot;

Set recordSetLING=Server.CreateObject(&quot;ADODB.recordSet&quot;)
recordSetLING.Open &quot;Create view LING as Keyword.Keyword_ID from Keyword where Keyword.Keywords=KW&quot;, connectionToDatabase
.
.
.
.
.
.
The above syntax outputs an error that states KW=invalid identifier. From my understanding, the values in KW was not input into the SQL statement before execution. I am certain that KW contains the correct value before insertion.

The problem is to input the value of KW into the SQL statement
&quot;Create view...&quot;. I have referred to several books on SQL and creating views...so far I haven't encountered any syntax that allows the user to create view by grabbing values from external variables and then executing the statement. Your help is greatly appreciated.Thanks!!!
 
Assuming that KW variable is string:

<%
.....
recordSetLING.Open &quot;Create view LING as select Keyword.Keyword_ID from Keyword where Keyword.Keywords='&quot; & KW & &quot;'&quot;, connectionToDatabase
.....

%>
 
Thanks for the info. Now i've managed to avoid the problem of KW being an invalid identifier. However, referring to my code above,

'To check whether KW contains the correct value from combobox
<br>You have chosen
<br>Keyword: <%=KW%>

This correctly shows me the value of KW that the user has chosen using a combobox in the interface. The problem is that in my results page I only see the check routine above and nothing else. By right it should display something from the database. I've run thru the SQL statements many times manually into the database and it works. Only in ASP it doesn't display it out.

Could it be that the value in KW was not inserted into the SQL statement?

What could be the problem if the value of KW was successfully inserted into the SQL statement?

Thanks for your help.
 
&quot;By right it should display something from the database&quot;
Did you mean that you need to create a view, then reteieve from it and display the result on your web page? If so, then:
1. I'm not sure if this statement:
recordSetLING.Open &quot;Create view LING as Keyword.Keyword_ID from Keyword where Keyword.Keywords=KW&quot;, connectionToDatabase&quot;
will create a view and populate a recordset from it, most likely not, rather it'll just create the view but not retrieve from it, so your recordset is empty - that's why nothing's displayed on the page.
2. Why do you need to create a view? If just for retreiving a recordset from it, then simple Select will do the same, just faster. Also you won't need to worry about dropping the view afterwords.
<%
Dim recordSetLING
Dim connectionToDatabase
Dim KW

'In my webpage, I have a combobox named Keyword. Values chosen in combobox will be stored in the external variable KW
KW = &quot; ' &quot; & Request.Form(&quot;Keyword&quot;) & &quot; ' &quot;


%>
'To check whether KW contains the correct value from combobox
<br>You have chosen
<br>Keyword: <%=KW%>

<%
Set connectionToDatabase=Server.CreateObject(&quot;ADODB.Connection&quot;)
connectionToDatabase.ConnectionTimeout=60
connectionToDatabase.Open &quot;PROVIDER=OraOLEDB.Oracle; Data Source=databasename; User ID=user; PASSWORD=password;&quot;

Set recordSetLING=Server.CreateObject(&quot;ADODB.recordSet&quot;)
Set recordSetLING=connectionToDatabase.Execute(&quot;SELECT Keyword.Keyword_ID from Keyword where Keyword.Keywords='&quot; & KW & &quot;'&quot;)

'loop through the recordset...
while not recordSetLING.EOF
'work with records
recordSetLING.MoveNext
wend

recordSetLING.Close()
set recordSetLING = nothing
set connectionToDatabase = nothing

%>
 
Hi,
To LV, I am really glad that your idea really helped me a lot. I've abandoned the idea of creating views and using Select statements all the way. I still have a small problem though...the following is my code:

Set recordSetLING=Server.CreateObject(&quot;ADODB.recordSet&quot;)
Set recordSetLING=connectionToDatabase.Execute(&quot;Select PUBLICATIONS.PUB_ID, CATEGORY.CAT_ID, ARTICLEDATE.DATE_ID, KEYWORD.KEYWORD_ID, PUBLICATIONS.PUB_ID||CATEGORY.CAT_ID||ARTICLEDATE.DATE_ID||KEYWORD.KEYWORD_ID as AID from PUBLICATIONS, CATEGORY, ARTICLEDATE, KEYWORD where PUBLICATIONS.PUBLICATIONS=&quot;&Pub&&quot; and CATEGORY.CATEGORY=&quot;&Cat&&quot; and ARTICLEDATE.ART_DATE=&quot;&AD&&quot; and KEYWORD.KEYWORDS=&quot;&KW&&quot;&quot;)

Set recordSetLINK=Server.CreateObject(&quot;ADODB.recordSet&quot;)
recordSetLINK.Open &quot;Select LINK from LINK where LINK.ARTICLE_ID=AID&quot; ,connectionToDatabase

For recordSetLING, i would like to concatanate all those ids and name it as AID. Next I would like to place the value of AID in the recordSetLINK's select statement. AID would be a string. How do I do this?
 
This is my latest revision of my code...ignore previous posts...

Set recordSetLINK=Server.CreateObject(&quot;ADODB.recordSet&quot;)
recordSetLINK.Open &quot;Select LINK, PUBLICATIONS.PUB_ID, CATEGORY.CAT_ID, ARTICLEDATE.DATE_ID, KEYWORD.KEYWORD_ID from LINK, PUBLICATIONS, CATEGORY, ARTICLEDATE, KEYWORD where PUBLICATIONS.PUBLICATIONS=&quot;&Pub&&quot; and CATEGORY.CATEGORY=&quot;&Cat&&quot; and ARTICLEDATE.ART_DATE=&quot;&AD&&quot; and KEYWORD.KEYWORDS=&quot;&KW&&quot; and LINK.AID=PUBLICATIONS.PUB_ID||CATEGORY.CAT_ID||ARTICLEDATE.DATE_ID||KEYWORD.KEYWORD_ID&quot; ,connectionToDatabase


Do While Not recordSetLINK.EOF

%><a href=&quot;<%=Response.Write(recordSetLINK(&quot;LINK&quot;))%>&quot;><%=Response.Write(recordSetLINK(&quot;LINK&quot;))%></a><%
Response.Write(&quot;<BR>&quot;)
recordSetLINK.MoveNext

Loop

My problem is I don't get any display at my results page using the above SQL statement. However, running it thru the database I can get the wanted results. So my guess is it's syntax problem.

How do I set LINK.AID=PUBLICATIONS.PUB_ID||CATEGORY.CAT_ID||ARTICLEDATE.DATE_ID||KEYWORD.KEYWORD_ID

where the concatenation part to become a string? Meaning the results should be LINK.AID='2222'. Thanks a lot for your help!!!
 
First of all, I still don't see the surrounding single quotes around your string variables, i.e. &quot;and KEYWORD.KEYWORDS=&quot; & KW, should be &quot;and KEYWORD.KEYWORDS='&quot;& KW & &quot;'&quot;, in order to include a string value into the WHERE portion of your statement. It looks like you use the biwise &quot;OR&quot; operator in your statement, it's hard to say without knowing what's your logic is. Try doing domething like this:

<%
...
sql = &quot;Select LINK, PUBLICATIONS.PUB_ID, CATEGORY.CAT_ID, ARTICLEDATE.DATE_ID, KEYWORD.KEYWORD_ID from LINK, PUBLICATIONS, CATEGORY, ARTICLEDATE, KEYWORD where PUBLICATIONS.PUBLICATIONS=&quot;&Pub&&quot; and CATEGORY.CATEGORY=&quot;&Cat&&quot; and ARTICLEDATE.ART_DATE=&quot;&AD&&quot; and KEYWORD.KEYWORDS=&quot;&KW&&quot; and LINK.AID=PUBLICATIONS.PUB_ID||CATEGORY.CAT_ID||ARTICLEDATE.DATE_ID||KEYWORD.KEYWORD_ID&quot;
Response.Write sql
Response.End
..
%>
Copy the written string from the page and execute it against the database, see if you get any results. It will help you correct syntax errors. And in order for a concatinated part to become a string, you need to surround it by single quotes, like in this sample:

<%
linkId = Request.QueryString(&quot;LinkId&quot;)
sql = &quot;select * from LINK where LINK.LINKID='&quot; & llinkId & &quot;'&quot;
%>
So if linkId = 2222, your sql variable will look like this:
select * from LINK where LINK.LINKID='2222'
 
Hi again...based on your suggestion in the previous thread, I've tried using a variable to replace my entire SQL statement as a string. It works but currently I'm facing a problem with OraOLEDB (Oracle's Provider) since I'm using Oracle 9i for the database. My code looks like this now:

<HTML>
<HEAD></HEAD>

<BODY>
<%
'Connection using execute method and a string variable
Dim connectionToDatabase, strconnection, recordset
Dim AD, KW, Pub, Cat
Dim strSQL

strDay = Request.Form(&quot;Day&quot;)
strMonth = Request.Form(&quot;lstMonths&quot;)
strYear = Request.Form(&quot;Year&quot;)

AD = &quot;'&quot; & strDay & &quot;-&quot; & strMonth & &quot;-&quot; & strYear & &quot;'&quot;
KW = &quot;'&quot; & Request.Form(&quot;Keyword&quot;) & &quot;'&quot;
Pub = &quot;'&quot; & Request.Form(&quot;Publication&quot;) & &quot;'&quot;
Cat = &quot;'&quot; & Request.Form(&quot;Category&quot;) & &quot;'&quot;

strconnection = &quot;PROVIDER=OraOLEDB.Oracle; Data Source=databasename; User ID=user; PASSWORD=password;&quot;
strSQL=&quot;Select LINK.LINK, PUBLICATIONS.PUB_ID, CATEGORY.CAT_ID, ARTICLEDATE.DATE_ID, KEYWORD.KEYWORD_ID from LINK, PUBLICATIONS, CATEGORY, ARTICLEDATE, KEYWORD where PUBLICATIONS.PUBLICATIONS=&quot;&Pub&&quot;
and CATEGORY.CATEGORY=&quot;&Cat&&quot;
and ARTICLEDATE.ART_DATE=&quot;&AD&&quot;
and KEYWORD.KEYWORDS=&quot;&KW&&quot; and LINK.AID=PUBLICATIONS.PUB_ID||CATEGORY.CAT_ID||ARTICLEDATE.DATE_ID||KEYWORD.KEYWORD_ID&quot;

Set connectionToDatabase=Server.CreateObject(&quot;ADODB.Connection&quot;)
connectionToDatabase.open strconnection

Set recordset=connectionToDatabase.Execute(strSQL)

Do While Not recordset.EOF

%><a href=&quot;<%=Response.Write(recordset(&quot;LINK&quot;))%>&quot;><%=Response.Write(recordset(&quot;LINK&quot;))%></a><%
Response.Write(&quot;<BR>&quot;)
recordSetLINK.MoveNext

Loop

' Close our recordset and connection and dispose of the objects
connectionToDatabase.Close
Set connectionToDatabase = Nothing

%>


</BODY>
</HTML>


I've tried using Response.Write strSQL and copied the string displayed and ran it thru my database. It works.
However, when I run this ASP page, it gives me OraOLEDB
&quot;Missing Expression&quot; error at the following line:

Set recordset=connectionToDatabase.Execute(strSQL)

Any mistakes with my syntax here?
Any other comments for my code?
Thanks a lot of your time...:)
 
Hi again...okay so I rebooted my comp and ran the code again and this time there's no error except that I get a blank page after I ran the ASP file. I figured it could be my SQL part and following LV's previous suggestion on concatenation string...I've made the following changes...

<HTML>
<HEAD></HEAD>

<BODY>
<%
'Connection using execute method and a string variable
Dim connectionToDatabase, strconnection, recordset
Dim AD, KW, Pub, Cat, CNT
Dim strSQL

strDay = Request.Form(&quot;Day&quot;)
strMonth = Request.Form(&quot;lstMonths&quot;)
strYear = Request.Form(&quot;Year&quot;)

AD = &quot;'&quot; & strDay & &quot;-&quot; & strMonth & &quot;-&quot; & strYear & &quot;'&quot;
KW = &quot;'&quot; & Request.Form(&quot;Keyword&quot;) & &quot;'&quot;
Pub = &quot;'&quot; & Request.Form(&quot;Publication&quot;) & &quot;'&quot;
Cat = &quot;'&quot; & Request.Form(&quot;Category&quot;) & &quot;'&quot;
CNT = Request.QueryString(&quot;PUBLICATIONS.PUB_ID||CATEGORY.CAT_ID||ARTICLEDATE.DATE_ID||KEYWORD.KEYWORD_ID&quot;)


strconnection = &quot;PROVIDER=OraOLEDB.Oracle; Data Source=databasename; User ID=user; PASSWORD=password;&quot;
strSQL=&quot;Select LINK.LINK, PUBLICATIONS.PUB_ID, CATEGORY.CAT_ID, ARTICLEDATE.DATE_ID, KEYWORD.KEYWORD_ID from LINK, PUBLICATIONS, CATEGORY, ARTICLEDATE, KEYWORD where PUBLICATIONS.PUBLICATIONS=&quot;&Pub&&quot; and CATEGORY.CATEGORY=&quot;&Cat&&quot; and ARTICLEDATE.ART_DATE=&quot;&AD&&quot; and KEYWORD.KEYWORDS=&quot;&KW&&quot; and LINK.AID='&quot;&CNT&&quot;'&quot;

Set connectionToDatabase=Server.CreateObject(&quot;ADODB.Connection&quot;)
connectionToDatabase.open strconnection

Set recordset=connectionToDatabase.Execute(strSQL)

Do While Not recordset.EOF

%><a href=&quot;<%=Response.Write(recordset(&quot;LINK&quot;))%>&quot;><%=Response.Write(recordset(&quot;LINK&quot;))%></a><%
Response.Write(&quot;<BR>&quot;)
recordSetLINK.MoveNext

Loop

' Close our recordset and connection and dispose of the objects
connectionToDatabase.Close
Set connectionToDatabase = Nothing

%>


</BODY>
</HTML>

*I've replaced the concatenation statement using a variable called CNT. Single quotes has been included in the SQL statement too to parse it as a string. However, I'm not too sure on the correct syntax for the variable declaration:

CNT = Request.QueryString(&quot;PUBLICATIONS.PUB_ID||CATEGORY.CAT_ID||ARTICLEDATE.DATE_ID||KEYWORD.KEYWORD_ID&quot;)

I ran the code again and I still get a blank page.
 
If you Response.Write strSql, copy it and execute against the database, do you get some results back?
Regarding the synatx: do you pass all those variables (PUBLICATIONS.PUB_ID, CATEGORY.CAT_ID, ARTICLEDATE.DATE_ID, KEYWORD.KEYWORD_ID) separately int the query string?
Does it looks like this:
myPage.asp?PUBLICATIONS.PUB_ID=1&CATEGORY.CAT_ID=2&ARTICLEDATE.DATE_ID=3&KEYWORD.KEYWORD_ID=4
If so then in order to build CNT varable you need to extract then separately:
CNT = Request.QueryString(&quot;PUBLICATIONS.PUB_ID&quot;) & &quot;||&quot; & Request.QueryString(&quot;CATEGORY.CAT_ID&quot;) & &quot;||&quot; & Request.QueryString(&quot;ARTICLEDATE.DATE_ID&quot;) & &quot;||&quot; & Request.QueryString(&quot;KEYWORD.KEYWORD_ID&quot;)
and concatinate CNT to strSQL.
 
Dear LV and anyone whose reading this,

So far LV's suggestions have been really helpful in debugging my code. However, I feel that it's appropriate to explain the logic of my code and what I actually intend to do with my SQL statement.

I have the following tables in my database:
Publications:
PUB_ID PUBLICATIONS
---------- ------------------
2 Star

Category:
CAT_ID CATEGORY
---------- ----------------
4 Business

ArticleDate:
DATE_ID ART_DATE
---------- ---------
1 10-JUL-03

Keyword:
KEYWORD_ID KEYWORDS
----------- ----------------
1 Bank,Merger

Link:
AID LINK
---------- -----------------------------------
2411 C:\Inetpub\

Hence, what I intend to achieve is that when a user selects Star, Business, 10-JUL-03, Bank,Merger from the interface, the ASP page would return the values
PUB_ID = 2
CAT_ID = 4
DATE_ID = 1
KEYWORD_ID = 1

From my ASP code below, I concatenate the four values to become 2411 which i put in a variable called CNT.
Hence, from the SQL statement, it would look up from table Link and find LINK.AID=2411. Finally I should be able to display the link to that file.

The following is my code (which doesn't return any results):

<HTML>
<HEAD></HEAD>

<BODY>
<%
'Connection using execute method and a string variable
Dim connectionToDatabase, strconnection, recordset
Dim AD, KW, Pub, Cat, CNT
Dim strSQL

strDay = Request.Form(&quot;Day&quot;)
strMonth = Request.Form(&quot;lstMonths&quot;)
strYear = Request.Form(&quot;Year&quot;)

AD = strDay & &quot;-&quot; & strMonth & &quot;-&quot; & strYear
KW = Request.Form(&quot;Keyword&quot;)
Pub = Request.Form(&quot;Publication&quot;)
Cat = Request.Form(&quot;Category&quot;)
CNT = &quot;PUBLICATIONS.PUB_ID&quot; & &quot;||&quot; & &quot;CATEGORY.CAT_ID&quot; & &quot;||&quot; & &quot;ARTICLEDATE.DATE_ID&quot; & &quot;||&quot; & &quot;KEYWORD.KEYWORD_ID&quot;

strconnection = &quot;PROVIDER=OraOLEDB.Oracle; Data Source=databasename; User ID=user; PASSWORD=password;&quot;
strSQL=&quot;Select LINK.LINK, PUBLICATIONS.PUB_ID, CATEGORY.CAT_ID, ARTICLEDATE.DATE_ID, KEYWORD.KEYWORD_ID from LINK, PUBLICATIONS, CATEGORY, ARTICLEDATE, KEYWORD where PUBLICATIONS.PUBLICATIONS='&quot;&Pub&&quot;' and CATEGORY.CATEGORY='&quot;&Cat&&quot;' and ARTICLEDATE.ART_DATE='&quot;&AD&&quot;'and KEYWORD.KEYWORDS='&quot;&KW&&quot;' and LINK.AID=&quot;&CNT&&quot;&quot;

Set connectionToDatabase=Server.CreateObject(&quot;ADODB.Connection&quot;)
connectionToDatabase.open strconnection

Set recordset=connectionToDatabase.Execute(strSQL)

While Not recordset.EOF

%><a href=&quot;<%Response.Write(recordset(&quot;LINK&quot;))%>&quot;><%Response.Write(recordset(&quot;LINK&quot;))%></a><%
Response.Write(&quot;<BR>&quot;)
recordSet.MoveNext

Wend

' Close our recordset and connection and dispose of the objects
connectionToDatabase.Close
Set connectionToDatabase = Nothing

%>


</BODY>
</HTML>

My goal's to display C:\Inetpub\ on my web browser (IE 6 or Netscape)when I click the submit button.
Tried checking using Response.Write strSQL and copy&pasted
the string and ran it thru my database. It works and gives me the correct result.
I really need help on this. Been stuck at this problem (not able to display anything on my results page) for weeks...:(
If anyone out there knows the problem with my code do post a reply or email me at hcbing@yahoo.com
Thanks a lot of all your help!!!
 
Just by looking at your code, you are missing a declaration of the Recordset object:
Set recordset = Server.CreateObject(&quot;ADODB.Recordset&quot;)
Set recordset=connectionToDatabase.Execute(strSQL)

Then you'll need to close and kill it as well:
recordset.Close()
set recordset = nothing

This might be a part of a problem, if the query executed directly against the database does bring you results back. Also you'll probably need to restructure your database, looks like it's just a flat set of tables with no relations between them.
 
You dont need to define the RS object if you are using it like this Set recordset=connectionToDatabase.Execute(strSQL)
The object that a conexion returns it's an recordset.
If your code doesnt work you might need to reconsider 1 thing
the whole problem is this code

While Not recordset.EOF

%><a href=&quot;<%Response.Write(recordset(&quot;LINK&quot;))%>&quot;><%Response.Write(recordset(&quot;LINK&quot;))%></a><%
Response.Write(&quot;<BR>&quot;)
recordSet.MoveNext

Wend

As far as i can see this makes a link that points to the C:\Inetpub\ file but the problem is that C:\Inetpub\ it's a absolute path not a relative web path so that wont work. Instead you have to use a web relative path or absolute on that file.
But to achieve this you have to tell me if you can access using a browser this link
If not there is an alternative solution.


________
George, M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top