Solved

VB.net SQL Distinguish between a table and a view

Posted on 2014-10-24
5
118 Views
Last Modified: 2014-10-25
Hi

Is it possible to distinguish between a table and a view programmatically?
What VB.net code would I use?
Thanks
0
Comment
Question by:murbro
  • 2
  • 2
5 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 40402574
No idea about a VB.net answer, but the T-SQL answer would be...
SELECT name, 
   CASE type 
      WHEN 'U' THEN 'Table' 
      WHEN 'V' THEN 'View' 
      ELSE 'Something Else' END as object_type
FROM sys.objects  
WHERE type IN ('U', 'V') 
   and name = 'object name goes here'

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40402782
{Potentially stupid question}  Why do you ask?
0
 

Author Comment

by:murbro
ID: 40402884
Hi Jim
I have spent the last 3 years building an Excel add-in that is used to edit SQL data.
All the tables and views in a SQL database are loaded to a TreeView where the user
can manipulate them. When they  click on an item to edit I want the code to distinguish between a table and view
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 40403497
You can execute the following SQL command against the system tables:

SELECT xtype FROM sysobjects WHERE name='YourObjectName'

The result will be U for a table (User Table) or V for a View.
0
 

Author Closing Comment

by:murbro
ID: 40404495
Thank you both
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now