Question

Does anyone know the best way to limit the number of rows returned when using typed TableAdapters? Options appear to be:

  1. Pass "top X" as a parameter (can't do this until we're on SQLS2008
  2. Issue a dynamic SQL statement: set rowcount X before calling my tableadapter method, then set rowcount 0 aftwards.
  3. Something else I haven't thought of :).

Many thanks in advance.

Was it helpful?

Solution

You can use TOP n, but you have to put brackets around it like so:

SELECT TOP (100) * FROM ...

You can also parameterise it as follows:

DECLARE @count INT
SET @count = 100
SELECT TOP (@count) * FROM field_company

OTHER TIPS

This works fine for me.

SELECT DISTINCT TOP (@count) FLD1, FLD2 FROM mytable WITH (nolock)

The parameter is generated along with any others in the TableAdapter.

I achieve this using parameterized stored procedure.

create procedure dbo.spFoo 
    @NoOfRows int = 200

AS


declare @sql varchar(2000)
select @sql =
'select top ' + Cast(@NoOfRows as varchar) + ' * FROM   Foo'

exec(@sql)

I have found a better way.

You can't parameterise the "TOP" value, but you can parameterise the "SET ROWCOUNT X" value.

Therefore, this works:

create procedure test_SelectTopFromTable (@rowCount int) as
begin
    set rowcount @rowCount
    select * from table
    set rowcount 0
end

exec test_SelectTopFromTable 100

However I do need to deal with exceptions happening, which would prevent the set rowcount 0 statement from running. I'll keep digging.

UPDATE
My boss told me the way to make it work (and it's so simple I'm a bit embarrassed) but I'm going to get him to post his answer rather than make it look like I found it myself. :)

TableadApter use SqlDataAdapter, which already has this feature.

What to do:

Click on Fill() or FillByXXX() method

myTradesTableAdapter.Fill(...)

and press F12 - it will bring you to its source code generated by designer. Copy this method.

Go to the DataSet designer, click on table adapter and press F7 - it will create/open code for the TableAdapters namespace.

Paste Fill() method here and modify it some like in example below:

namespace MyApp.DsMyDataTableAdapters {

public partial class MyTradesTableAdapter
{
    //// copy-paste method from generated Fill()
    //// and make new name: Fill() ==> FillTop()
    //// use the same params and add new one: int topN
    public virtual int FillTop(             //
         DsMyData.MyTradesDataTable dataTable,
         int someParameterId,
         // ...
         int topN)  // add new param
    {
        // original code:
        this.Adapter.SelectCommand = this.CommandCollection[0];
        this.Adapter.SelectCommand.Parameters[1].Value = someParameterId;

        if ((this.ClearBeforeFill == true))
        {
            dataTable.Clear();
        }


        // modified code
        int returnValue = 0;
        if (topN > 0)                  // validate topN
        {
            // get topN rows
            returnValue = this.Adapter.Fill(0, topN, dataTable);   

        }
        else 
        {
            // get all rows (original code) in case topN = 0 or negative
            returnValue = this.Adapter.Fill(dataTable);
        }
        return returnValue;
    }

    // ....
}

and then you can use it as:

int somePrm = 123;
myTradesTableAdapter.Fill(ds.myTradesTable, somePrm) // original - get all rows
myTradesTableAdapter.FillTop(ds.myTradesTable, somePrm, 100) // new - get 100 rows
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top