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.
Related
I am trying to perform an adhoc type SQL query, I want to send records from an instance of MSSQL in Windows to MSSQL in Ubuntu Server 20.04, the detail is that the reading it allows me and at the time of writing the said message appears, I explain:
When I want to add a record:
INSERT INTO [LINKEDSRV]. [BDD]. [DBO]. [TABLE] (column) SELECT 10
and it works for me,
But when I want to use it in an SP (I'm just adding the snippet where I call it):
SET #lQryStr = 'DELETE FROM [LINKEDSRV].' + #BDD + '.DBO.TABLE WHERE YEAR (p1) =' + CONVERT (VARCHAR, # lp1,1) + 'AND MONTH (p1) =' + CONVERT (VARCHAR, # lp1,1)
EXEC (#lQryStr)
OLE DB provider "SQLNCLI11" for linked server "LINKEDSRV" returned message "No transaction is active.". Msg 0, Level 11, State 0, Line 312 A severe error occurred on the current command. The results, if any, should be discarded.
When I use it in other instances of MSSQL on Windows I have no problem, but it specifically happens to me on linux. Follow the tutorial for the RPC and MSDTC settings, redirect port 135 (RPC) and create the iptables. The user has the necessary permissions, since if he allows me to enter when I call him outside of an SP. Could you help me, I do not know if it is not a new feature allowed in MSSQL in Linux, if this helps both are DEVELOPER versions. I will appreciate your contributions.
I have migrated an oracle database to Microsoft SQL Server via liquibase but there are still some SQL statements that don't work. This one looked like this in oracle:
CREATE OR REPLACE VIEW "BP_RESULTS_VIEW" (
BP_ID,
RES_NAME,
RES_LONG_NAME,
MEDIAN,
LOW_HINGE,
HIGH_HINGE,
H_SPREAD,
INNER_FENCE_LOW,
INNER_FENCE_HIGH,
OUTER_FENCE_LOW,
OUTER_FENCE_HIGH,
LOW_NOTCH,
HIGH_NOTCH,
LOW_ADJACENT_VALUE,
HIGH_ADJACENT_VALUE)
AS
SELECT
r.BP_ID,
rv.RES_NAME,
rv.RES_LONG_NAME,
r.MEDIAN,
r.LOW_HINGE,
r.HIGH_HINGE,
r.H_SPREAD,
r.INNER_FENCE_LOW,
r.INNER_FENCE_HIGH,
r.OUTER_FENCE_LOW,
r.OUTER_FENCE_HIGH,
r.LOW_NOTCH,
r.HIGH_NOTCH,
r.LOW_ADJACENT_VALUE,
r.HIGH_ADJACENT_VALUE
FROM
bp_results r,
results_view_display rv
WHERE
CAST (rv.value AS INT) = r.bp_id AND
rv.type = 'BOX';
After migrating it to Microsoft SQL Server it looks like this:
CREATE OR REPLACE FORCE VIEW BP_RESULTS_VIEW (BP_ID, RES_NAME, RES_LONG_NAME, MEDIAN,
LOW_HINGE, HIGH_HINGE, H_SPREAD, INNER_FENCE_LOW, INNER_FENCE_HIGH,
OUTER_FENCE_LOW, OUTER_FENCE_HIGH, LOW_NOTCH, HIGH_NOTCH,
LOW_ADJACENT_VALUE, HIGH_ADJACENT_VALUE) AS SELECT
r.BP_ID,
rv.RES_NAME,
rv.RES_LONG_NAME,
r.MEDIAN,
r.LOW_HINGE,
r.HIGH_HINGE,
r.H_SPREAD,
r.INNER_FENCE_LOW,
r.INNER_FENCE_HIGH,
r.OUTER_FENCE_LOW,
r.OUTER_FENCE_HIGH,
r.LOW_NOTCH,
r.HIGH_NOTCH,
r.LOW_ADJACENT_VALUE,
r.HIGH_ADJACENT_VALUE
FROM
bp_results r,
results_view_display rv
WHERE
CAST (rv.value AS INT) = r.bp_id AND
rv.type = 'BOX'
GO
But when I want to execute it always this error occurs:
Incorrect syntax near 'REPLACE'.
I don't understand why because the REPLACE statement exists in SQL Server too. It also seems like it doesn't recognize the CAST command. I am using Microsoft SQL Server Management Studio 17
In SQL Server 2016 SP1 and later (including Azure SQL Database), use CREATE OR ALTER VIEW for the equivalent functionality. In earlier SQL Server versions, one must first drop the view and then CREATE VIEW and GRANT permissions.
SQL Server doesn't support CREATE OR REPLACE VIEW.
Instead, create the view the first time. Then simply use ALTER VIEW. That is the simplest method. You can also drop the view and the re-create it.
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')
I'm running SQL Server 2008 R2. I'm trying to build a table that takes data from a table structured like this:
company | ded_id | descr
10 1 MEDINS
10 2 LIFE
10 3 PENSN
...
10 50 DOMREL
And I need to build a temp table it out to a format like this:
company | DESC1 | DESC2 | DESC3 ... | DESC50
10 MEDINS LIFE PENSN DOMREL
So I built the following query:
SELECT *
FROM (
SELECT company,'DESC'+CAST(ded_id as VARCHAR(2)) AS DedID,descr
FROM deduction
) deds
PIVOT (MAX(descr)FOR DedID IN([DESC1],[DESC2],[DESC3])) descs
So running this gives the following error:
Msg 325, Level 15, State 1, Line 6
Incorrect syntax near 'PIVOT'. You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the SET COMPATIBILITY_LEVEL option of ALTER DATABASE.
I double checked the compatibility level on the database and it is already set to 100 so that can't be the issue. Can you think of any other setting that might be causing this behavior?
The possible reason for that type of issue is you imported database from other source which might be running older version of SQL Server. Anyway, you have way to get out of it. Follow steps below:
Right click on Database (e.g. Northwind).
Click 'Properties'.
Click 'Options', from left pane, under 'Select a page' section.
Select appropriate database version as per your installation from drop-down of 'Compatibility level' at right.
Save changes and try now.
Below is screenshot of Properties window for your reference.
I had the same error (Incorrect syntax near 'PIVOT'...) and solved it by changing compatibility level in the Options section of database properties.
I also found how to script the compatibility level change:
ALTER DATABASE database_name
SET COMPATIBILITY_LEVEL = 100
(see https://msdn.microsoft.com/en-us/library/bb510680.aspx?f=255&MSPPError=-2147217396)
i faced the same problem... while using "Pivot" and "Format" and "Convert" command inside sql query.
one thing that worked for me was
instead of importing sqlite
i imported pyodbc
so my code changed
from :
import sqlite3
conn = sqlite3.connect('/Users/****/Desktop/****.sqlite')
to:
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};''Server=####;''Database=****;' 'Trusted_Connection=yes;')
I know it's not the same thing that was asked, but it solved it for me and it can help other people. pivot and a SQL reserved word, so it presents the error, what solved it was to put it in square brackets.
select * from ledgers where [pivot]
I have a database of 40 tables. I want to track the changes and keep record of every change made in database. what would be best option ?
Note: i am using MS SQL Server 2005.
i have tried following code for Change Tracking
ALTER DATABASE Customer
SET CHANGE_TRACKING = ON
(AUTO_CLEANUP = ON , CHANGE_RETENTION = 5 DAYS)
but gives error
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'ON'.
if anyone could help me on this issue, i would be greatful.
You might give C2 auditing a try: http://msdn.microsoft.com/en-us/library/ms187634%28SQL.90%29.aspx
I believe you are trying to use a 2008 feature.