---
title: "Delete top n according to the variable"
author: "mssp"
date: "2011-03-02T16:29:08Z"
tags: []
categories: ["数据库"]
slug: "delete-top-n-according-to-the-variable"
draft: false
lastmod: "2011-03-02T16:31:41Z"
id: 938445
url: "https://mssp.iteye.com/blog/938445"
---

declare @num int;  
select @num=count(\*) from budget where fk\_project\_id=62;

declare @topstr nVARCHAR(256);  
set @topstr='select top'+str(@num) +' * from budget';

execute sp\_executesql @topstr ;

 

```
declare @num int;
select @num=count(*) from budget where fk_project_id=62;

declare @topstr nVARCHAR(256);
set @topstr='select top'+str(@num) +' * from budget'; 

execute sp_executesql @topstr ;
```