Write in Excel spreadsheet from SQL Server linked server - sql-server

I have a client who has a huge Excel file. They absolutely want to continue to work with this file. They asked us if we can update data in the file from a PocketPC.
I created a linked server to the spreadsheet:
EXEC master.dbo.sp_addlinkedserver
#server = N'ExcelFile',
#srvproduct=N'Excel',
#provider=N'Microsoft.ACE.OLEDB.12.0',
#datasrc=N'Filename.xls',
#provstr=N'Excel 12.0;IMEX=1;HDR=YES;'
I can successfully query the file with the following:
SELECT *
FROM ExcelFile...[Feuil1$]
If the file is already open, I get an error. I guess the file MUST be closed?
Anyway, is there a way to update cells in the Excel file with something like:
UPDATE ExcelFile...[Feuil1$]
SET [BIP] = 123456
WHERE [BIP] = '966985'
I get this error:
An error occurred while preparing the query "UPDATE Feuil1$ set BIP
= (1.234560000000000e+005) WHERE BIP=(9.669850000000000e+005)" for execution against OLE DB provider "Microsoft.ACE.OLEDB.12.0" for
linked server "ExcelFile".
Thanks for your time and help

Related

T-SQL Openquery - error due to space in the table name

I have the below query that is trying to pull data from Sage 50 pervasive 13 database into SQL server using a link server. I've been able to pull all data from all tables into SQL Server except for this one table because it has a space in the table name.
I've not been successful with anything I've changed it to. Can anyone help me get this query working?
select *
from openquery(ARKSAGE,'select * from NEPHROPATHOLOGYASSO1.Budget Details')
When I change the above query to this:
select *
from openquery(ARKSAGE,'select * from NEPHROPATHOLOGYASSO1.[Budget Details]')
I get this error message:
OLE DB provider "MSDASQL" for linked server "ARKSAGE" returned message "[PSQL][ODBC Client Interface][LNA][PSQL][SQL Engine]Syntax Error: select * from NEPHROPATHOLOGYASSO1.<< ??? >>[Budget Details]".
Msg 7321, Level 16, State 2, Line 61
An error occurred while preparing the query "select * from NEPHROPATHOLOGYASSO1.[Budget Details]" for execution against OLE DB provider "MSDASQL" for linked server "ARKSAGE".
The PSQL in the error message tells me the linked server is probably running Postgresql, rather than SQL Server. Postgresql marks object names with double quotes instead of brackets. Therefore you should try this:
select *
from openquery(ARKSAGE,'select * from NEPHROPATHOLOGYASSO1."Budget Details"')
Additionally, I'm not sure what the << ??? >> text is for, but it looks a little like it's complaining about an odd unicode character in there somewhere. So look out for invisible whitespace. Or maybe it's just part of how the error message is formatted in the context of a linked Postgresql server.

Stored procedure using PYODBC Not loading destination table in SQL Server

def UploadTable(table):
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};Server=XXXXXX;Database=XXXXXX;Trusted_Connection=yes')
cur = conn.cursor()
cur.execute("TRUNCATE TABLE dr.Imported_OM01TMP4_Data")
create_statement = fts.fast_to_sql(table, "dr.Imported_OM01TMP4_Data", conn, if_exists="append")
cur.execute("EXEC [dr].[PopulateGlAccountRevenue_Files_UltimateEdition_DeltaLoad]")
conn.commit()
conn.close()
Please see my code snippet above, I am trying to run this stored procedure [dr].[PopulateGlAccountRevenue_Files_UltimateEdition_DeltaLoad] that is already defined in SQL Server.
My code runs fine but when I check to see if the destination table in the server is loaded with the data from the table dr.Imported_OM01TMP4_Data, I am seeing a blank.
When I populate the same table with my python code but execute the stored procedure in SQL Server, the destination table is loaded properly. Is this a permissions / access issue? I have DB Owner Access/Read/Write as well, so I am not sure what is wrong with my code.
Please advise.

Inserting into a table from view create witha link server

I have created a view that includes a view from a linked server and I am inserting into a table. when I query the table it returns no data when the where statement is referencing data from the linkserver table. It does however pull all the data when not using the where statement. why is this for I do not have much experience working with LinkedServers.
Here is code I used to build my view i am using to populate my table.
ALTER VIEW [dbo].[weightsDashboard]
AS
SELECT *
FROM [dbo].[weightsYak]
UNION ALL
SELECT *
FROM OPENQUERY([10.3.50.62\AGJET], 'SELECT * FROM [CheckWeigher].
[dbo].[weightsSs]')
Then I ran this to create the dataset I want to report off of.
select *
into Dataset_SaleSummary
from [dbo].[weightsDashboard]
Thanks in advance.
The root cause Would be driver. Use ODBC instead of OLE DB.If you are trying to connect oracle database from sql server
Delete the linked server from SQL Server
Right click on the "Linked Servers" folder and select "New Linked Server..."
Linked Server: enter anything..this will be the name of your new linked server
Provider: Select "Oracle Provider for OLE DB"
Product Name: enter "Oracle" (without the double quotes)
Data Source: enter the alias from your TNSNAMES.ORA file.
Provider String: leave it blank
Location: leave it blank
Catalog: leave it blank
Now go to the "Security" tab, and click the last radio button that says "Be made using this security context:" and enter the username & password for your connection.
I had exact same problem with an SQL 2014 getting data from SQL 2000 through OPENQUERY. Because ODBC compatibility problem, I had to keep generic OLE DB for ODBC driver. Moreover, the problem was only with SQL non-admin account. So finally, the solution I found was to add SET ROWCOUNT 0:
SELECT * FROM OPENQUERY(DWH_LINK, 'SET ROWCOUNT 0 SELECT * FROM TABLEA ')
It seems the rowcount might been change somewhere through the SQL procedure (or for this user session), so setting it to 0 force it to return "all rows".

How to load data in different servers

I am designing an ETL project on SSIS and I want it to be dynamic. I will use this project for many customers therefore I will query these extractions against different servers.
For example, I have this query in a step with "execute SQL task" component :
INSERT DataWarehouse.schema.fact1
SELECT *
FROM Database.schema.table1
My datawarehouse is always in localhost But "Database.schema.table1" could be in different servers therefore I will have Different linkservers in our customer's servers to retrieve its data.
This means for example I will need the query change like this for customer1 :
INSERT DataWarehouse.schema.fact1
SELECT *
FROM [192.168.1.100].Database.schema.table1
And for customer2 I will need the query to be like this :
INSERT DataWarehouse.schema.fact1
SELECT *
FROM [10.2.5.100].Database.schema.table1
I've tried extract and loading with SSIS components but because of my complex queries, It became so messy.
Any ideas how to make my query dynamic?
As per this link Changing Properties of a Linked Server in SQL Server
One way to solve your problem is to make sure that the linked server logical name is always the same, regardless of what the actual physical host is.
So the process here would be:
Create the linked server with the linked server wizard
Use this to rename the server to a consistent name that can be used in your code
i.e.
EXEC master.dbo.sp_serveroption
#server=N'192.168.1.100',
#optname=N'name',
#optvalue=N'ALinkedServer'
Now you can refer to ALinkedServer in your code
A better way is to script the linked server creation properly - don't use the SSMS wizard
Here's the template - you need to do more research to fund out the correct values here
USE master;
GO
EXEC sp_addlinkedserver
#server = 'ConsistentServerName',
#srvproduct = 'product name',
#provider = 'provider name',
#datasrc = 'ActualPhysicalServerName',
#location = 'location',
#provstr = 'provider string',
#catalog = 'catalog';
GO
But the last word is: Don't use linked servers. Use SSIS
I would suggest you to do the below steps to execute same statement across multiple servers. As suggested by #Nick.McDermaid, I would strongly recommend against linked server. It is better to go for exact server name in SSIS.
Put the INSERT statement into a separate variable
Create a foreach container in SSIS.
Inside foreach containter, have a script task and get the current server name from the list of servernames. You can have comma separated list of servernames and get current one.
Again, inside foreach container, create Execute Process Task & call Sqlcmd.exe with connection information specific to each server, based on the server name got in Step No. 3, using SSIS expressions. Refer to this Stackoverflow post on using expressions for Execute ProcessTask for more information on calling Execute process task in SSIS.
How about making a SSIS package that works for one of your systems.
Parameterize your working package to accept a connection string
create another package that loops thru your connection strings and calls your working package and passes the conn string

Importing Excel Data into SQL Server 2008 R2 Express

There seems to have been many discussions on this but I couldn't find something specific to what I am looking for. I am trying to use a query to import data from Excel to an existing table in SQL Server.
My query is:
INSERT INTO DailyRawData
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml; HDR=NO;
Database=C:\Users\home\Desktop\SQLImportTrim.xls', [data$]);
I get the following error:
Msg 7314, Level 16, State 1, Line 2
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" does not contain the table "data$". The table either does not exist or the current user does not have permissions on that table.
I don't think this is a permission issue for me as I am set up as SysAdmn. I am wondering if the error is due to the last part of the query [data$] since this is what the error msg refers to. FYI the name of the excel file is SQLImportTrim and the tab that contains all my data is named data. There is no table named data in my Excel file. Is my query correct?
you don't use [data$] you use the name of the sheet, so the standard starting point is usually [Sheet1$] if you haven't renamed it.

Resources