Why constraint cannot be added? - sql-server

I have a graph tables and want to add constraint. Do as in the MS doc here
https://learn.microsoft.com/en-us/sql/relational-databases/tables/graph-edge-constraints?view=sql-server-2017
I do it:
ALTER TABLE bought ADD CONSTRAINT EC_BOUGHT1 CONNECTION (Customer TO Product, Supplier TO Product);
But receive the error:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'CONNECTION'.
What is the reason? how to fix it?

I suspect edge constraints were included in SQL Server 2017 CTP 2.0 but the feature didn't make the RTM version. I don't have an old SQL Server CTP install to verify my theory but edge constraints are not available in SQL Server 2017 RTM, at least through the latest CU (CU31 as of this writing).
Per the comments, #siggemannen submitted doc feedback to have the documentation clarified. Edge constraints are available in SQL Server 2019 and later versions, including Azure SQL Database.

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.

SQL Server 2016 Stored Procedure Syntax Error

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!

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.

Getting script errors when generating SQL script for Azure using SQL Server Management Studio

I am using the free azure websites and made an sql database. I need to create the db tables, and then insert the dummy in.
I tried to generate the scripts through sql management studio but I get a lot of errors when I run it against the azure db.
Msg 40508, Level 16, State 1, Line 1
USE statement is not supported to switch between databases. Use a new connection to connect to a different Database.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'CONTAINMENT'.
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near 'COMPATIBILITY_LEVEL'.
How can I get the data into my azure database?
Just taking a guess here, based on the errors: When generating scripts via SSMS, you need to specify output specific to Windows Azure SQL Database (formerly known as SQL Azure). I don't have it installed at the moment so I can't provide you with a screenshot. I'll update my answer, should I get it installed before you verify this is indeed the issue.
as David mentioned above, you can specify the script generation to target Windows Azure SQL Database
Hi this may be a little late but I found the this link to be helpful:
You basically have to manualy change the context (click on the correct database on the list of dropdown) before executing the query. You may have noticed this when working between databases.
Hope this helps

linked server problem at sql server while connecting to oracle dbms

I have created a linked-server definition according to the article at :
http://www.ideaexcursion.com/2009/01/05/connecting-to-oracle-from-sql-server/
My aim is to transfer rows to tables at Oracle 11gR2.
After creating linked server, whenever I try to select a table using a query like :
SELECT *
FROM [192.168.1.188]..[ESIPARIS].[T_ERROR_LOG]
I get the error below :
Msg 7356, Level 16, State 1, Line 1
The OLE DB provider "OraOLEDB.Oracle" for linked server "192.168.1.188"
supplied inconsistent metadata for a column. The column "EVENT_OBJECT"
(compile-time ordinal 2) of object ""ESIPARIS"."T_ERROR_LOG"" was reported
to have a "LENGTH" of 50 at compile time and 100 at run time.
One more thing is that it duplicates field names whenever a select statment is prepared by "Sql Server Management Studio", some fields are duplicated as below :
SELECT [EVENT_DATE]
,[EVENT_DATE]
,[EVENT_DATE]
,[EVENT_DATE]
,[EVENT_OBJECT]
,[EVENT_OBJECT]
,[EVENT_OBJECT]
,[EVENT_OBJECT]
,[MESSAGE]
,[MESSAGE]
,[MESSAGE]
,[MESSAGE]
,[EVENT_ID]
FROM [192.168.1.188]..[ESIPARIS].[T_ERROR_LOG]
I would be very happy to hear from you about any ideas, thank you for your concern,
Best Regards,
Kayhan YÜKSEL
There are a number of scenarios which might throw this error:
your distributed query in SQL Server references a view with an underlying table in Oracle with a primary key column created in a certain way, Find out more
there's a bug when the querying a view with numeric columns. Find out more
it may be a problem with driver incompatibility, such as using the MS OleDB driver instead of the one Oracle provides.
If it isn't the driver one possible workaround is to use OPENQUERY. Otherwise. this support note contains general information on troubleshooting linked server and Oracle.
(This problem is a fairly generic one, so it turned out that the actual resolution was none of the things I suggested. I'm incorporating #kayhanyüksel's solution in the body of this response for the sake of completeness.)
Solved it with changes at listener and tnsnames. We are now able to connect from SQL Server to Oracle 11gR2 (running on 64 bit Red Hat Enterprise Linux 5.4 ) and vice versa. Documents followed are
- Making a Connection from Oracle to SQL Server
- The Oracle Gateways documentation
I had the same problem: The column ...... was reported
to have a "LENGTH" of 50 at compile time and 100 at run time. and duplicate column names when selected.
while i was trying to run a query in MS SQL from an ORACLE 11g database
I used the follownig type of query and it worked !
DECLARE #TSQL varchar(8000)
SELECT #TSQL = 'SELECT * FROM OPENQUERY(MyLinkedServer,''SELECT * FROM TableName'')'
EXEC (#TSQL)
where MyLinkedServer is the name of the linked server and
TableName is the name of the table.
here you have the link to the article that helped me: http://support.microsoft.com/kb/314520
Old thread but it may be useful to someone.
When I recently encountered this error, using as provider the MS OleDB driver instead of the Oracle OleDB provider solved the problem.
I have the same issue with 11g client but it was disappeared with client version 12 which works for me is using OPENQUERY and to_char with the field that makes problem.
I confirm that SQL management studio (no matter what version) gives many duplicated field. The only installing of last driver version we can have consistent queries. I hope it can be useful for you!

Resources