• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Select Column using Session Variable

New Here ,
Nov 07, 2007 Nov 07, 2007

Copy link to clipboard

Copied

I have set a session variable to hold the name of a column dependent on who the user is. Is it possible to use a session variable to select a column in a database table? If so, how? Does it need a where statement?

Something like this?:

<cfquery name="rsSelectColumn" datasource="Test">
SELECT Column 1, Column 2, Column 3 = Session Variable
FROM tbl_data
</cfquery>

Thanks,
Dave
TOPICS
Advanced techniques

Views

606

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Nov 07, 2007 Nov 07, 2007
I think you want to do this, if the value in the session variable is the actual column name:

<cfquery name="rsSelectColumn" datasource="Test">
SELECT Column 1, Column 2, #Session Variable# AS Col3
FROM tbl_data
</cfquery>

this way you can call any column stored in the session variable, and it will always be usable from the QUERY as COL3 regardless of what column you used.


If that is what you want to do - it IS that simple.

:-)

Sometimes we over think things too much.

Votes

Translate

Translate
Guide ,
Nov 07, 2007 Nov 07, 2007

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 07, 2007 Nov 07, 2007

Copy link to clipboard

Copied

Yes you can use session variables.

<cfquery name="getUser" datasource="test">
SELECT Col1, Col2, Col3
FROM myTable
WHERE Col3 = #Session.variable#
</cfquery>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 07, 2007 Nov 07, 2007

Copy link to clipboard

Copied

I don't think that's exactly what they're trying to do. But if you read the other thread .. you can see what was suggested so far.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 07, 2007 Nov 07, 2007

Copy link to clipboard

Copied

I think you want to do this, if the value in the session variable is the actual column name:

<cfquery name="rsSelectColumn" datasource="Test">
SELECT Column 1, Column 2, #Session Variable# AS Col3
FROM tbl_data
</cfquery>

this way you can call any column stored in the session variable, and it will always be usable from the QUERY as COL3 regardless of what column you used.


If that is what you want to do - it IS that simple.

:-)

Sometimes we over think things too much.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 07, 2007 Nov 07, 2007

Copy link to clipboard

Copied

> SELECT Column 1, Column 2, #Session Variable#

That's what I already suggested on the other thread

Did anyone read the comments on this thread? :-)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 08, 2007 Nov 08, 2007

Copy link to clipboard

Copied

LATEST
Thanks! Works like a charm.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation