Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter value for a string that is inside Variable?

Can some one please help me how can I pass a Parameter value into a string that is inside a Variable

Ex:

DECLARE @STR VARCHAR(MAX) = ''

DECLARE @ID INT

SET @STR = 'SELECT * FROM STUDENT WHERE STUDENT_ID=@ID'

Here I want to pass @ID as a parameter to Variable @STR

like image 680
Umesh Avatar asked Nov 25 '25 17:11

Umesh


1 Answers

Use sp_executesql to value to the parameter this will also avoid sql injection. Try this

DECLARE @STR NVARCHAR(MAX) = ''

DECLARE @ID INT

SET @STR = 'SELECT * FROM STUDENT WHERE STUDENT_ID=@ID'

exec sp_executesql @str,'@ID INT',@ID
like image 194
Pரதீப் Avatar answered Nov 28 '25 07:11

Pரதீப்



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!