Home All Groups Group Topic Archive Search About

select statement question

Author
24 Nov 2005 7:08 PM
GB
Hello,
How could I get a result with the following code?

BEGIN
DECLARE @raws int
SET @raws = 10
SELECT TOP @raws * from Table1
END

Thanks,
GB

Author
24 Nov 2005 7:13 PM
--CELKO--
The short, proprietary, slow answer is to use dynamic SQL.  The results
are unpredictable, since you have no ORDER BY clause.

The right answer is not to use SELECT * in production code and to start
thinking in SQL (i.e. sets) and not in sequential files.
Author
24 Nov 2005 7:40 PM
Tony Rogerson
Please show a reference to your performance comparison.

Being proprietary does not me it will be slow, in fact, using proprietary
features often makes code faster and quicker to develop, test and maintain.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials


Show quote
"--CELKO--" <jcelko***@earthlink.net> wrote in message
news:1132859591.868367.23150@g43g2000cwa.googlegroups.com...
> The short, proprietary, slow answer is to use dynamic SQL.  The results
> are unpredictable, since you have no ORDER BY clause.
>
> The right answer is not to use SELECT * in production code and to start
> thinking in SQL (i.e. sets) and not in sequential files.
>
Author
24 Nov 2005 7:37 PM
Tony Rogerson
BEGIN
    DECLARE @raws int
    SET @raws = 10

    SET ROWCOUNT @raws

    SELECT * from Table1

END


--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials


"GB" <v7v***@hotmail.com> wrote in message
news:8dohf.167979$Io.145494@clgrps13...
Show quote
> Hello,
> How could I get a result with the following code?
>
> BEGIN
> DECLARE @raws int
> SET @raws = 10
> SELECT TOP @raws * from Table1
> END
>
> Thanks,
> GB
>
>
Author
24 Nov 2005 7:38 PM
Jaime Lucci
BEGIN
DECLARE @raws int
SET @raws = 10
exec('SELECT TOP ' + @raws + ' * from Table1')
END


"GB" <v7v***@hotmail.com> wrote in message
news:8dohf.167979$Io.145494@clgrps13...
Show quote
> Hello,
> How could I get a result with the following code?
>
> BEGIN
> DECLARE @raws int
> SET @raws = 10
> SELECT TOP @raws * from Table1
> END
>
> Thanks,
> GB
>
>
Author
24 Nov 2005 7:51 PM
Andrew J. Kelly
If you use SQL2005 you can use a variable for TOP but in 2000 you must use
one of the suggestions already posted.

--
Andrew J. Kelly  SQL MVP


"GB" <v7v***@hotmail.com> wrote in message
news:8dohf.167979$Io.145494@clgrps13...
Show quote
> Hello,
> How could I get a result with the following code?
>
> BEGIN
> DECLARE @raws int
> SET @raws = 10
> SELECT TOP @raws * from Table1
> END
>
> Thanks,
> GB
>
>

AddThis Social Bookmark Button