Writing SQL queries can be considered by some as a form of art: everyone has a different style of writing and improving them (I’m talking about complex queries). Queries can achieve the same goal while being written in various and sometimes completely different way. While improving and optimizing a query should first and foremost include making sure that everything is written correctly and there are no logical errors, I wanted to share a tool, that some of you might not know, which can help make queries more efficient in times where you’re just not sure how to improve it further.
As part of my daytime job, I conduct professional interviews for candidates. For that, I’m always on the search for good interview questions. I regard a good question if it’s not too complicated, has a number of ways to solve, and if needed, can be solved programmatically with SQL or C / C# / JAVA languages.
Now that I’m done with asking the prime number question (I need to change questions every now and then), I thought I will post the solution here.
Have you ever wanted to create a copy of a database table, programmatically?
Could be a part of a stored procedure as a temporary table, or just a quick way of duplicating structure and constraints of the original one.
If so, this Transact-SQL script maybe what you’re looking for.