0

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

2
  • Build your command first and then pass it to exec, i.e. declare @sqlcommand nvarchar(max);set @sqlcommand = 'this ' + that; exec(@sqlcommand) Commented Oct 24, 2016 at 6:01
  • @artm i did it at first place. But there will be additional where conditions to the query. in this case i used minimum conditions. as i know varchar(max) can hold only 8000 characters. i have doubt that with additional conditions this limit might exceed. That's why i tried to execute as above Commented Oct 24, 2016 at 6:07

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.