Search code examples
sql-servercreate-table

Is it possible to create table using insert (select, update, delete)?


Due to some restrictions I am able to use SELECT, INSERT, UPDATE, DELETE. Is it possible to bypass CREATE TABLE using this?

I found the system tables and system views: sys.objects, INFORMATION_SCHEMA.COLUMNS

So it is possible to simulate this:

CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size)
);

by using something like this:

INSERT INTO INFORMATION_SCHEMA.COLUMNS (TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE) 
VALUES ('dbo','table_name','column_name1','data_type');
INSERT INTO INFORMATION_SCHEMA.COLUMNS (TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE) 
VALUES ('dbo','table_name','column_name2','data_type');
INSERT INTO INFORMATION_SCHEMA.COLUMNS (TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE) 
VALUES ('dbo','table_name','column_name3','data_type');

?

I am afraid that INFORMATION_SCHEMA is view and not a table but I hope there will be some way to do it.


Solution

  • Try this

    select '1' as column_name1 ,'Anand' as column_name2 ,'3' as column_name3 into table_name
    

    This creates the table table_name. but the data length is used as the values specified.so first column gets varchar(1) and 2nd column gets varchar(5)