SQL Server : error validating the default for a XML column - sql

I am trying to set the default value for a new column I just added. In properties I am trying to set 'Default Value or Binding' to a simple XML along the lines of:
<root>
<title>Welcome</title>
<body>Thank you for your time.</body>
</root>
However, when I click away, I get an error:
SQL error validating the default for column
I set this value in other numerical columns and it worked fine. What gives?

Not sure, but the visual designers in SQL Server Management Studio often don't quite work properly...
Just use a simple T-SQL statement to achieve the same thing:
ALTER TABLE dbo.YourTableName
ADD CONSTRAINT DF_YourTable_YourXmlColumn
DEFAULT '<root><title>Welcome</title><body>Thank you for your time.</body></root>'
FOR YourXmlColumn
and you're done!
(You didn't specify your table and column names - so I just made up placeholders - do replace those with your actual table and column names! Also: for the default constraint, I'd always recommend specifying an explicit name - makes it easier to disable and/or drop the constraint later, if you ever need to; my default naming convention is shown - again: adapt to your needs / likes)

It is true that knowing what TSQL command to run is advantageous in many ways but just to add an answer for SQL Server Management Studio so it is not swept under "it doesn't quite work properly", the syntax is ('[xml here]').
('<root><title>Welcome</title><body>Thank you for your time.</body></root>')

Related

How to fix this error: Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'NormalizedEmail' and etc

My application runs off my database that I created. I added migration, update-database and all that jazz so that it works perfectly fine.
Now that I have to convert my project to use the LIVE database, I'm getting this error message:
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'NormalizedEmail'.
Invalid column name 'ConcurrencyStamp'.
Invalid column name 'LockoutEnd'.
Invalid column name 'NormalizedEmail'.
Invalid column name 'NormalizedUserName'.
Invalid column name 'UserType'.
I'm not sure how to go about fixing this but when I use my old database it works, with this new one it just keeps giving me this error when I try to log in a user or anything to do with my database.
Help please!
ThANK YOU!
Remove this columns from code
I think it's mismatch with database
look at this
The problem is very simple to state and very difficult to solve. But there IS a solution.
Manual solution
If all automatic approaches fail and you do not have any extra information, then you can at least ensure that your schema is technically compatible with the application's expectations.
Depending on the RDBMS that you use and which was not specified in the question, you can get all the table names and column names. In MySQL and PostgreSQL you could query information_schema.columns, in SQL Server you can join sys.tables and sys.columns for that purpose. Make sure that you order the results by tablename, columnname and export it. Do it both for your old db and prod db. Find out what the differences are and implement alter statements to add the missing columns.
Automatic solution
If you have some scripts versioned somewhere that were doing the alters and possibly filling the new columns with data, then run those either by hand or a migration tool. Make sure that if such files exist, then you find them.
Removal
You can also remove the column references from code, as Cemil suggested in his/her answer, but you should avoid doing this, unless you are absolutely sure that it is feasible for your situation. The basic assumption is that the code references these for a reason and you are missing the columns from the database where they need to be created. Do not remove the column references from code until this basic assumption is proven wrong.

SQL CHECK statement error: A comma or a closing bracket was expected. (near "CHECK" )

I have made relational database model using MagicDraw tool. Every cashier has their workTime type saved in enumerator:
Then I generate DDL code and try to paste it into phpMyAdmin to generate DB but I get many errors. I tried to fix them by hand but errors are still here. So how should that check statement be written?
Errors:
A check constraint isn't part of a column's definition, it's its own clause in the table definition. You need a comma between the worktime char(13) not null and the check.
I think I have found a solution.
A book that I was following to create DDL from MagicDraw project suggested to pick "Check Constraints" as an "Enumeration transformation strategy" when transforming model. It didnt worked that way so I have chosen "Lookup tables" instead and DDL code seems to be working now.
However it still had some erorrs. I had to add semicolons after all INSERT statements.
And I also had to delete word "precision" after every 'double' type:
It works now.

Informix: Update Statement Error In WebSphere

I am trying to run this update statement but informix doesn't allow me to.
I have a table, named ITEMS and below I have selected some records from it.
SELECT SHORT_SKU, ITEMS."STYLE" FROM ITEMS;
SHORT_SKU STYLE
--------- -----
01846173 null
01811752 null
01811748 null
Trying to run the below UPDATE statement, informix says syntax error.
UPDATE ITEMS SET ITEMS."STYLE" = 'M' WHERE SHORT_SKU = '01846173';
^ syntax error here
Then I changed (as below) and got "Column (style) not found in any table in the query (or SLV is undefined)."
UPDATE ITEMS SET STYLE = 'M' WHERE SHORT_SKU = '01846173';
How do I update the "STYLE" field?
UPDATE 1
I did a change to one of WAS data source's custom properties, ifxDELIMIDENT. Originally it was blank. So, I changed it to true. Restarted WAS. And I couldn't login to our application. SQLExceptions were thrown by WAS but was not able to see the stack trace because WAS has truncated the last few lines. After changing the property back to blank, I was able to login to our application.
I tried another approach, which was to write a Java class that updates the ITEMMST.STYLE column. I executed this from a shell script. In the shell script, I defined and exported the variable DELIMIDENT with the value 'Y'. But I am still getting 'Syntax error'.
UPDATE 2
I managed to update the column. This is done by adding the 'DELIMIDENT=Y' property at the end of the connection string which will be passed to the DriverManager object when opening the database connection.
But, this won't work for our web application because it uses the WebSphere data source to create the db connection. It would be super if there's a way to set this property in the Informix environment itself.
Try:
UPDATE ITEMS SET "STYLE" = 'M' WHERE SHORT_SKU = '01846173';
It must be that STYLE is a reserved word so you must double-quote it to refer to the column. But standard UPDATE syntax doesn't allow you to prefix column names with the table name in the SET clause (since you can only be updating the columns of one table: the table mentioned in the UPDATE).
The right Syntax would be
UPDATE ITEMS SET STYLE = 'M' WHERE SHORT_SKU = '01846173';
As stated on IBM Documentation but as STYLE is a reserved word i guess your getting problems, Read IBM Recommendation on this.
Anyway you may find a work arround oat this link, otherwise you may consider changing the column name.
I am not aware of STYLE being a keyword in Informix (but I haven't gone to look for it). However, you can usually use keywords as column names etc without much problem.
If you must quote it, you need to set the DELIMIDENT environment variable - the value doesn't matter, but use DELIMIDENT=1 for concreteness. This enables SQL standard 'delimited identifiers', where double quotes surround identifiers (column names, table names, etc) and single quotes surround strings. (Normally, you can use either single quotes or double quotes around strings.)
One other point: if you use delimited identifiers, they also become case-sensitive (whereas normally, identifiers are case-insensitive). So you need to know how the STYLE column is stored in the system catalog. In most databases, they will be in lower-case. There's an outside chance that in a MODE ANSI database, they are stored in upper-case (but it is a while since I looked to make sure).
Use this query:
UPDATE ITEMS SET ITEMS.STYLE = 'M' WHERE SHORT_SKU = '01846173';
I think double quotes not required for column name.
Updated Answer 1:
Error Description-
-217 Column column-name not found in any table in the query
(or SLV is undefined).
The name appears in the select list or WHERE clause of this query but is
not defined in a table and does not appear as a statement local variable
(SLV) definition. Check that the column name or SLV name and the names of
the selected tables are spelled as you intended.
If all names are spelled correctly, you are not using the right tables,
the database has been changed, or you have not defined the SLV. If the
name not found is a reference to a column, that column might have been
renamed or dropped. If the name not found represents an SLV and you
defined the SLV in the statement, make sure that the SLV definition
appears before all other references to that SLV name.
This error message can also appear during the execution of an ALTER TABLE
statement when the engine tries to update views that depend on the table.
More info link
Updated Answer 2:
If not possible to change column name then get more information about SLV.
You can refer following links for description and use of SLV:
link1
link2
link3
There are 2 solutions for this.
Set the Informix JDBC data source 'ifxDELIMIDENT' property to 'true'
Rename the affected table columns
For the 1st option, we had a problem after setting the data source to 'true'. Suddenly all our queries did not work. After much troubleshooting, we found out that by setting the 'ifxDELIMIDENT' property to 'true', it also changed Informix to be case sensitive. In our Java code, we have all the column names in uppercase, and in Informix (Example: resultSet.getString("STYLE")), but the table column names are lowercase (Example: 'style'). This was why after changing this property, we were not able to login to our application Unfortunately this behavior was not documented anywhere in IBM's Info Centre nor in the internet.
We opted for the 2nd option which involved changing the affected column names to another column name (Example: Changed 'STYLE' to 'ITEM_STYLE').

SQL Server reports 'Invalid column name', but the column is present and the query works through management studio

I've hit a bit of an impasse. I have a query that is generated by some C# code. The query works fine in Microsoft SQL Server Management Studio when run against the same database.
However when my code tries to run the same query I get the same error about an invalid column and an exception is thrown. All queries that reference this column are failing.
The column in question was recently added to the database. It is a date column called Incident_Begin_Time_ts .
An example that fails is:
select * from PerfDiag
where Incident_Begin_Time_ts > '2010-01-01 00:00:00';
Other queries like Select MAX(Incident_Being_Time_ts); also fail when run in code because it thinks the column is missing.
Any ideas?
Just press Ctrl + Shift + R and see...
In SQL Server Management Studio, Ctrl+Shift+R refreshes the local cache.
I suspect that you have two tables with the same name. One is owned by the schema 'dbo' (dbo.PerfDiag), and the other is owned by the default schema of the account used to connect to SQL Server (something like userid.PerfDiag).
When you have an unqualified reference to a schema object (such as a table) — one not qualified by schema name — the object reference must be resolved. Name resolution occurs by searching in the following sequence for an object of the appropriate type (table) with the specified name. The name resolves to the first match:
Under the default schema of the user.
Under the schema 'dbo'.
The unqualified reference is bound to the first match in the above sequence.
As a general recommended practice, one should always qualify references to schema objects, for performance reasons:
An unqualified reference may invalidate a cached execution plan for the stored procedure or query, since the schema to which the reference was bound may change depending on the credentials executing the stored procedure or query. This results in recompilation of the query/stored procedure, a performance hit. Recompilations cause compile locks to be taken out, blocking others from accessing the needed resource(s).
Name resolution slows down query execution as two probes must be made to resolve to the likely version of the object (that owned by 'dbo'). This is the usual case. The only time a single probe will resolve the name is if the current user owns an object of the specified name and type.
[Edited to further note]
The other possibilities are (in no particular order):
You aren't connected to the database you think you are.
You aren't connected to the SQL Server instance you think you are.
Double check your connect strings and ensure that they explicitly specify the SQL Server instance name and the database name.
In my case I restart Microsoft SQL Sever Management Studio and this works well for me.
If you are running this inside a transaction and a SQL statement before this drops/alters the table you can also get this message.
I eventually shut-down and restarted Microsoft SQL Server Management Studio; and that fixed it for me. But at other times, just starting a new query window was enough.
If you are using variables with the same name as your column, it could be that you forgot the '#' variable marker. In an INSERT statement it will be detected as a column.
Just had the exact same problem. I renamed some aliased columns in a temporary table which is further used by another part of the same code. For some reason, this was not captured by SQL Server Management Studio and it complained about invalid column names.
What I simply did is create a new query, copy paste the SQL code from the old query to this new query and run it again. This seemed to refresh the environment correctly.
In my case I was trying to get the value from wrong ResultSet when querying multiple SQL statements.
In my case it seems the problem was a weird caching problem. The solutions above didn't work.
If your code was working fine and you added a column to one of your tables and it gives the 'invalid column name' error, and the solutions above doesn't work, try this: First run only the section of code for creating that modified table and then run the whole code.
Including this answer because this was the top result for "invalid column name sql" on google and I didn't see this answer here. In my case, I was getting Invalid Column Name, Id1 because I had used the wrong id in my .HasForeignKey statement in my Entity Framework C# code. Once I changed it to match the .HasOne() object's id, the error was gone.
I've gotten this error when running a scalar function using a table value, but the Select statement in my scalar function RETURN clause was missing the "FROM table" portion. :facepalms:
Also happens when you forget to change the ConnectionString and ask a table that has no idea about the changes you're making locally.
I had this problem with a View, but the exact same SQL code worked perfectly as a query. In fact SSMS actually threw up a couple of other problems with the View, that it did not have with the query. I tried refreshing, closing the connection to the server and going back in, and renaming columns - nothing worked. Instead I created the query as a stored procedure, and connected Excel to that rather than the View, and this solved the problem.

Getting around cyclical foreign key errors when trying to generate insert data scripts in SQL 2008

I am trying to generate some insert scripts using the SQL Server 2008 Script Wizard. Upon generating the scripts, I get the following error:
"The selected database contains foreign keys that create a cycle. Publishing data only is not supported for databases with cyclical foreign key relationships."
I've attempted to disable and remove all constraints in the database. The error is still occurring. Is there any way to get around this? Possibly make SQL ignore the constraints while generating the scripts.
On the Wizard page where you choose the radio button to select All Database Objects or Specific Objects, make sure to select All Database Objects. For some reason the tool needs something in there to generate even if you just want the table insert script.
Once I changed that radio button to All Database Objects, and selected the Advanced option to generate Type of script = Data Only, it worked all the way through.
I had the same problem as the OP. Then I tried again, this time in the advanced options, for the "types of data to script" option, I selected "schema and data" rather than data only. Then it worked for me without complaining about cyclical keys.
I was having the same issue, and I discovered today that you can use SQL Server Management Studio 2012 against a 2008 R2 DB and you won't get the error:
Sql Server Scripting Data Only: Workaround for CyclicalForeignKeyException?
Saving to file vs. to a new Query editor window seems to make it work for me on Management Studio 2008 :\
First off IMHO HLGEM's response is a bit cavalier--there are valid reasons at times to have cyclic references.
That said I think the script generator is hyper-sensitive. It seems to think just about any PK/FK pair is "cyclic" and I ended up having to use a copy of my database from which I'd stripped all keys to get the export to get beyond the "cyclical" error. A script like the following can help you drop keys globally but of course be careful!
SELECT
'ALTER TABLE ' + object_name(parent_obj) + ' DROP CONSTRAINT ' + [name]
AS Script
from sysobjects where xtype IN ('F')
[I didn't write this. See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=46682]
Further the tool is pretty useless in terms of feedback since its report doesn't provide enough detail to narrow down where the supposed cyclical references exist.
Finally I found the tool to be pretty flaky in that I get random timeouts. One other observation that I haven't researched extensively is I think the tool may require you to start from scratch after the cyclical error to clear it's cache since I see different behavior when I use Previous button vs. starting afresh.
You can export the data by setting the script option - "Script Check Constraints" to False
Sorry this will not work :(
You will have to determine which table is causing the issue.
I was getting the same error because I didn't had a table selected in the object list (one big table I wanted to create in another script). Selecting all of the tables solved the problem.
PD: Maybe a little bit late, but searching CyclicalForeignKeyException gets first in Google.