Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine if table exist using Transact SQL

Status
Not open for further replies.

WIREMESH

Programmer
Mar 15, 2004
109
US
Using Transact SQL How can I determine if a table exists in a SQL Server database. If it exists, I want to use the ALTER TABLE command. If not. I want to use the CREATE TABLE command.
 
TEXT TO lcSql TEXTMERGE NOSHOW PRETEXT 15
IF object_id('MyTable') IS NOT NULL
/* table exists in a SQL Server database */
ALTER TABLE dbo.MyTable ADD newCol nchar(10) NULL
ELSE
CREATE TABLE [dbo].[MyTable](
[Id] [smallint] IDENTITY(1,1) NOT NULL,
[KodName] [nchar](10) NULL,
[newCol] [nchar](10) NULL
) ON [PRIMARY]
ENDTEXT
lnResult = SQLEXEC(m.lnHandle,m.lcSql)
ASSERT .f.
IF lnResult < 0
AERROR(laError)
MESSAGEBOX(laError(2,1))
ENDIF
 
Wiremesh,

Are you sure you're in the right place? Transact-SQL is the SQL dialect used by Microsoft SQL Server. It has nothing to do with Visual FoxPro.

If you want to use Visual FoxPro to determine if a given table exists on SQL Server, then use the SQLTABLES() function. See the VFP Help for details.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Mike,
I am using Transact SQL from a VFP program. The SQLTables command will do what I need.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top