Reputation: 5817
I have a stored procedure that returns a table. But it can return two kind of tables according to what condition it has like below
...
if @TestCondition > 0
begin
select *
from Test1 NoExpiredTable
end
else
begin
select *
from Test2 ExpiredTable
end
So in the application how can I get the table name ? What I tried is
if (ds.Tables[0].TableName == "NoExpiredTable")
{
}
but ds.Tables[0].TableName gives me "Table".
Upvotes: 0
Views: 323
Reputation: 51711
The result set has no concept of the table it came from, you can include the table name in the records returned by the procedure . . .
if @TestCondition > 0
begin
select *, 'NoExpiredTable' TableName
from Test1 NoExpiredTable
end
else
begin
select *, 'ExpiredTable' TableName
from Test2 ExpiredTable
end
Then you can access it the same way you'd access any other column.
This won't do anything for you if no rows are returned, you won't know which table was selected from.
Here you can return a single record with the result of the condition (i.e. a single row with a single column, TableName) then the records from the actual table. e.g.
select case when
if @TestCondition > 0
begin
select 'NoExpiredTable' TableName
select *
from Test1 NoExpiredTable
end
else
begin
select 'ExpiredTable' TableName
select *
from Test2 ExpiredTable
end
Upvotes: 3
Reputation: 161773
SQL queries do not return tables. They return result sets. A result set has no name.
Upvotes: 3