To Make Db column Case Sensitive - sql

I have a database column of string type which is a primary key and case insensitive, I want to make the column as case sensitive such that it can have 2 entries like abc and ABC. I am trying like this
ALTER TABLE A
ALTER COLUMN B VARCHAR(10)
COLLATE SQL_Latin1_General_CP1_CS_AS
and I am getting following error
Msg 5074, Level 16, State 1, Line 1
The object 'PK_A' is dependent on column B'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN B failed because one or more objects access this column.

Related

Bulk insert null into table from csv file in SQL Server

I want to bulk insert from a csv file to a table.
This is my code for creating the table
create table customers
(
customerNumber int not null,
customerName varchar(50) not null,
salesRepNumber int null,
primary key(customerNumber)
);
This is the example of the csv file:
1001,John,1121
1002,James,1122
1003,Jonas,1123
1004,Jane,1124,
1005,Tom,1125,
1006,Bob,1126
1007,Thomas,NULL
This is the code for inserting data into the table:
BULK INSERT customers
FROM 'D:\Customers.csv'
WITH (fieldterminator=',', rowterminator='\n')
And I get these errors:
Msg 4864, Level 16, State 1, Line 4
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 7, column 12 (salesRepNumber).
Msg 4864, Level 16, State 1, Line 4
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 22, column 12 (salesRepNumber).
I am not so sure about how to bulk insert 'NULL' into a table and I am only supposed to use int datatype for the salesRep.

Alter Field Type from nvarchar(255) to nvarchar(Max) in SQL Server 2005

I would like to alter column type in table stk020. So, I used this query..
ALTER TABLE [stk020]
ALTER COLUMN [t16] nvarchar(Max) not null
The original column type of [t16] is nvarchar(255). I get this error
Msg 5074, Level 16, State 1, Line 1
The object 'DF_STK020_t16__725CC34D' is dependent on column 't16'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN t16 failed because one or more objects access this column.
Any solution?
You must first drop what we presume is the Default constraint on the column before you alter its data type:
Alter Table stk020 Drop Constraint DF_STK020_t16__725CC34D
GO
Alter Table stk020 Alter Column t16 nvarchar(max) not null
GO
Alter Table stk020 Add Constraint DF_STK020_t16__725CC34D
Default ... For t16

Msg 5074, Level 16, State 1, Line 1 The index 'IX_Alias_Alias' is dependent on column 'Alias'

Want to change the collation
using the below script
ALTER TABLE Alias ALTER COLUMN Alias nvarchar(25) COLLATE SQL_Latin1_General_CP1256_CI_AS
Error occurs
Msg 5074, Level 16, State 1, Line 1
The index 'IX_Alias_Alias' is dependent on column 'Alias'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN Alias failed because one or more objects access this column.
Actually I want to change the collation of all the table in database
You can use the below link to alter the collation of all the objects in a database.
Alter Collation for all the objects in a DB
You should delete the index first and then modify the column.

Trying to insert data correctly

After receiving very good correction from fuzzy lollipop, I amended my code to create an insert statement for every row of data. This is the code that I entered:
INSERT DEPARTMENTS
(Department_Id,Department_Name,Manager_Id,Location_Id)
VALUES
('D0001','Think Tank',NULL,'L0001')
GO
INSERT DEPARTMENTS
(Department_Id,Department_Name,Manager_Id,Location_Id)
VALUES
('D0002','Creators',NULL,'L0002')
GO
INSERT DEPARTMENTS
(Department_Id,Department_Name,Manager_Id,Location_Id)
VALUES
('D0003','Marketers',NULL,'L0003')
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0001','Joe','Blow',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0002','John','Doe',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0003','Sue','Happy',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0004','Tina','Turner',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0005','Ike','Turner',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0006','Big','Bird',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0007','Speedy','Gonzales',NULL,NULL,2010/06/25,NULL,NULL)
GO
However, these were the error messages that I received:
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 2
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 2
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'.
The statement has been terminated.
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
I won’t make the same mistake by failing to respond immediately to solutions. I did not know the checking the green checkmark meant that the answer was satisfactory.
Thank you for any help
You have two different types of errors.
The first is that you are violating a foreign key constraint. There are three ways to solve this:
Find out what the correct key should be (for example by querying the LOCATIONS table) and change your foreign key to the correct value.
Insert the missing row in the LOCATIONS table before inserting into DEPARTMENTS.
Remove the constraint (this is probably a bad idea).
The second error is simpler - you have incorrectly formatted your date. It should be a string.
'2010-06-25'
The complete query:
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0001','Joe','Blow',NULL,NULL,'2010-06-25',NULL,NULL)
1) there are no records with the given LocationIDs in the Location table
2) you need to quote the date values
Is there a FOREIGN KEY constraint between Department and Location. If so, then inserting a deparment requires an existing Location. Location is linked to Department with Location_ID
Timestamps syntax should be as a string '12/12/2010' not without the '-signs

How do I drop a column with object dependencies in SQL Server 2008?

The error message I'm obtaining when trying to drop a column:
The object 'defEmptyString' is dependent on column 'fkKeywordRolleKontakt'.
Msg 5074, Level 16, State 1, Line 43
ALTER TABLE DROP COLUMN fkKeywordRolleKontakt failed because one or more objects access this column.
I have already tried to find the default constraints, as described here:
SQL Server 2005 drop column with constraints
Unfortunately without any success :( The line returned is:
fkKeywordRolleKontakt 2 814625945 0 defEmptyString
And I cannot remove either of fkKeywordRolleKontakt and defEmptyString.
What is the correct way to get rid of this dependency?
EDIT: Perhaps this is of importance too. The column fkKeywordRolleKontakt is of type udKeyword (nvarchar(50)) with default dbo.defEmptyString.
Edit 2: Solved
I could solve the problem now. I'm sorry, I did not copy the full error message, which was:
Msg 5074, Level 16, State 1, Line 1
The object 'defEmptyString' is dependent on column 'fkKeywordRolleKontakt'.
Msg 5074, Level 16, State 1, Line 1
The object 'FK_tlkpRolleKontakt_tlkpKeyword' is dependent on column 'fkKeywordRolleKontakt'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE DROP COLUMN fkKeywordRolleKontakt failed because one or more objects access this column.
I could generate a script to drop the column by right-clicking on the column entry (dbo.tlkpRolleKontakt > Columns > fkKeywordRolleKontakt) (in MSSQL Server Manager), selecting Modify and deleting the column. Then Table Designer > Generate Change Script generated the necessary commands:
ALTER TABLE dbo.tlkpRolleKontakt
DROP CONSTRAINT FK_tlkpRolleKontakt_tlkpKeyword
EXECUTE sp_unbindefault N'dbo.tlkpRolleKontakt.fkKeywordRolleKontakt'
ALTER TABLE dbo.tlkpRolleKontakt
DROP COLUMN fkKeywordRolleKontakt
That's it :)
Did you try first:
ALTER TABLE <tablename> DROP CONSTRAINT defEmptyString;
?
drop the constraint which is dependent on that column with
ALTER TABLE TableName DROP CONSTRAINT dependent_constraint
Then Drop Column:
ALTER TABLE TABLE_NAME DROP COLUMN COLUMN_NAME
dependent_constraint : this constraint is shown in the error when we try to delete dependent column.
Example: trying to drop some column IsDeleted2
Error
The object 'DF__Employees__IsDel__15502E78' is dependent on column
'IsDeleted2'.
ALTER TABLE DROP COLUMN IsDeleted2 failed because one or more objects
access this column.
Error clearly states that we need to delete DF__Employees__IsDel__15502E78 constraint
ALTER TABLE Employess
DROP CONSTRAINT DF__Employees__IsDel__15502E78;
Drop Column: ALTER TABLE Employess DROP COLUMN IsDelted2
I could solve the problem now. I'm sorry, I did not copy the full error message, which was:
Msg 5074, Level 16, State 1, Line 1
The object 'defEmptyString' is dependent on column 'fkKeywordRolleKontakt'.
Msg 5074, Level 16, State 1, Line 1 The object
'FK_tlkpRolleKontakt_tlkpKeyword' is dependent on column
'fkKeywordRolleKontakt'. Msg 4922, Level 16, State 9, Line 1 ALTER TABLE DROP COLUMN fkKeywordRolleKontakt failed because one or
more objects access this column.
I could generate a script to drop the column by right-clicking on the column entry (dbo.tlkpRolleKontakt > Columns > fkKeywordRolleKontakt) (in MSSQL Server Manager), selecting Modify and deleting the column. Then Table Designer > Generate Change Script generated the necessary commands:
ALTER TABLE dbo.tlkpRolleKontakt
DROP CONSTRAINT FK_tlkpRolleKontakt_tlkpKeyword
EXECUTE sp_unbindefault N'dbo.tlkpRolleKontakt.fkKeywordRolleKontakt'
ALTER TABLE dbo.tlkpRolleKontakt
DROP COLUMN fkKeywordRolleKontakt
use this script to cancel the checking of constraint :
ALTER TABLE #tablename NOCHECK CONSTRAINT #constraintname
I ran into a simpler solution.
DELETE the data of that column.
Once the column has no value inside it do -
ALTER TABLE <table_name> DROP COLUMN <column_name>
This way the column is easily dropped.
P.S - This is a headache if you have like extreme amounts of data in the column.