473,388 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

ASP check if table exists

Check table's existance

--------------------------------------------------------------------------------

How can I check if a table exists in an MS SQL database using asp.plz help me . advance thanx


<% Option Explicit %>
<% Response.Buffer=True %>
<!--#include File ="Include/Connection.asp" -->
<%
Dim bugId,found_by,module,submodule,details,found_date


objCon.Execute "Create table bugfound (bugId int IDENTITY(100,1) PRIMARY KEY ,found_by nvarchar(50),module nvarchar(50),"&_
"submodule nvarchar(50),details nvarchar(250),found_date Datetime)"

ObjCon.Close
Set ObjCon= Nothing %>

this code i hv written.
Jun 14 '06 #1
1 13626
sashi
1,754 Expert 1GB
Hi Jaya,

be more specific my fren.. how do you plan to check for the table existence on MSSQL?? are you planning to use stored prodecures or your planning to do it programatically??

there are few ways to do it..

method 1
SELECT * FROM INFORMATION_SCHEMA.TABLES

You can then loop through the resulting rows and search for the given table names or just add a WHERE condition.

method 2
Here's an easy way to check if a temp table exists, before trying to create it (ie. for reusable scripts)

*** MyTempTable = YourTableName ***

IF object_id('tempdb..#MyTempTable') IS NOT NULL
BEGIN
DROP TABLE #MyTempTable
END

CREATE TABLE #MyTempTable
(
ID int IDENTITY(1,1),
SomeValue varchar(100)
)
GO

method 3 - using vb/asp

'--------------------------Start Code----------------------

Public Function TableExists(ByVal strDB As String, strTable As String) As

Boolean

On Error GoTo Hell

'Create a Catalog object

Dim oCat As ADOX.Catalog

Set oCat = New ADOX.Catalog

oCat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " &

strDB

'Create a table object

Dim oTbl As ADOX.Table

Set oTbl = New ADOX.Table

Set oTbl = oCat.Tables(strTable)

'Return Success

TableExists = True

Exit_For:

'Clean up

Set oCat = Nothing

Set oTbl = Nothing

Exit Function

Hell:

GoTo Exit_For

End Function

'--------------------------End Code----------------------

ADO Method

'--------------------------Start Code----------------------

Public Function TableExistsADO(ByVal strDB As String, strTable As String) As

Boolean

On Error GoTo Hell

'Create a Catalog object

Dim RS As Recordset

Set RS = New Recordset

RS.Open "SELECT * FROM " & strTable & " WHERE 1=0", _

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & strDB

'Return Success

TableExistsII = True

Exit_For:

'Clean up

If RS.State = adStateOpen Then RS.Close

Set RS = Nothing

Exit Function

Hell:

GoTo Exit_For

End Function

'--------------------------End Code----------------------
Jun 18 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
3
by: DottingTheNet | last post by:
i promise no more silly questions after this but how do i check if a table exists?? it is my understanding that the exists can only be used in the where clause of the query. i just want s'thing...
2
by: adam | last post by:
hello What query shoul I send to SQL serwer ( in transact SQL language ) to check does some database exist on serwer ? It similar to problem "does some table exist in database" - resolve to it...
2
by: Chris Windsor | last post by:
I hope the following describe what I'm trying to do: I have created a tool to be used by product analysts when studying different cell phone designs. Part of the tool is a set of 11 forms on a...
10
by: Geoff Jones | last post by:
Hi I'm trying to drop a table by using: Dim cmd As New OleDbCommand("DROP TABLE IF EXISTS books", myconnection) cmd.ExecuteNonQuery() but I get a syntax error: "Syntax error in DROP TABLE...
2
by: RLN | last post by:
Re: Access 2003 I have code to check to see if a table exists. There are several other tables (tblQ12, tblQ13, etc) that do already exist & this code runs fine; msgbox says the value returned is...
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
0
by: bharathreddy | last post by:
This article will explain you how to check weather a column already exists in a table before you add the column to the table using alter command. Using the system tables...
4
by: qwedster | last post by:
Howdy folks! I am using stored procedure to see if a value exists in database table and return 0 if exists or else -1, in the following SQL queries. However how to check if a value (that is...
2
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.