Renaming fields with variables

  • Here is what I want to do:

     DECLARE @sType1Name varchar(50)

     SET @sType1Name =  'Type (' + @sWeekNumber + ')'

     SELECT t1.OrganizationID, t1.OrganizationType as @sType1Name ...

    This does not pass the syntax checker.

    Anyone know how to get the same result, but in a way that works?

    I saw that someone else has asked this question, but I could not find the posting. If you know where I can find that first posting that would be fine as well.

  • Could you explain a bit about why you want to do this? What kind of 'problem' will it solve for you?

    /Kenneth

  • It involves me being allergic to doing anything with an interface . The column names in my question is displayed in a datagrid control.

    I solved the problem by moving the query from a stored procedure and into the code.

    If anyone has an answer I'm still interrested though. It is more fun to solve the problem in T-SQL .

     

  • Agreed Transact SQL is fun.

    Though the only way to do what you want there, is to resort to dynamic SQL, and I don't want to propose that as a 'solution', because many times it's not. (a solution that is) 

    Reasons to be reluctant or/and mindful of dynamic SQL can be found here. Do read it. http://www.sommarskog.se/dynamic_sql.html 'The Curse and Blessings of Dynamic SQL'

    /Kenneth

     

  • I considered that solution, but rejected it as not elegant enough

    To me, if I'm going to using such a solution, it seemed better to keep it in the code and leave my stored procedures unpolluted

    Thanks anyway for your input.

    /HL

  • You're absolutely on the right track with that notion

    BTW, I want people to read that link, not as an encouragment, but rather the opposite. There's waaaaaay more implications to dynamic SQL than most realize.

    /Kenneth

Viewing 6 posts - 1 through 5 (of 5 total)

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