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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Munesh SharmaRank #174 · 13.9K reputationPosted Nov 11, 2014, 8:02 PM EST
1ST CREATE connection SqlConnection cn=new SqlConnection(@"server=MUNESH\SQLEXPRESS;database=master;integrated security=SSPI"); cn.Open(); //here you create your table SqlCommand cmd=new SqlCommand("create table '" +user1+ ,cn);Joginder BangerRank #360 · 7K reputationPosted Nov 11, 2014, 8:17 PM EST
Ramchand RepalleRank #762 · 3K reputationPosted Nov 11, 2014, 8:26 PM EST
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.
Abhay ShankerRank #375 · 6.7K reputationPosted Nov 11, 2014, 10:36 PM EST