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
Related
I'm trying to insert data in next table:
Table Of Data
I'm using Management Studio, but i don't know how to relate the file inserted with the column which has the ID number (in my case is named 'Batch'). All examples that I've found are like this without that column :
CREATE TABLE nameTable(fileReport varbinary(max))
INSERT INTO nameTable (fileReport) SELECT * FROM OPENROWSET(BULK 'c:\File1.pdf', SINGLE_BLOB) AS BLOB
Ok, if I understand your question correctly the answer would be something like this:
INSERT INTO nametable
(batch,
filereport)
VALUES ('batch_number1',
(SELECT *
FROM OPENROWSET(BULK 'c:\File1.pdf', single_blob) AS fileReport)
)
I think your issue is that you're not doing the VALUES clause on your INSERT statement
By the way, credit where it's due; I got that answer from this web page
I am trying to insert some data from my local table into linkedserver via sql server. this is what i am doing but it keeps on throwing syntax error.
TRY 1
EXEC(
'INSERT into test.testschema.testoperation
(viasatsubscriptionID, subscriptionrowdate, phonenumberday, viasatcustomerid)
SELECT * FROM rdata.dbo.testoperation'
)AT REDSHIFT64
TRY 2
EXEC(
'INSERT into test.testschema.testoperation
(viasatsubscriptionID, subscriptionrowdate, phonenumberday, viasatcustomerid)'
)AT REDSHIFT64
SELECT * FROM rdata.dbo.testoperation
Both fails.
Any thoughts where i am going wrong?
testoperation is your local table, and since your query runs on the remote server, the table does not exist.
Why don't you try inserting directly to the remote table:
INSERT into REDSHIFT64.test.testschema.testoperation
(viasatsubscriptionID, subscriptionrowdate, phonenumberday, viasatcustomerid)
SELECT * FROM rdata.dbo.testoperation
I want to import an Excel file into SQL Server but I face an error.
My code:
create table test
(
AccountNumber varchar(50),
AccountName varchar(50),
ParentAccountNumber varchar(50)
)
select *
into test
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [Sheet1$]')
Error:
Msg 7302, Level 16, State 1, Line 10
Cannot create an instance of OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)".
Note:
Excel version : 2007
SQL Server version : 2012
If you are not aware of writing scripts, you can do it by wizard itself.
Right click the DB name you want to import your excel file into. select Task-> Import.
New wizard will open and select the source as Microsoft excel and destination as Sql server, and give the authentication.
Rest all are self expainable.
You can preview the data before you import and you can control the columns you want to import.
Atlast click on finish to run the package.
Will acknowledge you for the successfull import.
Try this after table creation,
INSERT INTO test (AccountNumber , AccountName , ParentAccountNumber)
SELECT *
FROM OPENROWSET ('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [Sheet1$]');
Try running the below code:
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 1
GO
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 1
GO
select * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=D:\testing.xls;HDR=YES',
'SELECT * FROM [Sheet1$]')
Make sure your excel sheet is closed when you are running this code.
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.
I've created an ADO.NET connection manager, and a DataReader source with the following SQL Command:
select
'test' as testcol
INTO
#tmp
select * from #tmp
If I click the refresh button in the DataReader component, I get SqlException "Invalid object name #tmp". The SQL statment itself is clearly valid and executes properly in sql server management studio. I've also tried setting DelayValidation on the connection manager, to no avail.
is the error on the INSERT or the SELECT?
if you are issuing only one command that contains both the INSERT and SELECT, try putting a semicolon before the SELECT.
EDIT after OP comment
encapsulate all the logic within a stored procedure:
CREATE PROCEDURE YourProcedureName
AS
select
'test' as testcol
INTO
#tmp
select * from #tmp
GO
the have your application run this single SQL command:
exec YourProcedureName
EDIT after next OP comment
OP doesn't say which SQL Server version they are using, if 2005 or up, try a CTE:
;with CTEtemp as
(
select
'test' as testcol
)
select * from CTEtemp
Why couldn't this be replaced with a "SELECT 'test' as testcol"? The SSIS query parser may be having trouble with it because there's a temp table involved and it expects a single statement, not an actual SQL script. Or, if what you're sharing above is only an example for illustration, maybe something like this:
SELECT *
FROM (SELECT 'test' AS testcol)
Can you elaborate on what you're trying to accomplish here and, if it is, why the temp table is required?
Use sp_executesql
Your command would become
exec sp_executesql #statement=N'
select
''test'' as testcol
INTO
#tmp
select * from #tmp'
You must use nvarchar string (hence the N), and escape single quotes by doubling them.
I had the same problem as you and this is how I just fixed it.