SQL Server Invalid Column Name Error - sql-server

I have an invalid Column Name error when inserting a record to my SQL Server table. Here's the definition:
CREATE TABLE [dbo].[myTable]
(
[id] int IDENTITY(1, 1) NOT NULL,
[person_name] varchar(255) NOT NULL,
[modified_By] varchar(255) NOT NULL
)
ON [PRIMARY] WITH (DATA_COMPRESSION = NONE);
GO
And insert
INSERT INTO myDB.dbo.myTable (id, person_name, modified_By)
VALUES (1, 'Aishwarya', 'admin')
But I get the following error upon execution:
Lookup Error - SQL Server Database Error: Invalid column name
'modified_BY'.
I can SELECT from the column fine. The only thing I've noticed in error is that "BY" is capitalized, contrary to the table definition. Any ideas?
UPDATE: Updating all the syntax errors, apologeez

Is your database set to a case sensitive collation? You can check by running this query:
SELECT DATABASEPROPERTYEX('<Insert Database Name>', 'Collation') SQLCollation;
Case insensitive collations usually have CI in the name, such as SQL_Latin1_General_CP1_CI_AS.
Where as case sensitive collation might be something like Latin1_General_BIN.
If the database has a case sensitive collation, then the capitalization in the T-SQL must match the column definition.
Your post here doesn't include the modified_BY capitalization in the insert statement, so double check that. If it's capitalized there properly, then check if there are any triggers on the table that might have the improper spelling.

In your INSERT statement do not supply a value for your IDENTITY column "Id". It should look like:
INSERT INTO myDB.dbo.myTable
(person_name, modified_By)
VALUES ('Aishwarya', 'admin')

Not an answer anyone wants to hear, but our DBA dropped and recreated the table (which had triggers set on it) and this has resolved the issue.

Related

I want to add a check in database column, so that people who enter data into it should be in capital and trimmed

The situation is: I have a column in a database table where people enter data using SQL scripts directly (without any web page).
I want to put a restriction on that column to ensure if anyone enters data it is trimmed and capitalized, otherwise it should not accept it.
Or it should do it automatically.
Yours sincerely.
Try this one. Replace 'myTableName' with your table name. Replace 'myColumnName' with your column name. NOTE : UpperCaseCheck is the name of the constraint, as well as DataLenCheck.
--UpperCaseCheck
ALTER TABLE myTableName
ADD CONSTRAINT UpperCaseCheck CHECK(myColumnName = UPPER(myColumnName) COLLATE Latin1_General_CS_AS);
-- DataLenCheck constraint
ALTER TABLE myTableName
ADD CONSTRAINT DataLenCheck CHECK(DataLength(myColumnName) = DataLength(Rtrim(LTrim(myColumnName))))
-- Instead of two constraint, we can use only one
ALTER TABLE myTableName
ADD CONSTRAINT UpperCaseCheckAndDataLenCheck CHECK(myColumnName = UPPER(myColumnName) COLLATE Latin1_General_CS_AS
AND DataLength(myColumnName) = DataLength(LTrim(Rtrim(myColumnName))))
SQL Server 2017
select upper(trim(#param_name))
Older versions
select upper(rtrim(ltrim(#param_name)))

SQL Server 'Invalid column' on a SELECT query to a temp table - CASE SENSITIVITY issue

I have a case where I have NO control whatsoever on the SQL Server configuration.
Here's my configuration:
SQL Server 2008
SQL Server INSTANCE Collation: Latin_General_BIN (Which I know is case-sensitive)
A given database (Which is not case-sensitive... SQL_Latin1_General_CP1_CI_AS)
Here's the scenario I want... if possible:
Given
CREATE DATABASE given_database;
USE given_database;
CREATE TABLE #test (Field1 nvarchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS);
When
SELECT field1 FROM #test; /*Notice the lower case on field1*/
Then
Invalid column name 'field1'
Is there a way to make this work?
I know this is something that no one would want to have but since I am stuck with this configuration on my client side and we have a lot of legacy code that have different casing in the queries... I wanted to know if there's a work around.
Is there a way to make this work?
Not with a temp table, no.
Tempdb inherits the instance default collation. You've defined the column with a case insensitive collation which should let you ask for value comparisons in a case insensitive manner, but the column name itself falls into the database collation so it needs to be referred to as defined.
CREATE TABLE #test (Field1 nvarchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS);
INSERT INTO #test
VALUES ('test'),('TEST'),('Test')
SELECT field1 FROM #test --should fail
SELECT Field1 FROM #test WHERE Field1 = 'tEst' --should work

Error creating entity Microsoft SQL server IDENTITY_INSERT

I'm using JHipster 4. I've create a simple entity just with a "name" property and when I try to create a entity from UI I get the folowing error. (I'm using Microsoft SQL Server).
In think the important part of the error is :
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'tenant' when IDENTITY_INSERT is set to OFF.
Direct answer appears to be that you just need to configure jhipster to have identity_insert on, per this link: https://jhipster.github.io/tips/004_tip_using_ms_sql_server.html
Adding the identityInsertEnabled="true" is the same as wrapping your Inserts with IDENTITY_INSERT ON and IDENTITY_INSERT OFF which will allow you to insert the project autogenerated identities directly. This is why we are using the MS SQL Server Liquibase for.
The error tells you everything you need to know. You are trying to insert a value in to an identity column. This is not allowed, as the column is supposed to be automatically populated unless you explicitly turn it off temporarily.
Documentation on what identity_insert is here: https://msdn.microsoft.com/en-us/library/ms188059.aspx
You can turn it off using the command set identity_insert SchemaName.TableName off though you had better be very confident you are inserting the correct data.
I would recommend you do some investigation with whoever manages your database as to why that column is an identity column and whether or not you should be inserting into it at all.
Assume a table:
create table test(ID int identity(1,1)
,Name nvarchar(100)
)
ALL of these insert statements will throw an error:
insert into test(ID,Name)
select ID
,Name
from OtherTable
insert into test(ID,Name)
select null as ID
,'TestName' as Name
insert into test(ID,Name)
values(99
,'Name'
)
Whereas these will work, automatically generating a new, unique value for the ID column:
insert into test(Name)
select Name
from OtherTable
insert into test(Name)
select 'TestName' as Name
insert into test(Name)
values('Name')
In short, you need to insert nothing into the identity column. Not a blank string, not a null value, but absolutely nothing at all.

How do I insert data in specific columns in SQL Server 2012?

I am using the AdventureWorks2012 database...
I created a backup table using the SELECT INTO clause. However, I got an error message when I tried to insert data into some columns using the query below:
INSERT INTO Sales.salesorderdetails_backup
(SalesOrderID ,
OrderQty,
ProductId,
SpecialOfferId,
UnitPrice,
UnitPriceDiscount,
LineTotal)
OUTPUT inserted.*
VALUES (57123,
45,
712,
1,
15.89,
0,
45.89)
I got the error msg:
Cannot insert the value NULL into column 'rowguid', table
'AdventureWorks2014.Sales.salesorderdetails_backup'; column does not
allow nulls. INSERT fails...
When you define your table, you can specify a "not null" constraint for any column or columns. Which means that you MUST put a value in that column for any insert.
This is generally a Good Thing. For example, "not null is the default for a primary key column.
You have two choices:
1) Make sure the column in question has a value before you "Insert" (PREFERRED)
2) Do an "alter table" to drop the "not null" constraint:
ALTER TABLE MyTable ALTER COLUMN MyColumn columnType NULL
ALSO: As you probably know, NEWID() is the MSSQL function to generate a GUID for your "insert".
I finally succeeded after I added DEFAULT constraints:
ALTER TABLE Sales.salesorderdetails_backup
ADD CONSTRAINT df_date DEFAULT GETDATE() for ModifiedDate;
ALTER TABLE Sales.salesorderdetails_backup
ADD CONSTRAINT df_guid DEFAULT NEWID() FOR rowguid;

SQL server key violation due to case insesitivity

I m having a table in which a primary key is there with 2 columns(CODE nvarchar,VALUE nvarchar).This table contains the values in the Key columns as (X8900,A) but when I try to insert a new value as (X8900,a) ,its giving error message “primary key violation”.
Why its giving this error,if case is different for values column and is there any solution for this in order avoid the error ?
You can specify if SQL Server should be case sensitive or not using collation. In this instance the column must have a case sensitive collation in order for you to be able to specify any type of unique constraint on it. For example, the first example will fail whereas the second will work, notice the CI and CS for case insensitive and sensitive.
CREATE TABLE test1 (
col1 varchar(20) COLLATE Latin1_General_CI_AS PRIMARY KEY
)
INSERT INTO test1 VALUES ('ASD')
INSERT INTO test1 VALUES ('asd')
CREATE TABLE test2 (
col1 varchar(20) COLLATE Latin1_General_CS_AS PRIMARY KEY
)
INSERT INTO test2 VALUES ('ASD')
INSERT INTO test2 VALUES ('asd')
Collation can be set at the column or database level. If set at database level then all character columns without a collation specified adopt the database collation.
You have to check the collation of your database. If you have a case insensitive collation, 'A' == 'a'. If you need to maintain difference between cases, you can either change the collation to a case sensitive collation, or you could cast the strings to varbinary. A binary representation differentiates between cases.
Collations can be set at the server level (i.e what databases default to) and at the database level (overriding the server collation). At an even more granular level, you can set collation on individual columns if you want/need. Here are a few articles to look at:
https://msdn.microsoft.com/en-us/library/hh230914.aspx#TsqlProcedure
https://msdn.microsoft.com/en-us/library/ms144250%28v=sql.105%29.aspx
Here are a few SQL snippets you can run to view your current server collation, as well as the default collations on each database
SELECT CONVERT (varchar, SERVERPROPERTY('collation'));
SELECT name, collation_name FROM sys.databases;

Resources