troubleshooting Question

Problems with stored procedure and SELECT INTO

Avatar of VB-tinkerer
VB-tinkerer asked on
Sybase Database
7 Comments3 Solutions2778 ViewsLast Modified:
In a stored procedure I try to use two conditional SELECT INTO statements which select into the same temporary table, but they create an error.

.
.
.
if @inst_type_id = '1'
begin
  select d.ORG_ID, d.REPORTING_TYPE_ID, d.REPORTING_PERIOD, d.FINANCIAL_PERIOD,
         #incremental.CREATION_ID, #incremental.CREATION_DATE, EXTRACT = 0
    into #dvc
    from MAIN..DVC d, MAIN..INSTALL_ID_ORG io, MAIN..INSTALLATION_ID i, #auth, #incremental
    where i.ME_IND = 'Y'
      and i.INSTALLATION_ID = io.INSTALLATION_ID
      and io.ORG_ID = d.ORG_ID
      and io.ORG_ID = #auth.ORG_ID
      and d.ORG_ID = #incremental.ORG_ID
      and d.REPORTING_PERIOD = #incremental.REPORTING_PERIOD
      and d.FINANCIAL_PERIOD = #incremental.FINANCIAL_PERIOD
end

if @inst_type_id = '2'
begin
  select ORG_ID, REPORTING_TYPE_ID, REPORTING_PERIOD, FINANCIAL_PERIOD,
         CREATION_ID = null, CREATION_DATE = null, EXTRACT = 1
    into #dvc
    from MAIN..ORDER_ITEM_DVC
    where ORACLE_STATUS = -1
end
..
.


When I create the procedure I get the error message:
There is already an object named '#dvc' in the database.

I know I can use a create table #dvc ... first and then insert the records, but I understand from a performance point of view, it is better to use SELECT  .. INTO

Can this be achieved in a stored procedure?

We use Sybase ASE 12.5

ASKER CERTIFIED SOLUTION
gletiecq

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 3 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros