create table for every user
I want create tables for every user.there are so many customers.how can i do it?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Abhay ShankerPosted Nov 12, 2014, 3:36 AM
Ramchand RepallePosted Nov 12, 2014, 1:26 AM
As my personal view, create a table for every customer is not a good idea, even though i don't know about your full requirement.
So, you can achieve in one of the way at SQL server level is:
1. First insert all the customers in one table
2. create a stored procedure like the below
CREATE PROCEDURE CustomerTableCreation(
@Name NVARCHAR(100)
)
AS
BEGIN
DECLARE @SQL NVARCHAR(1000)
SET @SQL = 'CREATE TABLE ' + @Name + '(Id INT)'
EXEC(@SQL)
END
3. create one cursor it gets the customer names and create a table for all customer names
DECLARE @name VARCHAR(100)
DECLARE db_cursor CURSOR FOR
SELECT distinct Name
FROM Customer
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC CustomerTableCreation @name
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
Note: The customer name/id should satisfy the table name conditions in sql server (Table Name Format, Unique Table Name etc)
If you want to achieve through code level,
you can call the stored procedure (mentioned in 2nd point) from code level. it would creates a table for you..
Thanks,
Ramchand.
Joginder BangerPosted Nov 12, 2014, 1:17 AM
Munesh SharmaPosted Nov 12, 2014, 1:02 AM