Incorrect syntax near the keyword 'EXEC'- OPENRPWSET - sql

I am trying to load data into SQL server table. here is my code;
from OPENROWSET('SQLOLEDB', 'Server=(local);Trusted_Connection=yes;',
'EXEC [dbo].[sp_model1] #model = ''predict1''',
'EXEC [dbo].[sp_model2] #model1 = ''predict2''',
#q =''SELECT * FROM [tcs].[dbo].[fact_FY2019_FY2021]
where
[date] >= ''''2021-02-03 00:00:00''''
and id = 11 order by key;'''
)as a
GO
Every time i run my insert statement along with above code, i get the following error
OLE DB provider "SQLNCLI11" for linked server "(null)" returned message "Deferred prepare could not be completed.".
Incorrect syntax near the keyword 'EXEC'.
Not sure why am i getting this error.
Any help would be appreciated.

Related

Problem with Stored Procedures in PHPmyAdmin

Attempting to perform operations with a random integer in SQL.
The following code works perfectly as intended when run as pure SQL, but triggers a syntax error when attempting to save it into a stored procedure.
SET #sample_count = (SELECT count(*)
FROM cinder_sample);
SELECT #sample_count;
SET #random_ID = (SELECT FLOOR(RAND()*#sample_count));
SELECT #random_ID;
Any ideas as to what could be going wrong?
The exact error triggered is:
"The following query has failed: "CREATE DEFINER=root#localhost PROCEDURE play_random_sp() NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER DELIMITER // SET #sample_count = (SELECT count() FROM cinder_sample)// SELECT #sample_count// SET #random_ID = (SELECT FLOOR(RAND()#sample_count))// SELECT #random_ID"
MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '// SET #sample_count = (SELECT count(*) FROM cinder_sample)// SELECT' at line 1"

Getting a syntax error when declaring a table in SQL

New to SQL and trying to run this code and I get the error "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '#Qtable TABLE(Qid INT) END' at line 3". The code runs fine if I take out the DECLARE statement;
SET #query_id = (SELECT ID from ua820988_dev.users WHERE `email` = 'example#gmail.com');
CREATE PROCEDURE query_requests()
BEGIN
DECLARE #Qtable TABLE(Qid INT)
END;
SELECT * from ua820988_dev.requests WHERE `match` = #query_id;
SELECT * from ua820988_dev.requests_archive WHERE `match` = #query_id;
I'm hoping to eventually put the results from the 2nd and 3rd SELECT statements into the table, but this is the watered down version for now just trying to get the code to run. I'm running SQL 5.6 on MariaDB 10.2.

Error while exporting SQL Server data via OPENROWSET feature

I am using below query to export SQL Server table data to Excel, but I am getting a syntax error as shown below.
Incorrect syntax near ' '.
Code:
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;
Database=\\local\testlocation\users$\Documents\test.xlsx','SELECT * FROM [Sheet1$]')
SELECT * FROM Test_Date
issue occurs in both SQL Server 2016/2008 R2, 64bit

OPENQUERY throws error when used with WIN2K8\SQL2K12

I'm trying the following Sql query to move my stored procedure result into table
SELECT *
INTO #tmpTable
FROM OPENQUERY(WIN2K8\SQL2K12, 'EXEC vcs_gauge #gauge_name=vs1_bag,#first_rec_time=2014-09-01 09:00:00,#last_rec_time=2014-09-01 10:00:00')
following error is thrown, when I execute the query.
Incorrect syntax near '\'.
I don't want to add linked server .How to resolve this issue?
EDIT1
When I do [win2k8\sql2k12], and first execute the following command
EXEC sp_serveroption 'YourServer', 'DATA ACCESS', TRUE
A new message comes
OLE DB provider "SQLNCLI11" for linked server "WIN2K8\SQL2K12" returned message "Deferred prepare could not be completed.".
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '-'.
You need to enclose DATETIME values in single quotes. And since your query is in a string itself, those single-quotes need to be doubled / escaped as follows (and you should probably also put the first parameter's value in escaped-single-quotes as it is clearly a string).
You should also fully qualify the stored procedure name with [DatabaseName].[SchemaName]..
And since the vcs_gauge proc uses Dynamic SQL, you need to specify the WITH RESULT SETS clause. For more info on this clause, please see the MSDN page for EXECUTE.
SELECT *
INTO #tmpTable
FROM OPENQUERY([WIN2K8\SQL2K12],
N'EXEC [DatabaseName].[SchemaName].vcs_gauge
#gauge_name = ''vs1_bag'',
#first_rec_time = ''2014-09-01 09:00:00'',
#last_rec_time = ''2014-09-01 10:00:00''
WITH RESULT SETS ( { column_specification} );
');

Update table using Openquery linked server

I've tried this code and still got the following error, perhaps anyone could help?
UPDATE a
SET a.MMDWNO = '21'
FROM OPENQUERY(NMIIFLIB,
'select * from MVXCDTANSN.MITMAS WHERE MMITTY = ''25''') a
Error :
OLE DB provider "MSDASQL" for linked server "NMIIFLIB" returned
message "[IBM][iSeries Access ODBC Driver][DB2 UDB]SQL7008 - MITMAS in
MVXCDTANSN not valid for operation.".
Msg 7343, Level 16, State 4, Line 1
The OLE DB provider "MSDASQL" for linked server "NMIIFLIB"
could not UPDATE table "[MSDASQL]".
The select statement works fine but when I try to update I always stuck with this.
If you're trying to update a table on linked server, try this syntax:
UPDATE OPENQUERY(NMIIFLIB, 'select * from MVXCDTANSN.MITMAS where MMITTY = ''25''')
SET MMDWNO = 21
you must try this. Hope this will help you.
UPDATE OPENQUERY(firstlink, 'select * from job.dbo.student where id = ''3''')
SET name = 'sanjeev acharya'