Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-09
3
Medium Priority
?
158 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 800 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 1200 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

926 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