Link to home
Start Free TrialLog in
Avatar of faisal_n_k
faisal_n_k

asked on

Transfer of data from SQL to excel and vice versa

Hi Guys,

Hope you all are doing fine I have got some difficulty in doing something which I am doing the first time. I have a program writeen in Vb.net in which backend is SQL. The scenario is like that from this program I have to take some data and update the data on another computer and both of these computers cannot be connected to one another. So I thought it is best to export the data into excel and again import the data from excel to SQL. But I am not able to do that I got some code from the net but it is not working.

What I want to do is that I want to create a small application which will use SQL only to export the data ? I am using below mentioned code but it is giving some error.


CREATE PROCEDURE export_data AS

Insert into #temp select Reqdate from Requests where Reqdate = '2004/08/26'

insert into OpenRowset('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\Student.xls; HDR=No', [StudentData])
select ReqDate from #temp
go


This gives an error that either the table StudentData does not exist or you do not have the permission to write on that.

Anybody can help me out.
Avatar of BillAn1
BillAn1

Exporting to an Excel spreadsheet requires you to have the excel spreadsheet already existing, plus to have a 'table' with the correct column headers etc in the spreadsheet.
You will need to create the spreadsheet student.xls and create a named area in a sheet called StudentData, and this must have a header called Reqdate in order for this kind of export to work.

However, if all you want to do is to transfer the data, you would be best off using BCP to unload / load the data to a text file - it will be much faster, and the file will be created for you.
 
to unload the data use something like this :
exec master..xp_cmdshell 'bcp "select Reqdate from Requests where Reqdate = ''2004/08/26'' queryout "c:\test.txt" -c -q -U"user" -P"password"'

then to load back in again on the other side -
exec master..xp_cmdshell 'bcp "database.dbo.table" in "c:\test.txt" -c -q -U"user" -P"password"'
Avatar of faisal_n_k

ASKER

Thanks Bill for the reply but  when I am running the code that you have given in the query analyzer it is giving me output like this

usage: bcp {dbtable | query} {in | out | queryout | format} datafile
  [-m maxerrors]            [-f formatfile]          [-e errfile]
  [-F firstrow]             [-L lastrow]             [-b batchsize]
  [-n native type]          [-c character type]      [-w wide character type]
  [-N keep non-text native] [-V file format version] [-q quoted identifier]
  [-C code page specifier]  [-t field terminator]    [-r row terminator]
  [-i inputfile]            [-o outfile]             [-a packetsize]
  [-S server name]          [-U username]            [-P password]
  [-T trusted connection]   [-v version]             [-R regional enable]
  [-k keep null values]     [-E keep identity values]
  [-h "load hints"]
NULL


Secondly what I want to ask is that do i need to make a file called test.txt earlier then running the code and can you please make me understand the code what is -c, -p, what username and passw ord does it requires

Thanks in advance
The message you get is because there is some error in the format of the string you are using - can you paste the exact command? you ran?
just notriced, in the version I gave you there should be 1 more " -
exec master..xp_cmdshell 'bcp "select Reqdate from Requests where Reqdate = ''2004/08/26''" queryout "c:\test.txt" -c -q -U"user" -P"password"'
that is, the whole SQL statement should be enclosed in double quotes.
any part of the SQL that would normally have a single quote (') needs to be replaced by two single quotes ('') NOT a double quote (")
If you are still getting an error, can you paste your exact command you use and I can check it.

the format for bcp to create the file is :
exec master..xp_cmdshell 'bcp "this is where you put your SQL statement"  queryout "this is where you specify the file" -c -q -U"user" -P"password"'

the -c option means write the output as a character text file, using tabs to delimit fields, and \n to seperate rows
the -q otpion is really not necessary, but it lets you quote the name of the database - it is only required if you have any spaces etc in the name of the database etc.
the -U option is to specify a user. It is this user that the extract will happen as. This can be any user who has the rights to execute the SQL you want. If you are concerned about security, you could create a specific user which only has permissions to select from the tables you want, and no other rights. (and of course on the destination the user will need to be able to insert)
the -P option is to specify the password that this user uses to logon to SQLServer.

No, you don't need to create the text file - it will be automatically created - one of the reasons, apart from speed, to use this method rather than the Excel route...



SQLState = S0002, NativeError = 208
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Requests'.
SQLState = 37000, NativeError = 8180
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.
NULL


I am getting this error while running this command
When I give the username and password that I am using to run this program it gives some other error and when I give my username and password it gves the same error

The command that I am using is

exec master..xp_cmdshell 'bcp "select ReqDate from Requests where Reqdate = ''2004/08/26''" queryout "c:\test.txt" -c -q"IT_prog" -U"sa" -P"password"'

bcp:  unknown option I
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
  [-m maxerrors]            [-f formatfile]          [-e errfile]
  [-F firstrow]             [-L lastrow]             [-b batchsize]
  [-n native type]          [-c character type]      [-w wide character type]
  [-N keep non-text native] [-V file format version] [-q quoted identifier]
  [-C code page specifier]  [-t field terminator]    [-r row terminator]
  [-i inputfile]            [-o outfile]             [-a packetsize]
  [-S server name]          [-U username]            [-P password]
  [-T trusted connection]   [-v version]             [-R regional enable]
  [-k keep null values]     [-E keep identity values]
  [-h "load hints"]
NULL
ASKER CERTIFIED SOLUTION
Avatar of BillAn1
BillAn1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi guys,

  just wanna ask how to insert excel file to sql database using asp.net