Saturday, 28 July 2018

When does a SQL Table think it's a View?

Try this one out:
  1. Create a SQL database
  2. Create a couple of tables
  3. Create a view that queries those tables and make sure to include a calculated column
  4. Create an empty table from the view using SELECT INTO...WHERE 1=0
  5. Drop the view
  6. Rename the empty table to the name of the dropped view
  7. Try and insert some data into the table
  8. Scratch your head...
Yup, SQL still thinks the object with the same name is a view and because it includes a calculated column it won't let you insert data into it.

To solve the issue, execute the sp_refreshview proc using the name of the new table.

No comments:

Post a Comment