这就是我想要关注CompanyName
:
SELECT 'Companyname', [Date], [Net Amount] FROM [Company1$Transaction Header]
其中Companyname
是FROM
条件[Company1$Transaction Header]
条件中的公司名称
这可能吗?
- 编辑
在检查Bluefeet建议后,我倾向于这样的事情:
您怎么看?
DECLARE @CompanyName
SET @CompanyName = (SELECT statement that gets the CompanyName)
SELECT @CompanyName, [Date], [Net amount], FROM [Company1@Transaction Header]
答案 0 :(得分:1)
如果要从表名中提取CompanyName,则可以使用information_schema
视图来获取数据:
select left(table_name, charindex('$', table_name)-1) Company
from information_schema.tables
where table_name ='Company1$Transaction Header';
然后你可以在查询中使用它:
select c.company,
date,
netamount
from [Company1$Transaction Header]
cross join
(
select left(table_name, charindex('$', table_name)-1) Company
from information_schema.tables
where table_name ='Company1$Transaction Header'
) c;
如果您提前知道公司和表的名称,那么您可以在查询中将公司名称包含为静态字符串:
select 'Company1' CompanyName,
date,
netamount
from [Company1$Transaction Header]