Why receiving a SYNTAX error while ALTERing a SQL table - sql

I have an existing SQL table and I am trying to add two more columns like this:
ALTER TABLE DSPCONTENT01.dbo.RADRESULTSTOTALS2
ADD ([TEST1] INT,
[TEST2] INT);
I get the following error:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.
How do I resolve it?

Don't use parentheses.
ALTER TABLE DSPCONTENT01.dbo.RADRESULTSTOTALS2
ADD
[TEST1] INT,
[TEST2] INT

Related

How to fix this message error that i cant create table using script?

I just want to make a new table in database, when I create table just got some messages like
Msg 102, Level 15, State 1, Line 5 Incorrect syntax near 'tbl_invoice'".
So my question is where do I start to make a new table?
use master
use db_penjualan
create table tbl_invoice (
no_invoice varchar primary key (10),
jenis_pembayaran varchar(25),
user_generate varchar (10),
tgl_terbit date not null )
;
Messages
Msg 102, Level 15, State 1, Line 5 Incorrect syntax near
'tbl_invoice'.
The problem lies with primary key declaration syntax. Write it as:
use db_penjualan
Go
create table tbl_invoice (
no_invoice varchar(10),
jenis_pembayaran varchar(25),
user_generate varchar (10),
tgl_terbit date not null ,
primary key(no_invoice)--define primary key col here
)
;

Move an existing Clustered columnstore index into a different file group

I am trying to move an existing Clustered Columnstore Index from one file group to another file group but couldn't find any command to do that.
Code what I have tried:
ALTER TABLE CCSI ON [dbo].[t179_s1_LOSS_ByEvent_ORIGINAL_440F6776-6185-4416-89D8-B69334457B25]
WITH ( MOVE TO FG_1 );
Error:
Msg 156, Level 15, State 1, Line 281
Incorrect syntax near the keyword 'ON'.
Msg 319, Level 15, State 1, Line 281
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Just like a clustered index, recreate it on the target filegroup with DROP_EXISTING. eg
create table foo(id int, a int)
create clustered columnstore index cci_foo on foo
go
create clustered columnstore index cci_foo
on foo
with (drop_existing=on)
on fg2

Error String or binary data would be truncated

I've been trying to create tables and add data into but I've run into this error
Msg 8152, Level 16, State 14, Line 35
String or binary data would be truncated.
Code:
CREATE TABLE Speakers_photos
(
SpeakerID CHAR(10) NOT NULL,
Image VARBINARY(MAX) NOT NULL,
PRIMARY KEY(SpeakerID),
FOREIGN KEY (SpeakerID) REFERENCES Speakers(SpeakerID)
ON UPDATE CASCADE ON DELETE NO ACTION
)
INSERT INTO Speakers_photos VALUES('S001210001', 0)
Where did it go wrong?
The truncation error will not occur with the code provided. Either the script is incomplete or the error is in an INSERT trigger on the Speakers_photos table. In the case of a trigger, look at line 35 as indicated in the error message.

sql server modify error

i m not able to modify table in SQL server. i m new to databases.
use work
go
alter table employee
modify id varchar(20)
error message is-
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'modify'
here is an screenshot
thanks
You have the syntax for altering a table wrong. You need:
ALTER TABLE YourTable
ALTER COLUMN ExistingColumn VARCHAR(20)
The syntax should be
ALTER TABLE Employee ALTER COLUMN ID VarChar (20)
Here is the ALTER COLUMN syntax.
http://msdn.microsoft.com/en-us/library/ms190273.aspx
Now, having said all that, I have a question for you.. Why is your ID column a VarChar as opposed to an Identity Column?

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.