Use variable as alias

  • Hello everybody,

    i hope somebody can give me a hint: is it possible to declare a variable and use its value as an alias in a select statement afterwards like

    DECLARE @test_var varchar(10)

    SELECT @test_var = (SELECT [col_1] FROM [table_1] WHERE [col_2]='something')

    SELECT [col_1],

    FROM [table_2] AS @j...

  • Can't think of why you would want to do that, but if I had a need to do something like that, it really sounds like dynamic sql is the only way to accomplish it.

  • Well, I have a table that contains keys and values: COL_KEY and COL_NAME. The first contains a nutrient code (always the same, no matter the language). The later contains nutrient names in a specific language. Like xyz_cal='Calories' in an English table, and xyz_cal='Kalorien' in a German table. There is another table with items (recipes and components) that have different nutrients and their values assigned (like 'Pancake' has 'xyz_cal'=200...). I'd like to select all the values for a selected item with COL_NAME of a nutrient in the target language. How would you use dynamic sql?

  • Now i need more info.  DDL for the tables and some sample data plus expected output based on the sample data.

    With that, either I or someone else out there will come up with a way of doing it.

  • SELECT @j-2 AS ParameterName, [col_1] AS parameterValue

    FROM [table_2] ...

    _____________
    Code for TallyGenerator

  • Perhaps you want a table variable?

    DECLARE @Table TABLE

    (

    Column1 INT NOT NULL,

    Column2 VARCHAR(10) NOT NULL

    )

  • Or you could define a separate view for each language

    David

    If it ain't broke, don't fix it...

  • Search this site for "DYNAMIC SQL".

    RegardsRudy KomacsarSenior Database Administrator"Ave Caesar! - Morituri te salutamus."

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply