Solved

How would you modify a Stored Procedure from executing if a table does not exist ?

Posted on 2008-10-09
3
147 Views
Last Modified: 2013-12-05
I am developing an Access ADP application using Access as the front end and SQL Server as the back end database. I execute a Stored Procedure in the Attach Code Snippet that has 3 parts.

Is there a way to execute Part A and if the table in Part A doesn't exist, exit the Stored Procedure ?
If the table in Part A exists, then execute Parts B and C.
Part A)

SELECT       *

FROM         ztblRefresh
 

Part B) 

DELETE FROM ztblCustomers
 

Part C)

INSERT INTO ztblCustomers

SELECT      ztblCustomersNew.*

FROM        ztblCustomersNew

Open in new window

0
Comment
Question by:zimmer9
  • 2
3 Comments
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 200 total points
ID: 22681651
The code section below MAY work.  I don't know of it will parse the entire procedure at runtime though in which case it wouldn't work.  You would need to do something like this if not



if not exists (select 1 from information_schema.tables where table_name = 'ztblRefresh')
   return

exec up_DoStuff
go


WHERE up_DoStuff is

create procedure up_DoStuff as
Part A)
SELECT       *
FROM         ztblRefresh
 
Part B)
DELETE FROM ztblCustomers
 
Part C)
INSERT INTO ztblCustomers
SELECT      ztblCustomersNew.*
FROM        ztblCustomersNew

if not exists (select 1 from information_schema.tables where table_name = 'ztblRefresh')

   return
 

Part A)

SELECT       *

FROM         ztblRefresh

 

Part B) 

DELETE FROM ztblCustomers

 

Part C)

INSERT INTO ztblCustomers

SELECT      ztblCustomersNew.*

FROM        ztblCustomersNew

Open in new window

0
 
LVL 10

Expert Comment

by:dwe761
ID: 22682065
What does Part A actually do?  Does it create ztblRefresh?  Otherwise, it sounds like if ztblRefresh exists, you want to execute Parts B and C.  Is that correct?
If so, it'd look something like this:
 

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ztblRefresh]') AND type in (N'U'))

BEGIN
 

   Part B) 

   DELETE FROM ztblCustomers

 

   Part C)

   INSERT INTO ztblCustomers

   SELECT      ztblCustomersNew.*

   FROM        ztblCustomersNew

 
 

END

Open in new window

0
 
LVL 10

Accepted Solution

by:
dwe761 earned 300 total points
ID: 22682082
Oops.  Change that to:
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ztblRefresh]') AND type in (N'U'))
...
 
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now