Incorrect syntax near 'PIVOT' - sql

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]

Related

How to view the collation setting in SQL Server 2000?

I'm working with an ancient SQL Server 8.00.194, and I need to know the collation setting of the database and specific column.
I've naively tried to use documentation, but it seems that there were no such thing as sys.databases (same for sys.columns, sys.tables):
SELECT * FROM sys.databases
Msg 208, Level 16, State 1, Line 5
Invalid object name 'sys.databases'.
Also I have tried to view properties from UI with Management Studio, but it breaks all the time:
Cannot show requested dialog.
Failed to retrieve data for this request. (SqlManagerUI)
UPD. I kinda know that this is possible, because somebody has changed the collation on one column on this server, and that's why I'm doing this.
Yes, the metadata views have changed (or rather, first appeared) in SQL Server 2005. Before that, you could query the following:
select * from master..sysdatabases;
select * from sysobjects;
select * from syscolumns;
An easier way would be looking at the INFORMATION_SCHEMA views - these are ANSI standard and existed even in 2000, in almost the same form as they are now. In particular, INFORMATION_SCHEMA.COLUMNS shows column collation, that's for certain.

How to duplicate table with more than 500 rows in PhpStorm

I try to duplicate a table using DB tools of PhpStorm from SQL SERVER, this table has 1300 rows, when I run the code to duplicate table, the new table got just 500 rows.
This is the code that I use to duplicate:
SELECT * INTO TableBK FROM OldTable
If I use this code directly in SQL Server Management Studio, duplicate is successful but PhpStorm.
What could I do to get all records in new table using PhpStorm?
Also I try
SELECT TOP 2000 * INTO TableBK FROM OldTable
But got the same problem.
Sounds like PhpStorm defines TOP limit (e.g. by using SET ROWCOUNT) which is then used for other queries.
Try changing the value at Settings | Database | Result set page size from default 500 to whatever you think will suit you best (setting it to 0 should remove this limit).
Alternatively try adding SET ROWCOUNT 0; before the SQL command that you are going to execute in SQL Console.
UPDATE 2022-11-17
Current 2022.2 IDE version has it changed. It is now located at Settings/Preferences | Tools | Database | Data Editor and Viewer screen and would be either Limit page size to or Result set prefetch size option.
https://www.jetbrains.com/help/phpstorm/2022.2/settings-tools-database-data-views.html

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.

IBDAC / UniDAC + interbase 6 or 7 + a table field named "returning"

I am porting REALLY old code to use the UniDAC components. I have hit a wall with a specific UPDATE sql that changes a field named "returning." Simply wrapping the field in quotes does not resolve the issue, because the SQL dialect in the database is 1, which does not support double quote field delimiters. Is there any way around this without changing the field? I am on delphi 7, and am moving away from the interbase db components.
Edit: SQL is as follows:
update logger set
returning = :RETURNING
where locator = :LOCATOR
returns the following error when trying to prepare:
---------------------------
Ww
---------------------------
Dynamic SQL Error
SQL error code = -104
Token unknown - line 3, char -1
where.
---------------------------
OK
---------------------------
This occurs even when I set the client SQL dialect to 1 in code:
query1.Connection.SpecificOptions.Values['SQLDialect'] := '1';
I had to talk to the developer. It has been addressed to a certain point, but I need to remove all parameters in the query before executing it. Very odd, but its a workaround :/
Is it possible for you to create a view leaving everything the same except for this field and use that view in your application?
Is it possible to run this application with the client dll of a Firebird < 2.1 (1.5 for example) and a server with the same version?
I think this is due to the new syntax where returning is a reserved word!

help on Change Tracking in SQL Server 2005

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.