Thursday, August 2, 2007

Create Table using stored procedure

My sis was asking me how to create a table using stored procedure, well, I think it's better just jot down some notes here, for my future reference too.

 

Here is an example:

CREATE PROCEDURE CreateTestTable
AS
    
CREATE TABLE [TestTable]
(
    [Id] int NOT NULL Primary key identity(1,1),
    [Description] varchar(7000),
    [TestDate] datetime, 
    [Cost] money,
    [TestRole] char(4),
    [IsAdmin] bit
)
 
RETURN

* using brackets [] to embrace the table name or field name so that even reserved words have been used, the names are still allowed.


* the field name [Id] has been defined to be primary key and it's auto increment by 1, start from 1. (Null is not allowed for this column)


* the data type for boolean is "bit" here.


* the difference between using "char" & "varchar" is that, when you define the size for char, no matter how many characters have been stored, it'll use up all the space defined. But for "varchar", it'll allocate just the size enough to store the variables. For variable string, it's good to use "varchar" as the data type.


* The maximum size for varchar is 8000 characters.


* For Chinese characters, it's good to use "nvarchar" as the data type.


 


Well, I didn't really do a proper study on the notes above, these are told by my friends or from my experience. Hope they are not wrong information. Tongue out

No comments:

Post a Comment