sql server - Dynamically Changing what table to select from with SQL CASE statement -


I am trying to write a stored procedure and based on a certain column value, I was able to change that table I want to be select from I will try to give an example:

  select item number, item type, item item from case when 'A' then table ALS tableb and where the case Item type when 'A' item number = @ Atom number ELSE PartNumber = @itemNumber END  

As You can see, not only do I change the table dynamically, which I choose, but since these two tables were created by two different people at two different times, the names of columns also vary Are there.

So, my question is: what is the best way to accomplish this, because SQL Server does not like to create my query.

If anybody who sees what I am trying to do, then a better way of doing this may suggest, I am in my ears: -)

< P>

You can not use the CASE statement in the FAR section, but you can use the following instead:

  SELECT itemnumber, itemtype, table with descriptions where WHERE itemNumber = @itemnumber and item type = 'A' Union SE LECT item number, item type, details from table, where partnumber = @itemnumbe R and item type & lt; & Gt; 'A'  

Comments