Use of SET ROWCOUNT in SQL Server - Limiting result set

1,086

Solution 1

Not in one statement. You're going to have to subtract @@ROWCOUNT from the total rows you want after each statement, and use a variable (say, "@RowsLeft") to store the remaining rows you want. You can then SELECT TOP @RowsLeft from each individual query...

Solution 2

Use TOP not ROWCOUNT

http://msdn.microsoft.com/en-us/library/ms189463.aspx

You trying to get 1000 rows MAX from all tables right?

I think other methods may fill up with from the top queries first, and you may never get results from the lower ones.

Solution 3

The requirement sounds odd. Unless you are unioning or joining the data from the two selects, to consider them as one so that you apply a max rows simply does not make sense, since they are unrelated queries at that point. If you really need to do this, try:

select top 1000 from (
    select orderId, null as name, 'TableA' as Source from TableA
    union all
    select null as orderID, name, 'TableB' as Source from TableB
) a order by Source

Solution 4

SET ROWCOUNT applies to each individual query. In your given example, it's applied twice, once to each SELECT statement, since each statement is its own batch (they're not grouped or unioned or anything, and so execute completely separately).

@RedFilter's approach seems the most likely to give you what you want.

Solution 5

Untested and doesn't make use of ROWCOUNT, but could give you an idea?

Assumes col1 in TableA and TableB are the same type.

SELECT TOP 1000 * 
FROM (select orderId 
      from TableA 
      UNION ALL 
      select name from TableB) t
Share:
1,086
Ahmed Safadi
Author by

Ahmed Safadi

Updated on June 04, 2022

Comments

  • Ahmed Safadi
    Ahmed Safadi about 2 years

    i want use a custom button with image ,

    I don't want to use Facebook default login button.

    this is my code

        let loginButton = FBSDKLoginButton()
        loginButton.readPermissions = ["public_profile", "email", "user_friends"]
        loginButton.center = self.view.center
        loginButton.delegate = self
        loginButton.imageView?.image = UIImage(named: "cyanx2")
        self.view.addSubview(loginButton)
    

    and my button would be

    @IBAction func customButton(sender: UIButton) {
    
     // here's go the action when click on default facebook button   
    }