Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting top records

Status
Not open for further replies.

orangeblue

Programmer
Nov 26, 2002
107
GB
hi
i have a staement
set @freeitems=(@count/3)

and after that I want to use@freeitem for the below statement - at the moment i am getting an error saying
'Incorrect syntax near '@freeitems'.'

whats the best way to do this


select top @freeitems price from OrderItems where discount_id=@discount_id and order_id=@order_id order by price
thanks
 
something like:
Code:
declare @freeitems numeric (8)
declare @order_id numeric(8,2)
declare @discount_id numeric (8,2)
declare @count numeric (8,2)
set @discount_id = 5
declare @sql nvarchar (4000)
set @count = 3
set @freeitems=(@count/3)
set @order_id = 20
--PRINT @freeitems
--PRINT @order_id
--PRINT @count
--print @discount_id
set @sql = 'select top '+ (select cast(+@freeitems as nvarchar))+' price
 from OrderItems
 where discount_id=@discount_id
 and order_id=@order_id order by price'
print @sql

exec sp_executesql @sql

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top