Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
arpana patil
Starting Member
24 Posts |
Posted - 2012-09-04 : 08:03:07
|
| How to create table along with columns dynamically in sql,while importing date from csv.Regards,Arpana |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-09-04 : 08:12:14
|
| Depends on how you are importing. For example, if you are using Import/Export wizard, it will by default, create the destination table automatically. To use import/export wizard, right-click on the database name in SSMS object explorer, select Tasks->Import Data and follow through the wizard. |
 |
|
|
anuraag205
Yak Posting Veteran
58 Posts |
Posted - 2012-09-04 : 08:12:44
|
| Create TestTable::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::USE TestDataGOCREATE TABLE CSVTest(ID INT,FirstName VARCHAR(40),LastName VARCHAR(40),BirthDate SMALLDATETIME)GOCreate CSV file in drive C: with name csvtest.txt with following content. The location of the file is C:\csvtest.txt1,......James,.......Smith,.........197501012,......Meggie,......Smith,.........197901223,......Robert,......Smith,.........200711014,......Alex,........Smith,.........20040202Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.BULKINSERT CSVTestFROM 'c:\csvtest.txt'WITH(FIELDTERMINATOR = ',',ROWTERMINATOR = '\n')GO--Check the content of the table.SELECT *FROM CSVTestGO--Drop the table to clean up database.DROP TABLE CSVTestGO::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::collected from, refer:::http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/Thanks |
 |
|
|
arpana patil
Starting Member
24 Posts |
Posted - 2012-09-04 : 08:18:16
|
| I am importing through bulk insert statement,i want table to be created dynamically with same column names as in csv and data type. |
 |
|
|
arpana patil
Starting Member
24 Posts |
Posted - 2012-09-04 : 08:19:41
|
Hi i dont want to create table manuallyquote: Originally posted by anuraag205 Create TestTable::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::USE TestDataGOCREATE TABLE CSVTest(ID INT,FirstName VARCHAR(40),LastName VARCHAR(40),BirthDate SMALLDATETIME)GOCreate CSV file in drive C: with name csvtest.txt with following content. The location of the file is C:\csvtest.txt1,......James,.......Smith,.........197501012,......Meggie,......Smith,.........197901223,......Robert,......Smith,.........200711014,......Alex,........Smith,.........20040202Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.BULKINSERT CSVTestFROM 'c:\csvtest.txt'WITH(FIELDTERMINATOR = ',',ROWTERMINATOR = '\n')GO--Check the content of the table.SELECT *FROM CSVTestGO--Drop the table to clean up database.DROP TABLE CSVTestGO::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::collected from, refer:::http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/Thanks
|
 |
|
|
|
|
|
|
|