Running SQL Server 2005. A piece of software integrates with our database. Unfortunately they chose to make direct SQL queries instead of using stored procedures. Now we have changed our database scheme and the query in question is no longer valid.
Is it possible to patch T-SQL in runtime, on SQL Server?
E.g.
When SQL Server gets a SELECT A FROM InvalidTable for a specific database, the query would be converted to SELECT A FROM ValidTable just before it gets executed.
Edit:
This is the query in question:
SELECT * FROM DataTable c JOIN Users u ON u.UserName = @P0 AND c.DepartmentID = b2.DepartmentID WHERE c.Status = 0 where DepartmentID has moved to another table!
You can use Views and Synonyms to hide schema changes.
So for simple things (like
InvalidTableis nowValidTable), you can define a synonymFor more complex things (columns, JOINs, table splits, data types etc) you need a view
Edit, after OP realises they’ve bollixed themselves
You can’t have a view or synonym if a table exists with the same name in the same database.
So, use another DB and make the legacy app use that instead. In the examples, you’d have DataTable in another db with the assumption you can use another DB withoout changing code
Otherwise, rename the correct DB and point all non-legacy code to the renamed DB.