Code like this, but it's wrong:
CREATE PROC sp_createATable
@name VARCHAR(10),
@properties VARCHAR(500)
AS
CREATE TABLE @name
(
id CHAR(10) PRIMARY KEY,
--...Properties extracted from @properties
);
Could you tell me how to deal with it? It really troubles me.
This is a way to create tables dynamically using T-SQL stored procedures:
Execute it as:
You are using a table variable i.e. you should declare the table. This is not a temporary table.
You create a temp table like so:
You declare a table variable like so:
Notice that a temp table is declared using # and a table variable is declared using a @. Go read about the difference between table variables and temp tables.
UPDATE:
Based on your comment below you are actually trying to create tables in a stored procedure. For this you would need to use dynamic sql. Basically dynamic SQL allows you to construct a SQL Statement in the form of a string and then execute it. This is the ONLY way you will be able to create a table in a stored procedure. I am going to show you how and then discuss why this is not generally a good idea.
Now for a simple example(I have not tested this code but should give you a good indication of how to do it):
This stored procedure can be executed like this:
sproc_BuildTable 'Customers','CustomerName','VARCHAR(32)','NOT NULL'
There is some major problems with this type of stored procedure.
Its going to be difficult to cater for complex tables. Imagine the following table structure:
This table is a little more complex than the first example not a lot. The stored procedure will be much much more complex to deal with. So while this approach might work for small tables it is quickly going to be unmanageable.
Creating tables require planning. When you create tables they should be placed strategically on different filegroups. This is to ensure that you don't cause disk IO contention. How will you address scalability if everything is created on the primary file group.
Could you clarify why you need tables to be created dynamically?
UPDATE 2:
Delayed update due to workload. I read your comment about needing to create a table for each shop and I think you should look at doing it like the example I am about to give you.
In this example I make the following assumptions
Let say this e-commerce site sells gaming consoles (i.e. Wii, PS3, XBOX360).
Looking at my assumptions I see a classical many to many relationship. A shop can sell many items(goods) and items(goods) can be sold at many shops. Lets break this down into tables.
First I would need a shop table to store all the information about the shop.
A simple shop table might look like this:
Lets insert three shops into the database to use during our example. The following code will insert three shops;
If you execute a
SELECT * FROM Shop
you will probably see the following:Right so now lets move onto the Items(goods) table. Since the items/goods are products of various companies I am going to call the table product. You can execute the following code to create a simple Product table.
Lets populate the products table with some products. Execute the following code to insert some products.
If you execute
SELECT * FROM Product
you will probably see the following:Ok at this point you have both product and shop information. So how do you bring them together. Well we know we can identify the shop by its ShopID primary key column and we know we can identify a product by its ProductID primary key column. Also since each shop has a different price for each product we need to store the price the shop charges for the product.
So we have a table that maps the Shop to the product. We will call this table ShopProduct. A simple version of this table might look like this:
So lets assume the American Games R Us shop only sells American consoles, the Europe Gaming Experience sells all consoles and the Asian Games Emporium sells only asian we would need to map the primary keys from the shop and product tables into the ShopProduct table.
Here is how we are going to do the mapping. In my example the American Games R Us has a ShopID value of 1(this is the primary key value) and I can see that the XBOX360 has a value of 3 and the shop has listed the XBOX360 for $159.99
By executing the following code you would complete the mapping:
Now we want to add all product to the Europe Gaming Experience shop. In this example we know that the Europe Gaming Experience shop has a ShopID of 3 and since it sells all consoles we will need to insert the ProductID 1,2 and 3 into the mapping table. Lets assume the prices for the consoles(products) at the Europe Gaming Experience shop is as follows: 1- The PS3 sells for $259.99 , 2- The Wii sells for $159.99 , 3- The XBOX360 sells for $199.99.
To get this mapping done you would need to execute the following code:
At this point you have mapped two shops and their products into the mapping table. Ok so now how do I bring this all together to show a user browsing the website. Lets say you want to show all the product for the European Gaming Experience to a user on a web page you would need to execute the following query.
You will probably see the following results:
Now for one last example lets assume that your website has a feature which finds the cheapest price for a console. A user asks to find the cheapest prices for XBOX360.
You can execute the following query:
This query will return a list of all shops which sells the XBOX360 with the cheapest shop first and so on.
You will notice I have not added the Asian Games shop. As a exercise add the Asian games shop to the mapping table with the following products. The Asian Games Emporium sells the Wii games console for $99.99 and the PS3 console for $159.99. If you work through this example you should now understand how to model a many to many relationship.
I hope this helps you in your travels with Database design.
You will need to build that CREATE TABLE statement from the inputs and then execute it.
A simple example:
First up, you seem to be mixing table variables and tables.
Either way, You can't pass in the table's name like that. You would have to use dynamic TSQL to do that.
If you just want to declare a table variable:
The fact that you want to create a stored procedure to dynamically create tables might suggest your design is wrong.
You can write the below code:-
execute it as:-
exec spCreateTable