Solved

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

Posted on 2008-10-09
3
149 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

770 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