Error while exporting SQL Server data via OPENROWSET feature - sql

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

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"

SQL Server 2012 table data export to excel file

I would like to export data from a SQL Server stored procedure to an Excel file. How can I achieve that?
I test like that:
insert into OPENROWSET(
'Microsoft.ACE.OLEDB.12.0',
'Excel 8.0;Database=D:\test.xlsx;;HDR=YES',
'SELECT EmpID FROM [Sheet1$]')
select * from tb1
but it returns an error:
Column name or number of supplied values does not match table definition.
I think it's because Excel doesn't have columns right? But there's no way to pre-write columns before exporting to Excel, and SQL Server can't create Excel by itself ...
I also tried with bcp:
bcp "SELECT * FROM mydb.tb1" queryout 'D:\test.xlsx' -t, -c -S . -d DESKTOP-D3PCUR7 -T
It returns error:
Incorrect syntax near 'queryout'.
How can I export table to Excel easily in SQL Server?

Microsoft SQL Server Error: "Incorrect syntax near keyword Select"

Unsure what syntax the error is referring to at this statement :-
Use MyDatabase
CREATE TABLE TestTable
AS (SELECT * FROM dbo.MyTable);
Any help is appreciated!
The dbo suggests that you are using SQL Server. The syntax error is that this syntax is not supported.
The equivalent syntax in SQL Server is:
SELECT *
INTO TestTable
FROM dbo.MyTable;
You need to use like below. The one you are using is Oracle syntax.
Use MyDatabase
Go
SELECT * INTO TestTable FROM dbo.MyTable
GO

SAS Pass Through OpenQuery into iHistorian Error

I am doing a pass through query using SAS into iHistorian Interactive SQL. The code is listed below. I know the pass through code works; I ran the quoted code through iHistorian Interactive SQL itself and it returned the required data. I get the following error when I attempt the provided code.
ERROR: CLI describe error: [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot initialize the data source object of OLE DB
provider "IhOLEDB.iHistorian" for linked server "fsp1".
Has anyone tried this with iHistorian? Does anyone know what's going on here? Thanks in advance.
proc sql;
connect to odbc as conn(dsn="FSPPDW" uid=FontaneF);
create table work.Stuff as
select * from connection to conn (
select * from OPENQUERY(fsp1,
'SELECT
tagname,
timestamp,
value as Signal
FROM ihrawdata
WHERE timestamp > 10/23/2015 AND timestamp < 10/25/2015
AND (tagname = Fsdfeee:000)
AND intervalmilliseconds = 60000
AND samplingmode=interpolated'
)
);
disconnect from conn;
;
quit;

Handling Dates from Oracle to SQL Server 2005 using OPENQUERY

I have linked SQL Server 2005 to an Oracle Server, and am using OpenQuery() to pass data from Oracle to SQL Server.
So, for instance, the following query works fine:
SELECT *
FROM OPENQUERY(ORACLE_LINK, 'SELECT FIELD1
FROM ORACLE.TABLE1');
Everything works, unless I ask for a field which is a date/time in the Oracle database. For instance, say that TABLE1 has ten fields, one of which is a timestamp. If I ask for all nine fields except for the timestamp, it works. If I ask:
SELECT *
FROM OPENQUERY(ORACLE_LINK, 'SELECT *
FROM ORACLE.TABLE1');
I get the following error:
OLE DB provider "MSDAORA" for linked server "ORACLE_LINK" returned message "Oracle error occurred, but error message could not be retrieved from Oracle.".
OLE DB provider "MSDAORA" for linked server "ORACLE_LINK" returned message "Data type is not supported.".
I also get the same error if I ask for only the timestamp field.
What is the best way of handling this error? Thanks.
I do it with this function (partial code):
select #StringOut = '{ts ''''' + convert(varchar(20), #DateIn, 20) + ''''' }';
RETURN #StringOut
and this goes into the query:
and procedure_date_dt = ' + dbo.TimestampString(#date) + '
and event_type = ''''Time Notation''''
and it works when I run it.