I need to execute a stored procedure by job agent, that sp connects to 'Microsoft.ACE.OLEDB.12.0' and gets data from excel sheet.
when I execute sp, It's work. but when execute it by job agent it doesn't work.
My code is here:
First I wrote a return value procedure to get excel sheet name:
ALTER PROCEDURE [dbo].[spExcelSheetName]
#TableName nvarchar(250) output
AS
declare #table Table (TABLE_CAT nvarchar(50), TABLE_SCHEM nvarchar(50), TABLE_NAME nvarchar(50), TABLE_TYPE nvarchar(50), REMARKS nvarchar(50))
insert into #table
EXECUTE SP_TABLES_EX 'ExcelSource';
select #TableName = TABLE_NAME from #table
Then a sp for getting data from excel sheet and insert into my sql table:
ALTER PROCEDURE [dbo].[spConvertExcel]
AS
begin
IF EXISTS(SELECT * FROM sys.servers WHERE name = N'ExcelSource')
EXEC master.sys.sp_dropserver 'ExcelSource','droplogins'
EXEC sp_addlinkedserver 'ExcelSource', '',
'Microsoft.ACE.OLEDB.12.0',
'D:\Test\Personel.xlsx',
NULL,
'Excel 8.0'
EXEC sp_addlinkedsrvlogin 'ExcelSource', 'false'
EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
declare #SheetName nvarchar(250)
exec spExcelSheetName #TableName = #SheetName OUTPUT
declare #Query nvarchar(max)
set #Query =
N'SELECT *
FROM OPENROWSET(''Microsoft.ACE.OLEDB.12.0'',
''Excel 12.0 Xml; HDR=YES;Database=D:\Test\Personel.xlsx'',
['+#SheetName+']);'
delete from ExcelPersonels
insert into ExcelPersonels
exec sp_executesql #Query;
delete from Personels
where personelcode in (select personelcode from ExcelPersonels)
update Personels set IsActive = 0
insert into Personels
select *, 1 from ExcelPersonels
end
This sp run without any problem, but when I want to run it by Job agent it doesn't work :(
job code is : exec (spConvertExcel)
Even I tried: osql –E –Q “exec spConvertExcel”
but it didn't work too.
error massage is:
Message
Executed as user: NT SERVICE\SQLAgent$SQL2014. Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "ExcelSource". [SQLSTATE 42000] (Error 7303) OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "ExcelSource" returned message "Unspecified error". [SQLSTATE 01000] (Error 7412). The step failed.
thanks for all replies.
Related
I am trying to copy all rows of a table from the database of my local machine to linked server database.
This is the query I am using:
DECLARE #Qry nvarchar(MAX)
DECLARE #Server nvarchar(50)
SET #Server = '[LINKEDSERVER]'
SET #Qry = '
DECLARE #Qry2 nvarchar(max)
SET #Qry2 = N''
SET IDENTITY_INSERT RDB.dbo.Type ON
insert into RDB.dbo.Type (id, Name) Select ID,Name From
[LOCALSERVER].localdb.dbo.Type
SET IDENTITY_INSERT RDB.dbo.Type OFF''
EXEC ' + #Server + '.RDB.dbo.sp_executesql #Qry2'
EXEC SP_EXECUTESQL #Qry
But I am getting this error - please help what to do here to make it work:
Could not find server 'LOCALSERVER' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.
and If remove the [LOCALSERVER] then I get:
Invalid object name 'localdb'.
Please help if any other dynamic query can work to copy table data from local server to linked server.
You are trying to run the sql statement in remote linked server. In that remote linked server, there is no linkedserver(entry in sys.servers) defined for your local server. Hence, you are getting the error.
You can define linked server for your server in the remote machine and execute statement.
DECLARE #Qry nvarchar(MAX)
DECLARE #Server nvarchar(50)
SET #Server = '[LINKEDSERVER]'
SET #Qry = '
DECLARE #Qry2 nvarchar(max)
SET #Qry2 = N''
SET IDENTITY_INSERT RDB.dbo.Type ON
insert into RDB.dbo.Type (id, Name) Select ID,Name From
[LOCALSERVER].localdb.dbo.Type
SET IDENTITY_INSERT RDB.dbo.Type OFF''
EXEC ' + #Server + '.RDB.dbo.sp_executesql #Qry2'
EXEC SP_EXECUTESQL #Qry
I want to create a dynamic query in SQL Server which will run on linked server. I am trying to do it as follows.
USE [MYDB]
GO
DECLARE #company AS nvarchar(50);
DECLARE #id nvarchar(MAX);
DECLARE #query nvarchar(MAX);
SET #company = 'mycompany.com';
SET #query = N'SELECT #csid = id FROM OPENQUERY(LINKSERVER12,
''SELECT id from company where name = #comp'')';
EXECUTE sp_executesql #company_query, N'#comp nvarchar(50), #csid
nvarchar(MAX) OUTPUT', #comp = #company,#csid = #id OUTPUT
In the above script, I want to pass the value for #comp dynamically. For that I tried setting input and output variable while executing SQL with sp_executesql.
I am getting the following error
Syntax error in SQL statement. Syntax error line 1 at or after token .[10179].
Msg 7321, Level 16, State 2, Line 4
An error occurred while preparing the query "SELECT id from company where name = #comp" for execution against OLE DB provider "MSDASQL" for linked server "LINKSERVER12".
The error is happening at the dynamic query
N'SELECT #csid = id FROM OPENQUERY(LINKSERVER12,
''SELECT id from company where name = #comp'')'
I tried replacing #comp in the SQL query with ''#comp'', ''''#comp'''' with no luck. Any help is greatly appreciated.
Just build the string query wihtout parameters.
USE [companyDB]
GO
DECLARE
#companyName AS nvarchar(50)
,#id nvarchar(MAX)
,#query NVARCHAR(MAX)
SET #companyName = 'AMAZON'
DECLARE #idTable TABLE
(
id INT
)
--Repace Server, UID and PWD
SET #query =
N'SELECT
[id]
FROM OPENROWSET
(
N''SQLNCLI''
,N''Server=10.111.1.111;UID=username;PWD=password123;''
,N''SELECT [id]
FROM [companyDB]
WHERE [name] = '''''+#companyName+'''''''
)'
INSERT INTO #idTable
EXECUTE (#query)
SELECT TOP 1
#id = id
FROM #idTable
I am writing scripts to generate stored procedures within a database whose current schema notation will be unknown (think shared hosting).
I have decided to use dynamic SQL within the stored procedures so that the web application can pass the database schema based on a user defined setting to the SQL Server in order for it to fire properly.
When I started writing the stored procedures, I noticed that dynamic SQL opens up a whole SQL injection problem I would not normally have so I re-wrote the procedure to combat this. However even though SQL allows me to run the script to generate the stored procedure, each time I try to run the test stored procedure, I get a syntax error
Incorrect syntax near the keyword 'WHERE'
I believe this is to do with the parameter for the schema but I am at a loss as to why this is not working? I am entering the value dbo for the schema.
/*
Name : usp_GetTestTicker
Description : returns test ticker
*/
if not exists (select * from dbo.sysobjects
where id = object_id(N'usp_GetTestTicker')
and OBJECTPROPERTY(id, N'IsProcedure') = 1)
BEGIN
DECLARE #sql as nvarchar(150)
SET #sql = 'CREATE procedure usp_GetTestTicker AS'
EXEC(#sql)
END
GO
ALTER PROCEDURE usp_GetTestTicker
#schema VARCHAR(25),
#TickerItemId INT
AS
SET NOCOUNT ON
BEGIN
DECLARE #sql_cmd NVARCHAR(MAX)
DECLARE #sql_params NVARCHAR(MAX)
SET #sql_cmd = N'SELECT * FROM #schema.TickerItem WHERE TickerItemId = #TickerItemId'
SET #sql_params = N'#schema VARCHAR(25), #TickerItemId INT'
EXEC sp_executesql #sql_cmd, #sql_params, #schema, #TickerItemId
END
GO
To prevent SQL injection, you will need to validate the schema against the sys.schemas table, e.g.
ALTER PROCEDURE usp_GetTestTicker
#schema NVARCHAR(25),
#TickerItemId INT
AS
BEGIN
SET NOCOUNT ON
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = #schema)
BEGIN
-- throw an error here. Your web code will have to handle the error and report an invalid schema
END
ELSE
BEGIN
DECLARE #sql_cmd NVARCHAR(MAX), #sql_params NVARCHAR(MAX)
SET #sql_cmd = N'SELECT * FROM ' + #schema + '.TickerItem WHERE TickerItemId = #TickerItemId'
SET #sql_params = N'#TickerItemId INT'
EXEC sp_executesql #sql_cmd, #sql_params, #TickerItemId
END
END
I am getting below error message when I am trying to run stored procedure.
Msg 7399, Level 16, State 1, Procedure accountupdater, Line 10 The OLE
DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)"
reported an error. Access denied. Msg 7350, Level 16, State 2,
Procedure accountupdater, Line 10 Cannot get the column information
from OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server
"(null)".
Additional information: previously it was working, but after installing MS Office it start to give an error message. We uninstalled MS Office and reinstalled “Microsoft Access Database Engine 2010”. Still getting error message.
Did some research and found that I need to install “Microsoft Access Database Engine 2010”. I did, but still getting the same error message.
ALTER PROCEDURE [dbo].[accountupdater]
AS
DECLARE #accountNum numeric, #businessFEIN varchar(100), #stateID varchar(100), #dbaName varchar(100), #addressLine1 varchar(100), #addressLine2 varchar(100), #city varchar(100), #state varchar(100), #zip varchar(100), #businessName varchar(100)
DECLARE accountCursor CURSOR FAST_FORWARD FOR
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Text; HDR=YES; Database=D:\Innoprise\',
'SELECT * FROM flagstaffAccountUpdate.csv') ors
OPEN accountCursor
--perform first fetch
FETCH NEXT FROM accountCursor INTO #accountNum, #businessFEIN, #stateID, #dbaName, #addressLine1, #addressLine2, #city, #state, #zip, #businessName
--check if there are more rows to fetch
WHILE ##FETCH_STATUS = 0
BEGIN
update BUSINESS SET FEIN=coalesce(#businessFEIN, FEIN), name=coalesce(#businessName, name) WHERE ID = (select business_id from VENDOR v where v.vendornumber=#accountNum);
update DBA set name=coalesce(#dbaName, name) where id = (select primarydba_id from vendor v where v.vendorNumber=#accountNum);
update VENDOR set stateId=coalesce(#stateID, stateID) where vendorNumber=#accountNum;
update ADDRESS set addressLine1=coalesce(#addressLine1,addressLine1), ADDressline2=coalesce(#addressLine2,addressline2),
city=coalesce(#city,city), state=coalesce(#state,state), zipCode=coalesce(#zip,zipCode)
where ID = (select v.address_ID from VENDOR v where v.vendorNumber = #accountNum);
FETCH NEXT FROM accountCursor INTO #accountNum, #businessFEIN, #stateID, #dbaName, #addressLine1, #addressLine2, #city, #state, #zip, #businessName
END
CLOSE accountCursor
DEALLOCATE accountCursor
If you have installed Microsoft Access Database Engine 2010 already,then please execute following queries and restart SQL Server Management Studio or SQL Services.
Refer here
USE [master]
GO
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
I would like to get the result of a function executed on an UNKNOWN remote database.
Server name and DB name are provided only at runtime.
I have tried this, but it didn't work:
DECLARE #result table (result BIT)
INSERT INTO #result
EXEC (N'SELECT [linkedserver].[remotedb].dbo.myudf(''myparameter'')')
SELECT * FROM #result
Turn on RPC on your linked server -
EXEC [master].dbo.sp_serveroption #server=N'linked_server', #optname=N'rpc out', #optvalue=N'true'
And try this one -
EXEC ('SELECT *
FROM AdventureWorks2012.sys.fn_helpcollations()') AT [linked_server]
Or this (more preferable) -
EXEC [linked_server].AdventureWorks2012.sys.sp_executesql N'SELECT DB_NAME()'