sql server - Is there a UNION equivalent to FULL OUTER JOIN in T-SQL that will union dissimilar column sets? -
I am trying to complete the following:
select col1, col2 Select table1 union with Col2, col3, col3, col3, col2, col3, col3, col3 1, 1, 1, 1
with result:
Columns ofcolumns and are returned to the lines. You can think about it that there is a complete external unions consonant.
The simple answer to this question is:
select col1, col2, null as col3 to select table1 unions, tap ac col1, col2, col3 to table 2
This, however, is difficult to do with dynamic T-SQL and dbo.sp_executesql, and the resulting query can be very long, comprising a large number of units.
I could come up with the best solution to create a temporary table with all possible columns and put each selection in the temporary table, such as:
create table Insert in #temp (col1 int, col2 int, col3 int) #temp (col1, col2), select col1, from col2 to # 1, select (col2, col3), select col2, col3 to table 2
But for this it is necessary to know ahead of time what the names of the columns are. My special scenario and this question assumes that if the matches in the name of the column match then the match is also fine. In fact, the columns I am trying to manipulate are all of the same type.
Is there an easy way to do this?
Thank you!
My interpretation of your question is that you want to be able to support an add-on hoc query Dynamic Sql & amp; Column name & amp; Data type ...
This query will give you a list of columns & amp; For a specific table, type their data type:
SELECT @columnName = column name as c.name, @columnDataType = ty.name SAS.TABLES type column data type JOIN SYS. COLUMNS C ON c.object_id = Ta.object_id JOIN SYS.TYPES ty tyus.user_type_id = c.user_type_id WHERE ta.name = '[your_table_name]'
this @columnName < Populates / code> & amp;
@columnDataType
variables so that you can use them somewhere else.
You have to give the name of the column and name of the table, which is in the minimum in order to get meaningful data, after a single column, it is argued that what you need to do to create a curie It is a matter of creation.
Most situations will have to determine what is the required query - The unfortunate part is that when you ask, the answer is usually "everything" for that reaction, I Keep things out so that you need the amount of money to support everything. Time. Stress that it is cheap & amp; Fast to break the requirement in manageable parts - this will let you be able to get feedback. It is possible that no one will ever use a small subset of functionality, or it does not require all of the user. Learn how to manage client expectations.
Comments
Post a Comment