Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 With in SP create temp table

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2008-12-29 : 18:14:41
I am creating the following Sp, is it right to create a temp table inside the SP will it work.
this is my first time using this logic.


CREATE PROCEDURE [dbo].[Usp_GetCtrFundingSources]
@contractid int
AS

DECLARE @ElementID Int

DECLARE @CUR_TEMP CURSOR

SET @CUR_TEMP = CURSOR SCROLL FOR (select distinct(element_id) from tab_ccsnetelementcontracts where contractid=@contractid)

CREATE TABLE #TEMP
(
efundid INT,
fundid INT,
fund_source nvarchar(100)
)


OPEN @CUR_TEMP

FETCH FIRST FROM @CUR_TEMP INTO @ElementID
WHILE @@FETCH_STATUS = 0
BEGIN
insert into #TEMP(efundid,fundid,fund_source) select efund.efund_id,efund.fundid, fndsrc.fund_source from Tab_ccsNetEfund efund
inner join Tab_ccsNetFundSource fndsrc on efund.fundid = fndsrc.fundid
where efund.element_id=@ElementID

FETCH NEXT FROM @CUR_TEMP INTO @ElementID
End

Close @CUR_TEMP

Deallocate @CUR_TEMP

insert into #TEMP(efundid,fundid,fund_source) select efund.efund_id,efund.fundid, fndsrc.fund_source from Tab_ccsNetEfund efund
inner join Tab_ccsNetFundSource fndsrc on efund.fundid = fndsrc.fundid
where efund.element_id=@ElementID

select efundid,fundid,fund_source from #TEMP

Drop table #TEMP




Thank you very much for the helpful info.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-29 : 18:22:13
Yes you can create a temp table inside a stored procedure, that's usually where you'd use them anyway.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-30 : 00:53:03
i really cant understand purpose of using cursor here. Also why are inserting twice to temp table, one inside cursor loop and one outside? whats your actual requirement?
Go to Top of Page
   

- Advertisement -