I am trying to make a query that I can run from Python with dynamic table name and date. In the process of this, I have tried the following query in SSMS, but it is producing an error message. How can I use variables for table name and a date, and get the query to work?
DECLARE @table_name VARCHAR(50)='table_name';
DECLARE @valid_to datetime = getdate();
EXEC('UPDATE '+ @table_name + '
SET valid_flag = 0,
valid_to = '+ @valid_to +'
where valid_flag=1')
I get the following error message:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '10'