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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.