Skip to content
Advertisement

Adding Variables to SQL queries

I have the following code which returns a table from my ms sql Database

How would I add the one_yrs_ago variable to the SQL query in this situation , like so :

Advertisement

Answer

Not seeing the full context but this is how I would do it in SQL Server

I would say parameterize what you pass in is important as otherwise you could get SQL injection!

Full example to play with, note that you declare the parameters and their types and then pass the actual values you want in all on that one line

Little example to run below:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement