开发者

Getting error while writing script to create a table

I am working on sql server 2005. I am writing a T sql script which will need to add a table to the database, but i want to add 开发者_如何学JAVAthe check that if the table is already created. But my script is giving me this error message:

Msg 102, Level 15, State 1, Line 7
Incorrect syntax near 'ON'.
Msg 2714, Level 16, State 6, Line 1
There is already an object named 'DeliveryChargeRegion' in the database.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'END'.


This code is similar to that generated by SSMS:

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[TABLE_NAME_HERE]') AND type in (N'U'))
BEGIN
    -- CREATE TABLE HERE
END


Another approach

if object_id('tablename') is NULL
BEGIN
    CREATE TABLE tableName

END

Nothing wrong with the sys.objects approach, I just find the above easier to write and read...


IF not  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TABLENAME]') AND type in (N'U'))

begin

-- Create table.

end

GO

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜