Solved

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

Posted on 2008-10-09
3
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

615 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