OPENQUERY syntax issue SQL Server 2012 - sql

I have a function that returns a table that will need to be called remotely. Since this functionality is not supported by SQL at the moment, I will need to utilize OPENQUERY to do the following
OPENQUERY([Linked_Server],'Query')
However, I keep getting a syntax error when I put in the ip address for the linked server. Linked server is setup properly from the looks of it. I am getting the following error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'OPENQUERY'.
The script is:
OPENQUERY('NN.NNN.N.NN','SELECT * FROM dbo.DBarBillers')
(where N's are the digits of the defined linked server's IP address).
Intellisense is putting the red line under OPENQUERY and the linked server argument
I have tried unquoting the ip address, and bracing it instead of quoting and all are yielding errors. Thoughts?

You need to select from the openquery so the syntax is
SELECT * FROM OPENQUERY([Linked_Server],'SELECT * FROM dbo.DBarBillers')

Related

ADO - in case of error: How to get information about Query Line number?

In SQL Server Management Studio using this following SQL query:
SELECT
[testing_error]
FROM
[baza].dbo.[sprawy]
I get this error:
Msg 207, Level 16, State 1, Line 2
Invalid column name 'testing_error'
I know why this error occurs, and I do not need help in fixing them.
But....
As so far I already checked Errors Collection (ADO) and used this collection but only Invalid column name 'testing_error'. error is taken from errors object.
My question is about: how using ADO get the information about the "Line"?
Because I'm looking for a way to get the information which line of the SQL query the error is located on.

"Incorrect syntax near" with Ip address in query in SQL Server 2012

I have tried create new view using an IP address in the query syntax. But this resulted in an error.
Query in View Editor:
SELECT *
FROM IP-ADDRESS.DATABASE.dbo.TABLE
Error:
In Normal Query (Work it):
How may I correctly execute this query?
Thank!
Try using tsql instead of the UI. I tested in the UI and I see what you mean that it is not keeping brackets.
CREATE VIEW [YourViewName]
AS
SELECT
*
FROM [IP-ADDRESS].DATABASE.dbo.TABLE
WHERE Something

SQL and IBMDASQL

I'm trying to query a set of files.
Out of the 15 I have to query 3 of them are returning the error:
Msg 7314, Level 16, State 1, Line 1
The OLE DB provider "IBMDASQL" for linked server "Server name here" does not contain the table "system.lib.file". The table either does not exist or the current user does not have permissions on that table.
I am using SQL and made a linked server using the IBMDASQL driver.
Like I said, 3 files out of the 15 are only giving me this problem.
When I do a wrkobj on the files I see that public has all permission, and I've added the specific user account just in case.
When I connect using a basic ODBC and microsoft query I don't even see the file on the list. When I log directly into the iSeries I can see and manipulate the files. .
Now using the alternate file name. But getting an insert error. As asked, here is the code and then below the exact error.
INSERT INTO [PARKWAY-TRAIN].S60017.ORDERF912.NC_NURS_ORD_REF
(RECORD_ID, NURSING_ORDER_CD, ORDER_DESCRIPTION, LONG_DESCRIPTION, ORDER_TYPE, FREQUENCY_CD, FREQ_PM_MAINT, NOTE_REQUIRED_STS, NOTE_TEMPLATE, TPL_STATUS, FORM_ID, PARAMETER_ID, COSIG_REQ)
SELECT STND.RECORD_ID, STND.NURSING_ORDER_CD, STND.ORDER_DESCRIPTION, STND.LONG_DESCRIPTION, STND.ORDER_TYPE, STND.FREQUENCY_CD, STND.FREQ_PM_MAINT, STND.NOTE_REQUIRED_STS, STND.NOTE_TEMPLATE, STND.TPL_STATUS, STND.FORM_ID, STND.PARAMETER_ID, STND.COSIG_REQ
FROM [MODEL2].TNICLN2.ORDERF143.NC_NURS_ORD_REF STND
WHERE STND.RECORD_ID <> 'D' AND NOT EXISTS (SELECT HOSP.NURSING_ORDER_CD FROM [PARKWAY-TRAIN].S60017.ORDERF912.NC_NURS_ORD_REF HOSP WHERE HOSP.NURSING_ORDER_CD = STND.NURSING_ORDER_CD)
The error message:
OLE DB provider "IBMDASQL" for linked server "PARKWAY-TRAIN" returned message "SQL0104: Token . was not valid. Valid tokens: .
Cause . . . . . : A syntax error was detected at token .. Token . is not a valid token. A partial list of valid tokens is . This list assumes that the statement is correct up to the token. The error may be earlier in the statement, but the syntax of the statement appears to be valid up to this point. Recovery . . . : Do one or more of the following and try the request again: -- Verify the SQL statement in the area of the token .. Correct the statement. The error could be a missing comma or quotation mark, it could be a misspelled word, or it could be related to the order of clauses. -- If the error token is , correct the SQL statement because it does not end with a valid clause.".
Msg 7343, Level 16, State 2, Line 1
The OLE DB provider "IBMDASQL" for linked server "PARKWAY-TRAIN" could not INSERT INTO table "[PARKWAY-TRAIN].[S60017].[ORDERF912].[NC_NURS_ORD_REF]".
If I run just the Select portion of the code, I get a return 503 lines to be exact. It only fails when I throw in the insert portion of the code.
IBM says to correct the token. I've got no clue as to which token is in error to correct though.
The 'Table not found' error is very suspicious. Using STRSQL:
select table_name, table_schema,
base_table_name, base_table_schema, base_table_member,
system_table_name, system_table_schema
from systables
where table_name = 'NC_NURS_ORD_REF'
Check to see that the table named NC_NURS_ORD_REF in library ORDERF912 is really associated with the system table name NCORDER in the library ORDERF912.
The syntax error is equally suspicious. Given that this is a cross-system query, I doubt you'll be able to run it as-is from the IBM i STRSQL command. That would mean that the proper multi-system database configuration is set up on the i side.
I found someone with a similar problem and it looks like it's on the Microsoft side: http://bytes.com/topic/db2/answers/447753-error-inserting-into-iseries-db2-table-long-name-via-sql-server-2000-linked-server I think his workaround was to use the OPENQUERY() form.
One thing you can definitely check on the IBM i side is whether journaling is turned on for ORDERF912.NC_NURS_ORD_REF. This is important because you need to use the right commit level. Take the results of the previous query against SYSTABLES and use the SYSTEM_TABLE_NAME to do a DSPFD. Look for the text 'File is currently journaled'. If the table is not journaled, you have several choices. The best choice is to start journaling the file, but if that's not available, try INSERT... WITH NC. Or turn off commitment control at the driver.
Also, have a look at the following IBM technote to make sure the linked server is set up properly: http://www-01.ibm.com/support/docview.wss?uid=nas8N1014514

TRY_CONVERT fails on SQL Server 2012

The following SQL statement fails on SQL Server 2012(SP1) 11.0.3401.0 Version
DECLARE #b VARCHAR(10) = '12312.2'
SELECT TRY_CONVERT(DECIMAL(10,2),#b)
error message
Msg 195, Level 15, State 10, Line 2
'DECIMAL' is not a recognized built-in function name.
But works fine on SQL Server 2012(SP1) 11.0.3393.0
Both servers have no problem with TRY_PARSE()
DECLARE #b VARCHAR(10) = '12312.2'
SELECT TRY_PARSE(#b AS DECIMAL(10,2))
Update:
after further testing and trying conversion to different types got another different error message
DECLARE #b VARCHAR(10) = '12312'
SELECT TRY_CONVERT(INT,#b)
Error:
error: Msg 195, Level 15, State 10, Line 2
'TRY_CONVERT' is not a recognized built-in function name.
So original error message was miss leading, but now I'm even more confused why it is not there.
Check that the database compatibility level for the particular database you are using the function in is set to SQL Server 2012 (110) under properties/options/compatibility level for the database.
Make sure that you have specified on which database you want to run the query on.
use [Database_Name]
go
>>>>query here <<<
go
In my case I had a number of different databases on the server with different compatibility levels.
You can run the following script to Upgrade your database compatibility level to SQL Server 2012 (110):
if (SELECT compatibility_level FROM sys.databases WHERE name = '[Your_Database_Name]')<120
begin
ALTER DATABASE StreamNet
SET COMPATIBILITY_LEVEL= 110
end
Note: Replace [Your_Database_Name] with your current database name.
Reference: See Microsoft solution for more details
Try another IDE or environment to see if isolates the error? I was getting this error running a query in Azure Data Studio. When I ran the same query in SQL Server Management Studio -- where I'd first written it -- it ran successfully.

Getting incorrect sytax error with SQL Server

I have what I think should be a simple question but for the life of me cannot solve it myself. I'm just trying to do a simple search on a table.
Select *
From dbo.Case
Where dbo.Case.CountyName='Milwaukee'
I keep getting this error:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'Case'.
I've also tried taking off the "dbo" and it still does not work.
What am I doing wrong? (Using SQL Server 2008)
CASE is a reserved keyword. You will need to enclose it in [] to use as a an identifier such as a database, column, or table name:
Select *
From dbo.[Case]
Where dbo.[Case].CountyName='Milwaukee'
Case is a reserved word in SQL, so you need to put square brackets around it.