I'm working on creating some stored procedures to automatically mirror data from multiple servers/databases (40+) into one central server. I have created a table that has the column names from the databases that I am referencing like this:
What I'm wanting to do, is essentially grab the COUNT(ColumnID)
based on the @TableID
table variable that I declare. From there, the central server will already have corresponding columns for each of these sets of columns and the reference table also has the name of the central databases columns for each of these columns listed in the same table for the TableID
and ColumnID
. I want to pull these column names into an array and/or string where I can EXEC a dynamic Query such as EXEC('SELECT '+@ColumnsString+' FROM [LinkedServer].'+@TableName+'');
I already have the dynamic Linked Server's setup and working. But I'm looking for a way to either store the multiple column names into a single array that I can reference to create a string variable and also to reference for UPDATE queries. IE: SET @ColumnString = (@arrayvalue[1]','+@arrayvalue[2]+','+@arrayvalue[n]+'');
Is there a function available in SQL Server that could accomplish this?