Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team could you please help me is there any automation code for creating table when table name is given to create. all data types and constraints etc. will be available in a separate table.
a separate table is maintained where we have column names, data types for that table and required constraint information.
I need at SP or any other SQL code to create a table when I give table name, it should create that table with the given name.

It is something like below.
SQL
    CREATE PROCEDURE createtable
    (
    @TABLENAME as nvarchar (50)
     )
    AS
BEGIN
	DECLARE 
	--@TABLE_CATALOG nvarchar (50),
	@TABLE_SCHEMA nvarchar (50),
	@TABLE_NAME nvarchar (50),
	@COLUMN_NAME nvarchar (50),
	@DATA_TYPE nvarchar (50),
	@CHARACTER_MAXIMUM_LENGTH nvarchar (50),
    @SQL nvarchar(2000)
    --SET @SQL = "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[@TABLENAME]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    --drop table [dbo].[@TABLENAME]
    BEGIN
	
    SELECT @SQL = 'CREATE TABLE ' + @TABLENAME + '('
    SELECT @SQL = @SQL + 'SELECT @TABLE_CATALOG = TABLE_CATALOG,@TABLE_SCHEMA = TABLE_SCHEMA,@TABLENAME = TABLE_NAME,@COLUMN_NAME = COLUMN_NAME,@DATA_TYPE = DATA_TYPE,@CHARACTER_MAXIMUM_LENGTH = CHARACTER_MAXIMUM_LENGTH FROM information_schema.columns WHERE TABLE_NAME = @TABLENAME)'
	
    --CONSTRAINT [PK_FinancialPeriod' + @TABLENAME + '_1] PRIMARY KEY CLUSTERED(
    --[FinPeriodNr] ASC
	
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
	EXEC(@SQL)
	END
	END
    --GO
    --SET NOCOUNT ON;
Posted
Updated 11-Dec-14 0:55am
v3
Comments
Tomas Takac 11-Dec-14 5:16am    
AFAIK there is nothing like this in SQL Server. What database are you using BTW? Please update your tags.
Tomas Takac 11-Dec-14 7:06am    
Apparently you already have that SP. So what's the problem, where are you stuck?
Member 11302957 12-Dec-14 1:04am    
Thanks Tomas, I searched the sp in online, its not working, is there anything I need to add to work properly.

You an do that in SQL dynamically as below

SQL
Create Table #columnNames(columnName varchar(40),ColumnDataType varchar(40),ColumnWidth varchar(10))
        insert #columnNames select 'col1','varchar','10'
        insert #columnNames select 'col2','int','0'

        Declare @Query varchar(Max),@tableName varchar(20),@ColName varchar(200)
        set @tableName='#Table1'


        select @ColName=Coalesce(@ColName+', ','')+Cast(a.columnName as varchar(20))+' '+
                Cast(a.ColumnDataType as varchar(20))+
                (Case when a.ColumnDataType in ('varchar','nvarchar') then '('+a.ColumnWidth+')' else '' end)
        from #columnNames a

        set @Query='Create Table '+@tableName+' ('+@ColName+')'


        Sp_execute exec (@Query)
 
Share this answer
 
you want to make copy of a table structure with different schema name??
then see this..
http://dba.stackexchange.com/questions/53085/is-there-a-way-to-generate-table-create-script-in-tsql[^]
 
Share this answer
 
I think you can use the below code snipet:
string myConnectionString= @"DataSource =...";

SqlConnection dbConnection = new SqlConnection(myConnectionString);

string myCommand = "CREATE TABLE myTable (column1 VARCHAR(10),colunm2 INT)";

SqlCommand dbConnection = new SqlCommand(myCommand,dbConnection);

try
{
dbConnection.Open();

dbCommand.ExecuteNonQuery();
}

catch{}

dbConnection.Close();


And the command string you can build dynamically every time by fetching the data from defination table.
 
Share this answer
 
Comments
Tomas Takac 11-Dec-14 7:05am    
This is not what the OP is asking.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900