SQL Server 2016 Stored Procedure Syntax Error - sql-server

I recently restored a SQL Server 2008 database into an instance of SQL Server 2016. Most of the functionality seems to work fine, but many of my stored procedures that include updates to an application database table called SYS_USER fails with the following error:
Msg 102, Level 15, State 1, Procedure SYS_USERupdate, Line 35 [Batch Start Line 0]
Incorrect Syntax near '#errorNumber'
The database does not have a stored procedure called SYS_Userupdate and none of the procedures' code includes the term #errorNumber. I attempted to run the SQL query from one of the failing procedures directly in SQL Server Management Studio and received the same error message. Here is the SQL query that is failing:
UPDATE SYS_USER
SET SYS_USER_LGF_DT = GETDATE()
WHERE SYS_USER_ID = #SYS_USER_ID
I plugged in a valid value for the #SYS_USER_ID variable. Similar queries in related to other tables run without an issue. All of the stored procedures work on a SQL Server 2008 instance with no errors. Also, the database compatibility_level to 100, which should be acceptable for SQL Server 2016.

This happens when you're not running on a correct version of SQL Server, or if the compatibility level of the database isn't set sufficiently. So change it to 130
To check compatibility level:
select compatibility_level
from sys.databases
where name = '<database name>'
To alter compatibility level:
alter database <database-name>
set compatibility level = 130 -- SQL Server 2016
Compatibility levels list for all SQL versions: ALTER DATABASE (Transact-SQL) Compatibility Level

Reposting the answer since it was proposed in a comment to the question. The solution to the problem was that there were triggers on the affected tables that I did not know were there. Syntax errors on these triggers were causing insert and update queries on the tables to fail.
Credit to Jeroen Mostert https://stackoverflow.com/users/4137916/jeroen-mostert for helping with this!

Related

SQL Server Emergency mode - Syntax incorrect

I dont know whats the problem with that sql query :
ALTER DATABASE myDB SET EMERGENCY;
i got that error :
Msg 102, Niveau 15, Etat 6, Ligne 1
Incorrect syntaxt near 'EMERGENCY';
I use Microsoft SQL Server 2000 - 8.00.760 and SSMD 2016.
I dont know whats the problem
The issue is that you are using SQL Server 2000. Paul Randal mentions here
I decided to add a new feature to SQL Server 2005 called EMERGENCY-mode repair that will do steps 2 and 3 as an atomic operation.
So the syntax you are trying to do does not exist in 2000.
The "hacking system tables" he mentions is described here.
But you should be restoring from backup as first resort here.

How to workaround "SELECT failed because the following SET options have incorrect settings: 'ANSI_PADDING'. " error

I'm experiencing an error in sql server 2014 (SP2), when I try to run a 'create table' sql statement after SET ANSI_PADDING OFF. The statement fails with error message
"Msg 1934, Level 16, State 1, Procedure log, Line 7
SELECT failed because the following SET options have incorrect settings: 'ANSI_PADDING'."
This used to work fine (although I haven't explicitly tried it in a while on my sql server instance).
-- code that generates error:
use test
set ansi_padding off
create table test_ap3 (i int)
-- code that runs fine:
use test
set ansi_padding on
create table test_ap3 (i int)
Note: The create table with 'set ansi_padding off' works fine on other sql servers (i've tried it very recently on sql server 2014 SP3 standard edition, and sql server 2016 too).
I tried creating a new database on my local server, and the issue does not occur on the new database. The options settings for both dbs are the same. Old db had compatibility level set to 2012, but even after I changed it to 2014 the issue still occurrs.
The issue turned out to be caused by code in a database level trigger "for create table". The trigger has code with xml operations, and xml operations will fail if ANSI_PADDING is off!

SQL Server 2017 - Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'IF'

I exhausted 2 hours on google searching for answers, without any solutions. I need serious help with this.
DROP TABLE IF EXISTS dbo.MySqlTable
that's the only line of code on my page, when I execute it, I get
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IF'.
Here's a screenshot:
Drop Table
I just don't get why I can't drop the table.
What's weird is that it does drop on my PC, on this school laptop, nothing.I installed SQL Server 2017 on both. VS 2013 on my PC for later, hopefully, and VS 2015 on my Laptop. But, I haven't integrated those yet.
This feature is available only on SQL Server 2016 which is compatibility level of 130. Check your db on what compatibility level is.
select name, compatibility_level from sys.databases
If it is less than 130 you can alter it to 130 (2016) or 140 (2017) as below
alter database testdb set compatibility_level = 130
I seriously hate these things sometimes.
I removed IF EXISTS from my code, and simply did the usual DROP TABLE and it worked -_-
The first time I tried to drop a table on SQL Server 2017, it failed. After an hour on the internet, I learned that I had I had to use the IF EXISTS clause, and guess what, It worked.
When I got this laptop and installed the same SQL Server 2017, I obviously tried what took me an hour to find, DROP TABLE IF EXISTS, though that failed.
Thanks everyone.
Though, why did I use different DROP statements in the same version of SQL Server 2017?
I have a feeling I'm going to encounter more problems in the future.

syntax error on SQL Server

I'm receiving this error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '90'.
when i tried to compile this t-sql:
ALTER DATABASE SGCT SET compatibility_level = 90
Does anyone know why?
For SQL SERVER – 2005 try to use:
EXEC sp_dbcmptlevel AdventureWorks, 80;
GO
More details
Sets certain database behaviors to be compatible with the specified version of SQL Server. You are already using 2005 and 90 is to set for 2005. If you need to do a backward COMPATIBILITY
You should try this.
ALTER DATABASE SGCT
SET COMPATIBILITY_LEVEL = 80;
GO

ALTER DATABASE returning Major Error 0x80040E14, Minor Error 25501

I am attempting to recreate a SQL Server Compact database from a script. I started off creating it like this:
CREATE DATABASE [MyDatabase]
GO
and that seemed to work. The next commands in the script are these:
ALTER DATABASE [MyDatabase] SET ANSI_NULL_DEFAULT OFF
GO
and about two dozen similar commands. I have tried a representative sample of these and they all return the error:
Major Error 0x80040E14, Minor Error 25501
ALTER DATABASE [MyDatabase] SET ANSI_NULLS OFF
There was an error parsing the query. [ Token line number = 1,Token line offset = 7,Token in error = DATABASE ]
Does anyone know what the matter is?
I'm using Microsoft SQL Server Management Studio 2008 R2 on Windows XP.
If you check here - http://msdn.microsoft.com/en-US/library/ms174454(v=sql.90).aspx, I don't think you can do alter database in SQL Server CE.

Resources