Tables
are database objects that contain all the data in a database. In tables, data
is logically organized in a row-and-column format similar to a spreadsheet.
Each row represents a unique record, and each column represents a field in the
record. For example, a table that contains employee data for a company might
contain a row for each employee and columns representing employee information
such as employee number, name, address, job title, and home telephone number.
Tables can be created through SQL Server Management Studio and T-SQL command.
You need to check the object's existence before creating it to avoid above error as shown below:
Note: This is always a good idea to add some logging information while creating and dropping any object or executing any T-SQL code for that matter. Logging always helps in troubleshooting.
Tables can be created through SQL Server Management Studio and T-SQL command.
CREATE TABLE command is used to create a table in the database. For instance, following command can be used to create MyTable:
CREATE TABLE dbo.MyTable
(
ID int
,Name varchar(30)
,DoB date
,gender char(1)
)
If you execute the above command twice, you will get following error:
Msg 2714, Level 16, State 6, Line 1
There is already an object named
'MyTable' in the database.
You need to check the object's existence before creating it to avoid above error as shown below:
IF OBJECT_ID('dbo.MyTable') IS NOT NULL DROP TABLE dbo.MyTable
DROP TABLE dbo.MyTable
DROP TABLE dbo.MyTable
GO
CREATE TABLE dbo.MyTable
(
ID int
,Name varchar(30)
,DoB date
,gender char(1)
)
You will see following message after
executing above query:
Command(s) completed successfully.
Note: This is always a good idea to add some logging information while creating and dropping any object or executing any T-SQL code for that matter. Logging always helps in troubleshooting.
IF OBJECT_ID('dbo.MyTable') IS NOT NULL DROP TABLE dbo.MyTable
BEGIN
PRINT 'Dropping table dbo.MyTable...'
DROP TABLE dbo.MyTable
END
GO
PRINT 'Creating table dbo.MyTable...'
CREATE TABLE dbo.MyTable
(
ID int
,Name varchar(30)
,DoB date
,gender char(1)
)
Now you will see following messages
after executing above code:
Dropping table MyTable...
Creating table MyTable...
No comments:
Post a Comment