user2837480
user2837480

Reputation: 369

Dynamic SQL query Issue - SQL Server

I'm writing a dynamic query for paging purpose. below is the last part of the executable query and when i print it and run in different window it works fine. but when i use exec it shows red squiggle for casting parts (CAST(@pint_StartRowIndex AS VARCHAR)) as showing in attached image.

could anyone tell me why I'm getting this error? If i use Print instead of Exec it gives no errors and printed query execute with no errors.

enter image description here

Upvotes: 0

Views: 60

Answers (1)

James
James

Reputation: 729

try like this :

exec ('SELECT * from table where Rank BETWEEN CAST ('+ @start + 'as nvarchar)' + ' AND cast (' +  @end + 'as nvarchar)')

quote everything as string except @start and @end

Upvotes: 1

Related Questions