0

这就是我想做的重点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]
4

1 回答 1

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]
于 2013-04-19T10:11:44.780 回答