 |
|
 |
|
Next: SQL 2005 Cluster with dedicated nic for Sharepoin..
|
| Author |
Message |
External

Since: Jan 22, 2004 Posts: 10
|
(Msg. 1) Posted: Fri Aug 10, 2007 2:16 pm
Post subject: SELECT row_number() over (order by @order_by) passes syntax check Archived from groups: microsoft>public>sqlserver>programming (more info?)
|
|
|
/*
Using variables for dynamic sorting
SELECT row_number() over (order by @order_by) executes without an error but
doesn't do anything. Is this a bug or am I doing something wrong?
Using SQL Server 2005 SP2
*/
if object_id('tempdb.dbo.#tRowNumber') is not null
drop table #tRowNumber
go
create table #tRowNumber (id int identity, account_number char(10), name
varchar(100))
go
insert #tRowNumber values ('0000000000','NameZero')
insert #tRowNumber values ('1111111111','NameOne')
go
declare @order_by sysname
set @order_by = 'name'
SELECT row_number() over (order by @order_by) as row_number
, id
, account_number
, name
from #tRowNumber
-----------
row_number id account_number name
1 1 0000000000 NameZero
2 2 1111111111 NameOne >> Stay informed about: SELECT row_number() over (order by @order_by) passes synta.. |
|
| Back to top |
|
 |  |
External

Since: Jun 26, 2007 Posts: 39
|
(Msg. 2) Posted: Fri Aug 10, 2007 3:35 pm
Post subject: Re: SELECT row_number() over (order by @order_by) passes syntax check [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
The variable, in this case, is being interpreted as a string literal.
You'll have to use dynamic SQL if you want to actually change the sort order
dynamically...
--
Adam Machanic
SQL Server MVP - http://sqlblog.com
Author, "Expert SQL Server 2005 Development"
http://www.apress.com/book/bookDisplay.html?bID=10220
<a> wrote in message
> /*
> Using variables for dynamic sorting
>
> SELECT row_number() over (order by @order_by) executes without an error
> but doesn't do anything. Is this a bug or am I doing something wrong?
>
> Using SQL Server 2005 SP2
> */
>
>
> if object_id('tempdb.dbo.#tRowNumber') is not null
> drop table #tRowNumber
> go
> create table #tRowNumber (id int identity, account_number char(10), name
> varchar(100))
> go
> insert #tRowNumber values ('0000000000','NameZero')
> insert #tRowNumber values ('1111111111','NameOne')
> go
>
>
> declare @order_by sysname
> set @order_by = 'name'
>
> SELECT row_number() over (order by @order_by) as row_number
> , id
> , account_number
> , name
> from #tRowNumber
>
>
> -----------
> row_number id account_number name
> 1 1 0000000000 NameZero
> 2 2 1111111111 NameOne
> >> Stay informed about: SELECT row_number() over (order by @order_by) passes synta.. |
|
| Back to top |
|
 |  |
External

Since: Apr 10, 2006 Posts: 15
|
(Msg. 3) Posted: Mon Aug 13, 2007 9:57 pm
Post subject: Re: SELECT row_number() over (order by @order_by) passes syntax check [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Adam Machanic" wrote in message
> The variable, in this case, is being interpreted as a string literal.
> You'll have to use dynamic SQL if you want to actually change the sort
order
> dynamically...
Dynamic sql is the unwitting attempt of sql programmers to work
with sql as if it were relational. It is unwitting because sql
programmers lack a clear vision of what it is that they are
striving toward. Though the method is clumsy the inclination to
use it should be motivation to explore a system where its
use becomes less necessary. A better name for dynamic sql is
'variable' sql since it is the concept of a variable that lies
at the heart of the issue. A primer for the unwitting is here:
http://beyondsql.blogspot.com/2007/08/dataphor-creating-super-function.html >> Stay informed about: SELECT row_number() over (order by @order_by) passes synta.. |
|
| Back to top |
|
 |  |
External

Since: Jun 12, 2007 Posts: 23
|
(Msg. 4) Posted: Tue Aug 14, 2007 6:12 am
Post subject: Re: SELECT row_number() over (order by @order_by) passes syntax check [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Aug 10, 2:16 pm, <a> wrote:
> /*
> Using variables for dynamic sorting
>
> SELECT row_number() over (order by @order_by) executes without an error but
> doesn't do anything. Is this a bug or am I doing something wrong?
>
> Using SQL Server 2005 SP2
> */
>
> if object_id('tempdb.dbo.#tRowNumber') is not null
> drop table #tRowNumber
> go
> create table #tRowNumber (id int identity, account_number char(10), name
> varchar(100))
> go
> insert #tRowNumber values ('0000000000','NameZero')
> insert #tRowNumber values ('1111111111','NameOne')
> go
>
> declare @order_by sysname
> set @order_by = 'name'
>
> SELECT row_number() over (order by @order_by) as row_number
> , id
> , account_number
> , name
> from #tRowNumber
>
> -----------
> row_number id account_number name
> 1 1 0000000000 NameZero
> 2 2 1111111111 NameOne
In addition to dynamic SQL suggested by Adam, try this:
CREATE TABLE #t(i INT, name INT)
DECLARE @d VARCHAR(10)
SET @d = 'name'
SELECT ROW_NUMBER() OVER(ORDER BY CASE WHEN @d = 'name' THEN name ELSE
i END) FROM #t
Alex Kuznetsov, SQL Server MVP
http://sqlserver-tips.blogspot.com/ >> Stay informed about: SELECT row_number() over (order by @order_by) passes synta.. |
|
| Back to top |
|
 |  |
External

Since: Apr 10, 2006 Posts: 15
|
(Msg. 5) Posted: Tue Aug 14, 2007 9:26 pm
Post subject: Re: SELECT row_number() over (order by @order_by) passes syntax check [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
One of the most popular characterizations of sql code is the 'try this'.
Of course lying at the heart of the 'try this' is usually the
the ubiquitous 'workaround', the mainstay of sql. Given the
ever presence of these two staples it is a mystery how often
just what it is that is being worked around is not even mentioned.
Leaving that part out of course has its advantages. Books of
'workarounds' become written so often that the meaning of
'workaround' becomes lost and just the code remains. And what
we have is a language with a vast closet of skeletons where by
mutual agreement among its custodians the key to the closet has
been throw away.
www.beyondsql.blogspot.com
"Alex Kuznetsov" wrote in message
> .
> In addition to dynamic SQL suggested by Adam, try this:
>
> CREATE TABLE #t(i INT, name INT)
> DECLARE @d VARCHAR(10)
> SET @d = 'name'
> SELECT ROW_NUMBER() OVER(ORDER BY CASE WHEN @d = 'name' THEN name ELSE
> i END) FROM #t
> >> Stay informed about: SELECT row_number() over (order by @order_by) passes synta.. |
|
| Back to top |
|
 |  |
External

Since: Jun 12, 2007 Posts: 23
|
(Msg. 6) Posted: Wed Aug 15, 2007 8:22 am
Post subject: Re: SELECT row_number() over (order by @order_by) passes syntax check [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Aug 14, 11:26 pm, "Steve Dassin" wrote:
> One of the most popular characterizations of sql code is the 'try this'.
> Of course lying at the heart of the 'try this' is usually the
> the ubiquitous 'workaround', the mainstay of sql. Given the
> ever presence of these two staples it is a mystery how often
> just what it is that is being worked around is not even mentioned.
> Leaving that part out of course has its advantages. Books of
> 'workarounds' become written so often that the meaning of
> 'workaround' becomes lost and just the code remains. And what
> we have is a language with a vast closet of skeletons where by
> mutual agreement among its custodians the key to the closet has
> been throw away.
>
> www.beyondsql.blogspot.com
>
> "Alex Kuznetsov" wrote in message
>
>
>
> > .
> > In addition to dynamic SQL suggested by Adam, try this:
>
> > CREATE TABLE #t(i INT, name INT)
> > DECLARE @d VARCHAR(10)
> > SET @d = 'name'
> > SELECT ROW_NUMBER() OVER(ORDER BY CASE WHEN @d = 'name' THEN name ELSE
> > i END) FROM #t
And what is your better alternative?
T-SQL is not about allowing unlimited flexibility, it is more about
being specific in your canned queries. As such, if you want to use it
off the beaten path, you do need workarounds.
Alex Kuznetsov, SQL Server MVP
http://sqlserver-tips.blogspot.com/ >> Stay informed about: SELECT row_number() over (order by @order_by) passes synta.. |
|
| Back to top |
|
 |  |
External

Since: Apr 10, 2006 Posts: 15
|
(Msg. 7) Posted: Wed Aug 15, 2007 9:37 pm
Post subject: Re: SELECT row_number() over (order by @order_by) passes syntax check [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Oct 28, 2010 Posts: 1
|
(Msg. 8) Posted: Thu Oct 28, 2010 7:00 am
Post subject: Re: Re: SELECT row_number() over (order by @order_by) passes syntax [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Close...
SELECT Field1, Field2,
CASE
WHEN @SortField = 'Field1' THEN
ROW_NUMBER() OVER(ORDER BY Field1)
WHEN @SortField = 'Field2' THEN
ROW_NUMBER() OVER(ORDER BY Field2)
ELSE
ROW_NUMBER() OVER(ORDER BY DefaultField)
END
AS RowNum
FROM myTable
WHERE RowNum BETWEEN 5 AND 10
> On Friday, August 10, 2007 3:16 PM <a> wrote:
> /*
> Using variables for dynamic sorting
>
> SELECT row_number() over (order by @order_by) executes without an error but
> doesn't do anything. Is this a bug or am I doing something wrong?
>
> Using SQL Server 2005 SP2
> */
>
>
> if object_id('tempdb.dbo.#tRowNumber') is not null
> drop table #tRowNumber
> go
> create table #tRowNumber (id int identity, account_number char(10), name
> varchar(100))
> go
> insert #tRowNumber values ('0000000000','NameZero')
> insert #tRowNumber values ('1111111111','NameOne')
> go
>
>
> declare @order_by sysname
> set @order_by = 'name'
>
> SELECT row_number() over (order by @order_by) as row_number
> , id
> , account_number
> , name
> from #tRowNumber
>
>
> -----------
> row_number id account_number name
> 1 1 0000000000 NameZero
> 2 2 1111111111 NameOne
>> On Friday, August 10, 2007 3:35 PM Adam Machanic wrote:
>> The variable, in this case, is being interpreted as a string literal.
>> You'll have to use dynamic SQL if you want to actually change the sort order
>> dynamically...
>>
>>
>> --
>>
>> Adam Machanic
>> SQL Server MVP - http://sqlblog.com
>>
>> Author, "Expert SQL Server 2005 Development"
>> http://www.apress.com/book/bookDisplay.html?bID=10220
>>
>>
>>
>> <a> wrote in message
>>> On Tuesday, August 14, 2007 12:57 AM Steve Dassin wrote:
>>> "Adam Machanic" wrote in message
>>>
>>> order
>>>
>>> Dynamic sql is the unwitting attempt of sql programmers to work
>>> with sql as if it were relational. It is unwitting because sql
>>> programmers lack a clear vision of what it is that they are
>>> striving toward. Though the method is clumsy the inclination to
>>> use it should be motivation to explore a system where its
>>> use becomes less necessary. A better name for dynamic sql is
>>> 'variable' sql since it is the concept of a variable that lies
>>> at the heart of the issue. A primer for the unwitting is here:
>>>
>>> http://beyondsql.blogspot.com/2007/08/dataphor-creating-super-function.html
>>>> On Tuesday, August 14, 2007 9:12 AM Alex Kuznetsov wrote:
>>>> On Aug 10, 2:16 pm, <a> wrote:
>>>>
>>>> In addition to dynamic SQL suggested by Adam, try this:
>>>>
>>>> CREATE TABLE #t(i INT, name INT)
>>>> DECLARE @d VARCHAR(10)
>>>> SET @d = 'name'
>>>> SELECT ROW_NUMBER() OVER(ORDER BY CASE WHEN @d = 'name' THEN name ELSE
>>>> i END) FROM #t
>>>>
>>>> Alex Kuznetsov, SQL Server MVP
>>>> http://sqlserver-tips.blogspot.com/
>>>>> On Wednesday, August 15, 2007 12:26 AM Steve Dassin wrote:
>>>>> One of the most popular characterizations of sql code is the 'try this'.
>>>>> Of course lying at the heart of the 'try this' is usually the
>>>>> the ubiquitous 'workaround', the mainstay of sql. Given the
>>>>> ever presence of these two staples it is a mystery how often
>>>>> just what it is that is being worked around is not even mentioned.
>>>>> Leaving that part out of course has its advantages. Books of
>>>>> 'workarounds' become written so often that the meaning of
>>>>> 'workaround' becomes lost and just the code remains. And what
>>>>> we have is a language with a vast closet of skeletons where by
>>>>> mutual agreement among its custodians the key to the closet has
>>>>> been throw away.
>>>>>
>>>>> www.beyondsql.blogspot.com
>>>>>
>>>>> "Alex Kuznetsov" wrote in message
>>>>>
>>>>>> On Wednesday, August 15, 2007 11:22 AM Alex Kuznetsov wrote:
>>>>>> On Aug 14, 11:26 pm, "Steve Dassin" wrote:
>>>>>>
>>>>>> And what is your better alternative?
>>>>>>
>>>>>> T-SQL is not about allowing unlimited flexibility, it is more about
>>>>>> being specific in your canned queries. As such, if you want to use it
>>>>>> off the beaten path, you do need workarounds.
>>>>>>
>>>>>> Alex Kuznetsov, SQL Server MVP
>>>>>> http://sqlserver-tips.blogspot.com/
>>>>>>> On Thursday, August 16, 2007 12:37 AM Steve Dassin wrote:
>>>>>>> My blog is quite specific about it
>>>>>>>
>>>>>>> www.beyondsql.blogspot.com
>>>>>>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>>>>>>> ASP.NET 4.0 browser capabilities
>>>>>>> http://www.eggheadcafe.com/tutorials/aspnet/668c15e2-9fe5-4a7a-94ac-00...0bf4d1b >> Stay informed about: SELECT row_number() over (order by @order_by) passes synta.. |
|
| Back to top |
|
 |  |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|