Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > Databases > General SQL Server Support

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-27-2008, 09:45 PM
Jan Hudecek
 
Posts: n/a
problem with column alias

Hello,

i have a problem using aliased column. I have the following query:

With masids AS
( select masID,
dbo.concat_friendly(masIDFrom) as 'From'
...
,ROW_NUMBER() OVER (ORDER BY From ) as RowNumber
from ...
)
(truncated)

I just need to get a column named 'From'! But it seems there is no way
to tell the sql server to sort by that column. If I use it like this
it says that the keyword FROM is used. If I use it with quotes 'From'
it says that i can't sort by a constant expression (it treats it as a
literal). If I put it into brackets [From] it says unknown column. The
only solution I found is to use (select dbo.concat_friendly(masIDFrom)
as the order column. But that is not satisfactory for multiple
reasons. Is there any other way?

Thanks
Jan
Reply With Quote
  #2 (permalink)  
Old 07-27-2008, 10:06 PM
Plamen Ratchev
 
Posts: n/a
Re: problem with column alias

You cannot use a column alias in the definition of another column. The
solution is as you found to repeat the expression. Alternative solution is
to define the first column in one CTE (or derived table) and then use the
column in a second CTE.

Also, it is good to define a valid column name (not a revered keyword) and
then change it to whatever you want in the final SELECT (or client
application).


Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #3 (permalink)  
Old 07-27-2008, 10:11 PM
Roy Harvey (SQL Server MVP)
 
Posts: n/a
Re: problem with column alias

You can not use an alias in the ORDER BY of an OVER.

Perhaps you can do it in two stages by using two SELECTs as Common
Table Expressions. The first would set the value and column name, the
second would apply the ROW_NUMBER.

I would also stop using a reserved keyword as a column name.

Roy Harvey
Beacon Falls, CT

On Sun, 27 Jul 2008 13:45:40 -0700 (PDT), Jan Hudecek
<hudecekjan@gmail.com> wrote:

>Hello,
>
>i have a problem using aliased column. I have the following query:
>
> With masids AS
> ( select masID,
> dbo.concat_friendly(masIDFrom) as 'From'
> ...
> ,ROW_NUMBER() OVER (ORDER BY From ) as RowNumber
> from ...
> )
>(truncated)
>
>I just need to get a column named 'From'! But it seems there is no way
>to tell the sql server to sort by that column. If I use it like this
>it says that the keyword FROM is used. If I use it with quotes 'From'
>it says that i can't sort by a constant expression (it treats it as a
>literal). If I put it into brackets [From] it says unknown column. The
>only solution I found is to use (select dbo.concat_friendly(masIDFrom)
>as the order column. But that is not satisfactory for multiple
>reasons. Is there any other way?
>
>Thanks
>Jan

Reply With Quote
  #4 (permalink)  
Old 07-27-2008, 10:14 PM
Jan Hudecek
 
Posts: n/a
Re: problem with column alias

On 27 Čec, 23:06, "Plamen Ratchev" <Pla...@SQLStudio.com> wrote:
> You cannot use a column alias in the definition of another column. The
> solution is as you found to repeat the expression. Alternative solution is
> to define the first column in one CTE (or derived table) and then use the
> column in a second CTE.
>
> Also, it is good to define a valid column name (not a revered keyword) and
> then change it to whatever you want in the final SELECT (or client
> application).
>
> Plamen Ratchev

Thank you, you're probably right about that keyword..
Jan
Reply With Quote
  #5 (permalink)  
Old 07-28-2008, 05:06 PM
--CELKO--
 
Posts: n/a
Re: problem with column alias

That is a bad mix of proceudral code and scoping problems.

1) Dump the UDF for real SQL code. You are still in a procedural
mindset and not writing SQL (i.e. declarative code). It will mess up
index access, portability and you get alias scoping problems.

2) Do not use a reserved word for an alias. Do not use a vague
reserved word for an alias.

3) Put a column list in the CTE header, just like you do with a
VIEW.

4) ORDER BY needs a column name, not an alias.

WITH Mas_ids (mas_id, foobar, row_nbr) -- give a list here
AS
(SELECT mas_id, foobar, -- don't use UDFs in SQL, pick a meaningful
name (not foobar)
ROW_NUMBER() OVER (ORDER BY foobar) AS row_nbr
FROM ...
)
Reply With Quote
  #6 (permalink)  
Old 07-28-2008, 10:00 PM
Jan Hudecek
 
Posts: n/a
Re: problem with column alias

On 28 Čec, 18:06, --CELKO-- <jcelko...@earthlink.net> wrote:
> That is a bad mix of proceudral code and scoping problems.
>
> 1) Dump the UDF for real SQL code. *You are still in a procedural
> mindset and not writing SQL (i.e. declarative code). *It will mess up
> index access, portability and you get alias scoping problems.
>
> 2) Do not use a reserved word for an alias. *Do not use a vague
> reserved word for an alias.
>
> 3) Put a column list in the CTE header, just like you do with a
> VIEW.
>
> 4) ORDER BY needs a column name, not an alias.
>
> WITH Mas_ids (mas_id, foobar, row_nbr) *-- give a list here
> AS
> (SELECT mas_id, foobar, -- don't use UDFs in SQL, pick a meaningful
> name (not foobar)
> * * * * ROW_NUMBER() OVER (ORDER BY foobar) AS row_nbr
> *FROM ...
> *)


Thanks for all the suggestions, I'm actually gunning for portability
but the problem is im generating this query programmatically depending
on some circumstances and i need to do it in such a way that the code
that creates it will be the same on oracle, mysql and mssql.
Creating a view would probably be the first way in which i would
improve the set of queries...
Reply With Quote
  #7 (permalink)  
Old 07-29-2008, 04:44 PM
--CELKO--
 
Posts: n/a
Re: problem with column alias

>> Thanks for all the suggestions, I'm actually gunning for portability but the problem is I am generating this query pro grammatically depending on some circumstances .. <<

That is almost always a bad idea. It is slow, it says that you have
no control over the execution of your application, etc. -- you are ad
libbing, which can be funny in a comedy show, but not so good in a
database

>> and I need to do it in such a way that the code that creates it will be the same on oracle, mysql and mssql. <<


There is as defined by ANSI and ISO; it is supposed to run anywhere
and I find it is much easier to write than most "one product cowboy
coders" think. Then there is portable SQL; it has mild dialect
features that can be mapped to Standard SQL, so the conversion is
mechanical (getdate() => CURRENT_TIMESTAMP, etc). Then there is pure
dialect and you are screwed (Oracle's CONNECT BY).

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > Databases > General SQL Server Support


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:42 AM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Credit Card | Mortgage Calculator | Debt Help | Buy Anything On eBay | Loans



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114