unable to read excel file in sql server - sql-server

I am creating a excel file using bcp command/xp_cmdshell in SQL Server 2005.
select #bcpquery =
'bcp "SELECT Column1 FROM Tablename" queryout '+
#fullFileName +' -c -t -U<username> -P<password> -S'+ ##servername
And I am not able to read the excel data using below statement
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=<PATH\filename.xls>;HDR=YES',
'SELECT * FROM [sheetname$]')
I am not able to read file, I believe this is happening due to unicode characters.
Could anybody help me?

It appears are though you're using BCP to copy the data out as a delimited file, whereas the OPENROWSET call is reading an Excel sheet. Excel is very forgiving when openings a CSV as XLS, but I have no idea if the Jet driver is the same way.
I would suggest using either BULK INSERT to read the delimited file, or ideally SSIS to both export AND import the data.

Related

Is it possible to import data into an existing CSV file using BCP in SQL Server?

I am using SQL Server 2014. I need to import data from my table into an existing CSV file using BCP command. By using BCP can we just create the file or we can even overwrite the existing file's data? I need to maintain the leading zeros of some fields. For this I am trying to change the column format into text before pushing the data. But as the file is recreating whenever I am executing the statement, the changes made to the file are getting discarded.
Below is my code..
SET #query = 'bcp "SELECT Data FROM tempdb..##tblData ORDER BY ID" queryout C:\Data\MedicalPolicy.csv -c -t, -T -S' + ##servername
EXEC master..XP_CMDSHELL #query
Can someone help me in maintaining the leading zeros in Excel CSV file?

Comparing 2 XML files in SQL

I am importing XML files into a SQL table as SINGLE_BLOBS:
INSERT INTO #XMLSource(XmlData)
SELECT *
FROM OPENROWSET (BULK C:\TEXT.XML, SINGLE_BLOB) AS ImportSource
does anyone know of a way to compare either the XML files prior to import or the SINGLE_BLOBS after import? I just need to see if the file has been changed.

Exporting data to excel from sql server is not working

I am using the below code to export data to excel file. This is working fine when the excel file is blank. But when I update the filed to null then the insert query is executed successfully but the excel is showing blank.
update openrowset('Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=E:\..\.xlsx;',
'select Column1,Column2,Column3 FROM [Sheet1$]')
set Column1=null,Column2=null,Column3=null
insert into OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=E:\..\.xlsx;', 'SELECT * FROM [Sheet1$]')
select Column1,Column2,Column3 from table_Name
I want set blank the file first before writing to that.
Kindly help.
From what I've read online, openrowset is read only so you need to use OPENDATESOURCE instead. Try something like this.
UPDATE OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0','Data Source=E:\...\YourExcelFile.xlsx;;Extended Properties=Excel 12.0')...[Sheet1$]
SET Column1=null,Column2=null,Column3=null
INSERT INTO OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0','Data Source=E:\...\YourExcelFile.xlsx;;Extended Properties=Excel 12.0')...[Sheet1$]
SELECT Column1,Column2,Column3 from table_Name

How to select specific column to import using BCP whithout configuration file?

I'm using BCP command to import, in SQL Server 2005, through of a configuration file. So:
EXEC master..xp_cmdshell 'BCP database.dbo.table in d:\folder\foo.csv -f d:\folder\configuration.xml -c -t, -T -F 2 '
I want to import certain columns without having to use the configuration file 'configuration.xml'.
why don't you try by query...
it results the data.... and you pass that to your table...
INSERT INTO tablename
SELECT * FROM
OPENROWSET ('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=D:\sam;', 'SELECT name,id from sam.csv');

convert database table into XML schema file

I am using SQL Server 2005. Is there any command or GUI tool (e.g. any menu/function from SQL Server management studio) to convert database table into XML schema file (.xsd)?
thanks in advance,
George
I've found this. Give it a try
Select CourseID, Name, CoursePrice
FROM CouseMaster.Course Course
FOR XML AUTO, XMLSCHEMA
You can write to file like this:
bcp.exe "select top 0 * from (select 1 as iCol) as t for xml auto, xmlschema" queryout outfile.xsd -T -c
I'm Using the TOP 0 to exclude the xml of the actual query data since you only want the schema. The -c causes it to be plain character data in the output, use -w instead if you want utf-16 (unicode) output.
EDIT - and if you want to change the xml structure, look at PATH with FOR XML.
Declare #SQL nvarchar(1000)
SET #SQL= 'bcp.exe '+ '"select * from yourdbname.yourschema.yourtablename for xml path (''record''), ROOT (''tabel'')"' +' queryout '+ 'c:\yourfilename.xsd' +' -w -r -t -SyourServerName -T'
print #SQL
EXEC Master..xp_CmdShell #SQL
Replace allvalues starts with 'your', accordingly

Resources