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
Solved

Newbie Question Re: Stored Procedures/Views

Posted on 1998-08-06
6
202 Views
Last Modified: 2010-03-19
Here is my situation: I am using several views to consolidate data from a rather large table. The selection criteria is based on the contents of another table (that holds the names of the fields to be shown in the views.)

When a user makes a change to the selection criteria through my VB app, the selection criteria tables will be changed. This is a "trigger" to drop and recreate the existing views.  

Since I can't drop/create views in triggers or stored procedures, I was wondering if there is a way to call a VB exe from a stored procedure? Or if there is some better way to drop and create a view when a non-related table changes. I know I can do it through the program itself, but I would much prefer do it directly through sql to keep the views in sync with the selection criteria table.
0
Comment
Question by:trillian30
  • 4
  • 2
6 Comments
 
LVL 4

Expert Comment

by:mitek
ID: 1089431
I don't think you can create a view from an SQL Server trigger. The way I would approach this problem, would be creating an SQL stored procedure that would (using dynamic SQL, of course) drop and re-create the view, based on parameters passed to it.
This procedure would be called from VB app whenever "a user makes a change to the selection criteria"

Yes, there IS a way to call an external program from SP (xp_cmdshell) but I wouldn't even dare to use it to call anything larger than 'dir' command.

I would think, the way to do that is not "to call a VB exe from a stored procedure", but just the other way around. This way, it would be a simple call to SP, which would dynamicly build a DROP/CREATE VIEW statement and handle everything cleanly.

Again, can't give you an ANSWER, but a general plan of actions.

Tell me what you think about it.

BTW, that's NOT a newbie question.

0
 
LVL 4

Expert Comment

by:mitek
ID: 1089432
A really sick way of going about this problem would be to write a batch script for isql utility, that would drop/create the views, and call this file via xp_cmdshell from a trigger.
But only a sick (or really geniuos) mind could design something like THAT ...


0
 

Author Comment

by:trillian30
ID: 1089433
My understanding is that you also can not create a View in a stored procedure.  I'm not really sure what you mean by 'dynamic sql' though.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Expert Comment

by:mitek
ID: 1089434
I didn't say so. You CAN create a view from an SP, but it looks like it's not possible to call this SP from an SQL Server TRIGGER -- that's one of inherent limitations of triggers in SQL Server.

as for dynamic sql ? it's when you make a query within a stored procedure and then execute it with EXECUTE() command.

like that:

  DECLARE @01 varchar(255),@02 varchar(255),@03 varchar(255),@04 varchar(255)

  SELECT @01 = 'DROP VIEW testView '
  SELECT @02 = 'CREATE VIEW testView AS '
  SELECT @03 = 'SELECT * FROM ForumTopics '
  SELECT @04 = 'WHERE TopicSecurity=''MEM'''
  EXECUTE (@01)             -- drop the view
  EXECUTE (@02 + @03 + @04) -- recreate the view


0
 

Author Comment

by:trillian30
ID: 1089435
You are *so* fabulous! That worked perfectly! Do you want to resubmit so you can get the points for the answer?

0
 
LVL 4

Accepted Solution

by:
mitek earned 200 total points
ID: 1089436
Yep. Thank you very much. Let me know if you still have problems with it.

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

856 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