Reputation: 1391
I am working on a project where I am tasked to create a series of Union queries to compile data from different databases with the same structure. Some of these queries are very wide with large number of columns. And nope, I cannot change the number of columns. When I tried to create the query it gives a collation error. I can solve that easily with this command added to the column:
COLLATE DATABASE_DEFAULT
Now, the issue is that I have to do 50+ queries each with UNIONs to 5 or 6 different databases and 200+ columns. Every time I add that command to the column I also have to add an alias to the column or I lose the name. Any ideas if there is a more efficient way to do this without having to add the COLLATE clause to every column?
Upvotes: 0
Views: 2287
Reputation: 24245
You can take advantage of SSMS Column Editing mode. Here is how that would work.
SELECT P .AB |
, QR.CDEFG
, S .HIJK
FROM P
JOIN QR ON ...
-- etc
SELECT P .AB |
, QR.CDEFG |
, S .HIJK |
FROM P
JOIN QR ON ...
COLLATE DATABASE_DEFAULT AS
, this text will appear on all lines at once:SELECT P .AB COLLATE DATABASE_DEFAULT AS |
, QR.CDEFG COLLATE DATABASE_DEFAULT AS |
, S .HIJK COLLATE DATABASE_DEFAULT AS |
FROM P
JOIN QR ON ...
AB
CDEFG
HIJK
SELECT P .AB COLLATE DATABASE_DEFAULT AS AB
, QR.CDEFG COLLATE DATABASE_DEFAULT AS CDEFG
, S .HIJK COLLATE DATABASE_DEFAULT AS HIJK
FROM P
JOIN QR ON ...
For more on this, see e.g. https://blog.sqlauthority.com/2016/06/06/sql-server-vertical-select-mode-feature-sql-server-management-studio/
Upvotes: 3