Delphi查询中的临时表

时间:2019-09-20 08:58:05

标签: sql-server tsql delphi temp-tables

我正在尝试将其合并到临时表中。您在这里看到语法错误吗? 当我执行它时没有显示任何问题,但是当我尝试引用此查询时,它显示“无效的表名#Temp_trans_13”(我使用的是MSSQL)。

select * into #temp_trans_13 from (
select 
      periode,
      konto_nr as ktonr,
      konto_kurztext as ktobez,
      soll_periode - haben_periode as SaldoMonat,
      soll_saldo - haben_saldo as SaldoJahr

from
      sap_SuSa_liste
where
      Jahr = :jahr
and   periode = 12
and   buchungskreis = :bkr
and   konto_nr between '200000' and '999999'
union
select
      periode,
      konto_nr as ktonr,
      konto_kurztext as ktobez,
      soll_periode - haben_periode as SaldoMonat,
      soll_saldo - haben_saldo as SaldoJahr

from
      sap_SuSa_liste
where
      Jahr = :jahr
and   periode = 13
and   buchungskreis = :bkr
and   konto_nr between '200000' and '999999'
union
select
      periode,
      konto_nr as ktonr,
      konto_kurztext as ktobez,
      soll_periode - haben_periode as SaldoMonat,
      soll_saldo - haben_saldo as SaldoJahr

from
      sap_SuSa_liste

where
      Jahr = :jahr 
and   periode = 14
and   buchungskreis = :bkr
and   konto_nr between '200000' and '999999')t 

3 个答案:

答案 0 :(得分:2)

看起来整个查询可以简化为:


select 
      periode,
      konto_nr as ktonr,
      konto_kurztext as ktobez,
      soll_periode - haben_periode as SaldoMonat,
      soll_saldo - haben_saldo as SaldoJahr
into #temp_trans_13 
from sap_SuSa_liste
where
      Jahr = :jahr
     and   periode in (12,13,14)
     and   buchungskreis = :bkr
     and   konto_nr between '200000' and '999999'

UNION返回不同的值,这意味着,即使SQL Server的查询优化器足够聪明,可以避免执行N个查询,它仍然 still 必须对数据进行排序和分组以消除重复。 / p>

如果需要重复消除,则可以将DISTINCT添加到SELECT子句

临时表

临时表是临时的。它们仅在单个连接期间存在。前缀为#的服务器仅对创建它们的连接可见。

仅当另一个查询尝试使用该表而找不到该表时,才会引发错误invalid table name #Temp_trans_13,这可能是因为尚未创建该表,或者是因为该表是在一个不同的连接。

要使用来自另一个连接的临时表,需要通过使用##作为前缀来创建global temporary table

答案 1 :(得分:2)

在这种情况下,我发现尝试将问题归结为基本内容的查询很有帮助。

使用SSMS 2014,我有一个Table1,其中包含一个名为Int的{​​{1}}列。

如果我尝试

ID

我得到了错误

  

第15级,州1,第1行的消息102   ')'附近的语法不正确。

如果我尝试也会收到相同的错误消息

select * into #mytemp from (select * from table1)

但是,请遵循 R部分中的示例。使用派生表 TransactSql docs 此查询

select ID from (select * from table1)

查询正确执行并产生预期的单列结果集。

进一步执行

select ID from (select * from table1) as derivedtable

正确执行并产生预期结果。

因此,显然select * into #mytemp from (select * from table1) as derivedtable 使 as xxxx构造无法正常工作。

答案 2 :(得分:0)

这将解决您的问题:

declare 
   @bkr varchar(10),
   @jahr int

set @bkr = :bkr
set @jahr = :jahr

select T.* 
into #temp_trans_13 
from     
(
select 
      periode,
      konto_nr as ktonr,
      konto_kurztext as ktobez,
      soll_periode - haben_periode as SaldoMonat,
      soll_saldo - haben_saldo as SaldoJahr

from
      sap_SuSa_liste
where
      Jahr = @jahr
and   periode = 12
and   buchungskreis = @bkr
and   konto_nr between '200000' and '999999'
union
select
      periode,
      konto_nr as ktonr,
      konto_kurztext as ktobez,
      soll_periode - haben_periode as SaldoMonat,
      soll_saldo - haben_saldo as SaldoJahr

from
      sap_SuSa_liste
where
      Jahr = @jahr
and   periode = 13
and   buchungskreis = @bkr
and   konto_nr between '200000' and '999999'
union
select
      periode,
      konto_nr as ktonr,
      konto_kurztext as ktobez,
      soll_periode - haben_periode as SaldoMonat,
      soll_saldo - haben_saldo as SaldoJahr

from
      sap_SuSa_liste

where
      Jahr = @jahr 
and   periode = 14
and   buchungskreis = @bkr
and   konto_nr between '200000' and '999999'
) T