adding alias to asp sql string

akohl's picture

They have: 117 posts

Joined: Feb 2001

Here is the string that i have been using on my asp page.
But I want to display aliases instead of the actual field names. So I tried adding an alias to the first field mentioned in the below string. I pasted three tries and the error messages that I got.

Its similar to the previous problem that I posted. Basically, how to compose sql strings to be used on asp pages connecting to an ms access database.

After helping me with this specific problem, could you reccomend some articles explaining the basics on this issue? Or even a good book explaining how to work with databases with asp.

sqlstr="SELECT Books.title, Authors.first_name, Authors.last_name, Publishers.publisher_name, Books.category, Books.binding, Books.price, Books.stock FROM (Authors INNER JOIN Books ON Authors.author_id = Books.author_id) INNER JOIN Publishers ON Books.publisher_id = Publishers.publisher_id WHERE (((Publishers.publisher_id)="&requested_publisher&"));"

'&"my title"&'
expected end of statement

' &"my title"& '
expected end of statement

sqlstr="SELECT Books.title' &'my title'& '
missing operator

Andy Kohlenberg
Jerusalem, Israel

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

Andy,

I'm not sure what you mean by alias, do you mean you want to use a variable? If so then you would code it like so:

strSQL = "SELECT * FROM My Table WHERE MyField = '" & MyVariable & "'"

As for books, I don't know of any specific titles, but I find books by WROX to be very good. An ASP book should at least have one chapter on databases. You can also find some examples at the following sites:

asp101.com
ultimateasp.com
freevbcode.com

PJ | Are we there yet?
pjboettcher.com

akohl's picture

They have: 117 posts

Joined: Feb 2001

What I mean by alias is that rs.fields(0).name will return the alias that I chose instead of the actual name of the field, which might not be an appropriate field heading for presenting to the user.

Andy Kohlenberg
Jerusalem, Israel

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

Andy,

I see what you mean. It's good practice not to use field names from the database in your presentation layer. The headings should be hard coded in the ASP to be something friendly for the user. This would avoid any mistakes in the future, incase someone renamed a field.

PJ | Are we there yet?
pjboettcher.com

akohl's picture

They have: 117 posts

Joined: Feb 2001

Hardcoding the field headings into the asp code is what I have been doing up until now. But I thought that giving the field names aliases in the recordset would be easier. Are you saying that this cannot be done?

Andy Kohlenberg
Jerusalem, Israel

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

You can setup aliases for tables in your query but I think you still have to reference them by their real name in the ASP page.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.