Skip to content
Advertisement

sql – union tables with same prefix

I have a set of tables with same prefix and same structure. All I need is to “combine” them as one.

Basically I retrieve tables with certain pattern from information_schema.tables, then get rid of the last union all.

Above method works for 20-30 tables, as the @result won’t exceed the limit for nvarchar. But I’m curious how to get this job done if number of tables N is very large? Or there’s better way to deal with this problem?

Advertisement

Answer

This should suffice. “Union all” replaced with “Go”.

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