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

x
?
Solved

VB.net Refreshing SQL Views when new columns are added to a table

Posted on 2016-10-15
5
Medium Priority
?
70 Views
Last Modified: 2016-10-15
Hi

I have a SQL database that has multiple Views. When I add a new column into one of the tables used in these views this doesn't automatically refresh the Views. I have to go and manually recreate them. Having posed the question on whether this can be refreshed automatically I received a "no" as the answer. I  want to write VB.net code to do this. I need help with the best way to go about this.
0
Comment
Question by:Murray Brown
  • 2
  • 2
5 Comments
 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41844881
When you add a column or delete a column from the table and if your view uses * in the column list then use below command to update the view. You can easily do this in the SQL itself. Dont write the VB.NEt code. Try below once!!

ALTER TABLE yourtableName	
  ADD column_name INT;

EXECUTE sp_refreshview N'YourViewName'; 

Open in new window

0
 

Author Comment

by:Murray Brown
ID: 41844883
Thanks. I have to do this in VB.net because it is for the users of my app. Surely there is a way to do exactly what you are doing via VB.net
0
 
LVL 35

Accepted Solution

by:
Pawan Kumar earned 2000 total points
ID: 41844885
Yes if you want to do that in VB.NEt then call a SP and inside that use below. It will work.

EXECUTE sp_refreshview N'YourViewName';
0
 

Author Closing Comment

by:Murray Brown
ID: 41844887
Thanks very much. I appreciate the help
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41844976
buy using select * is not considered a best practice!
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

772 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