--這種是無效的過程  
declare @sql nvarchar(500), @where nvarchar(500),@i nvarchar(64),@p nvarchar(50),@id int
set @id=5
set @sql='select '+@p+'=AreaCode from AdministrativeAreaList where AreaID='+cast(@id as varchar)
--select @sql
exec @sql
--測試結果:未能找到存储过程 ''。

---sql server 2000/2005 塗聚文
DECLARE @TableName VARCHAR(500),@sql NVARCHAR(200),@OrderID INT,@c nvarchar(50),@cstucount nvarchar(50)
 SET @TableName = 'AdministrativeAreaList ';
 SET @OrderID = 5;
SET @sql = 'SELECT top 1 @c=AreaCode FROM AdministrativeAreaList WHERE AreaID ='+cast(@OrderID as nvarchar)+' ORDER BY AreaID DESC'

exec sp_executesql @sql,N'@c nvarchar(50) output',@cstucount output--将exec的结果放入变量中的做法
select @cstucount