Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Fastest method for union of two selects
Message
 
To
24/11/2006 11:14:49
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Environment versions
SQL Server:
SQL Server 2000
Miscellaneous
Thread ID:
01171624
Message ID:
01172282
Views:
19
This message has been marked as the solution to the initial question of the thread.
>This is the exact stored procedure I am trying
>
>*********************************************************************
>
>CREATE proc get_packset_ledger_sp
> @p_packsetid char(13),
> @p_shetkariid char(13)
>as
>CREATE TABLE #MyTempTable (date datetime, packsetid char(13) , shetkariid char(13), inqty float, outqty float)
>
>INSERT INTO #MyTempTable select outdt as date ,packsetid, shetkariid, sum(qty) as inqty, 0.000 as outqty from packout where packsetid = @p_packsetid and shetkariid = @p_shetkariid group by packsetid, shetkariid, outdt order by packsetid, shetkariid, outdt
>
>INSERT INTO #MyTempTable select frtinhdr.indate as date , frtindtl.packsetid, frtinhdr.shetkariid, 0.000 as inqty, sum(frtindtl.boxrecdqty) as outqty
>from frtindtl left join frtinhdr on frtindtl.hdrid = frtinhdr.id
>where frtindtl.packsetid = @p_packsetid and frtinhdr.shetkariid = @p_shetkariid
>group by frtindtl.packsetid, frtinhdr.shetkariid,frtinhdr.indate order by frtindtl.packsetid, frtinhdr.shetkariid,frtinhdr.indate
>
>select date, packsetid, shetkariid, sum(isnull(inqty,0.000)) as inqty, sum(isnull(outqty,0)) as outqty from #MyTempTable group by date, packsetid, shetkariid order by date, packsetid, shetkariid
>GO
>
>*********************************************************************

No needs of temporary table, I rarely use temp tables, usually when I need to store a results from some other SP. I use TABLE VARIABLE instead (fast and they are not physically created on disk). In your case you didn't need any table creation:
CREATE proc get_packset_ledger_sp
   @p_packsetid char(13),
   @p_shetkariid  char(13)
as
CREATE TABLE #MyTempTable (date datetime, packsetid char(13) , shetkariid char(13), inqty float, outqty float)
SELECT date,
       packsetid,
       shetkariid,
       sum(isnull(inqty ,0.0)) as inqty,
       sum(isnull(outqty,0.0)) as outqty
FROM (select outdt as date,
             packsetid,
             shetkariid,
             qty   as inqty,
             0.0   as outqty
      from packout
      where packsetid  = @p_packsetid  and
            shetkariid = @p_shetkariid
      UNION ALL
      select frtinhdr.indate as date,
             frtindtl.packsetid,
             frtinhdr.shetkariid,
             0.0 as inqty,
             frtindtl.boxrecdqty
      from frtindtl
      left join frtinhdr on frtindtl.hdrid = frtinhdr.id
      where frtindtl.packsetid = @p_packsetid and
            frtinhdr.shetkariid = @p_shetkariid) Tbl1
GROUP BY date, packsetid, shetkariid
order by date, packsetid, shetkariid
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform