select
[OrderTypeName]
,EXEC('SELECT TOP 1 [Status]
FROM dbo.Order_' + t_ros.
[OrderTypeName]) as [Status]
FROM dbo.OrderTypes t_ros
Sample Data:
OrderTypeName
-------------
ABC
XYZ
...
Views: dbo.Order_ABC, dbo.Order_XYZ....etc.
I have many kinds of orders that are stored in different tables. These tables have different structure and I am trying to get common structure using views that has the same name as the type of Order. I know the above code is not the correct way and is also showing me errors because I can't use EXEC inside select. But I am unable to find the correct way to achieve this. Please help.
2条答案
按热度按时间qoefvg9y1#
Credit to @abolfazl-sadeghi as this is basically just a fixed up version of what he suggested - instead of trying to use EXEC inside the query, instead build a union query dynamically then EXEC once.
As already discussed, this is obviously not an ideal way to be doing things, but I appreciate you are constrained by the existing design.
To improve the resilience of this going forward, I have also added a join to the system schema to verify that the views actually exist and have a [Status] column. That way if someone drops a view, or adds a bad entry to the ordertypes table, it will still work. It wont currently handle the case where you have a variety of column types for [Status] - you could work around that by converting to varchar in the "TOP 1" query - but obviously then the output would be a varchar, and that might not be what you want. You could also constrain to a column type.
bfhwhh0e2#
You need to put the whole thing into dynamic SQL, you cannot execute dynamic SQL in the middle of a query.
Note the use of
QUOTENAME
to correctly escape names.You can use
sp_executesql
to pass parameters to the dynamic query.