|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
select statement questionHello,
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 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. 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. 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. > BEGIN
DECLARE @raws int SET @raws = 10 SET ROWCOUNT @raws SELECT * 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 > > 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 > > 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 > > |
|||||||||||||||||||||||