ParamByName with temporary table

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
eduardomendes
Posts: 28
Joined: Wed 24 Feb 2010 14:08

ParamByName with temporary table

Post by eduardomendes » Wed 03 Nov 2010 18:48

When I try to perform,

SELECT * into #Temp_Test FROM dbo.Employee
WHERE Id = 00001 AND ((MONTH(LastDate) <= :pMonth and YEAR(LastDate) = :pYear) or YEAR(pLastDate) < :pYear)


It's drop the temporary table and I can't access it later.

But if I hard code the params on the query, it works fine.

Could you help me?

AndreyZ

Post by AndreyZ » Fri 05 Nov 2010 13:08

Hello,

This behaviour is connected with the specificity of SQL Server work with temporary tables. You can use this code:

Code: Select all

  UniQuery.SQL.Clear;
  UniQuery.SQL.Add('SELECT * INTO #Temp_Test FROM dbo.Employee');
  UniQuery.SQL.Add('WHERE Id = 00001 AND ((MONTH(LastDate) <= :pMonth and YEAR(LastDate) = :pYear) or YEAR(pLastDate) < :pYear)');
  UniQuery.SQL.Add('SELECT * FROM #Temp_Test');
  UniQuery.ParamByName('pMonth').AsInteger := 1;
  UniQuery.ParamByName('pYear').AsInteger := 2000;
  UniQuery.Open;
Also you can use global temporary tables.

Post Reply