I can update the column's length if it in null when created
MYCOLUMN1 nvarchar(12) NULL
My query:
Alter table MYTABLE ALTER Column MYCOLUMN1 nvarchar(13) NULL
but how can I change the length if column is not null?
MYCOLUMN2 nvarchar(12) NOT NULL
Alter table MYTABLE ALTER Column MYCOLUMN2 nvarchar(13) NOT NULL
when I try to do that SQL Server says
Msg 4902, Level 16, State 1, Line 1
Cannot find the object "MYCOLUMN2" because it does not exist or you do not have permissions.
I have permission because i am admin and table's column does exist.
Guys thanks for your response...
Here is table structure
CREATE TABLE [dbo].[MYTABLE](
[MyKey] [int] IDENTITY(1,1) NOT NULL,
[mycolumn1] [nvarchar](12) NULL,
[mycolumn2] [nvarchar](12) NOT NULL,
CONSTRAINT [PK_MYTABLE] PRIMARY KEY CLUSTERED
(
[MyKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Column names are case sensitive. Please try
Alter table MYTABLE ALTER Column [mycolumn2] nvarchar(13) NOT NULL
I've tried and it works fine on sqlserver2012.
Related
I'm aware that this question gets asked a million times, but I've already checked, double, and triple that corresponding values already exist in my parent table. I'm trying to populate a bridge table between my Albums and Artists, both of which are already populated. After getting errors in the C# program that was auto-populating, I tried inserting a single value manually, and still got the INSERT error.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Album](
[Album ID] [int] IDENTITY(1,1) NOT NULL,
[Album Title] [nchar](50) NOT NULL,
[Release Year] [int] NULL,
CONSTRAINT [PK_Album] PRIMARY KEY CLUSTERED
(
[Album ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Album-Artists] Script Date: 3/6/2015 12:49:33 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Album-Artists](
[Album ID] [int] NOT NULL,
[Artist ID] [int] NOT NULL,
CONSTRAINT [PK_Album-Artists] PRIMARY KEY CLUSTERED
(
[Album ID] ASC,
[Artist ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Artists] Script Date: 3/6/2015 12:49:33 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Artists](
[Artist ID] [int] IDENTITY(1,1) NOT NULL,
[Artists Name] [nchar](20) NOT NULL,
CONSTRAINT [PK_Artists] PRIMARY KEY CLUSTERED
(
[Artist ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
GO
ALTER TABLE [dbo].[Album-Artists] WITH CHECK ADD CONSTRAINT [FK_Album-Artists_Album] FOREIGN KEY([Album ID])
REFERENCES [dbo].[Album] ([Album ID])
GO
ALTER TABLE [dbo].[Album-Artists] CHECK CONSTRAINT [FK_Album-Artists_Album]
GO
ALTER TABLE [dbo].[Album-Artists] WITH CHECK ADD CONSTRAINT [FK_Album-Artists_Artists] FOREIGN KEY([Album ID])
REFERENCES [dbo].[Artists] ([Artist ID])
GO
ALTER TABLE [dbo].[Album-Artists] CHECK CONSTRAINT [FK_Album-Artists_Artists]
GO
(I've tried with and without the table quantifiers (Artists.[Artist ID], etc))
Your foreign key is trying to match ArtistId to AlbumId.
This is incorrect. You need to recreate it.
ALTER TABLE dbo.[Album-Artists]
DROP CONSTRAINT [FK_Album-Artists_Artists];
ALTER TABLE [dbo].[Album-Artists]
WITH CHECK ADD CONSTRAINT [FK_Album-Artists_Artists]
FOREIGN KEY([Artist ID]) REFERENCES [dbo].[Artists] ([Artist ID]);
Also the correct syntax for the insert is
INSERT INTO [dbo].[Album-Artists]
([Album ID], [Artist ID])
VALUES (10, 3);
Though dot-separated prefixes are currently ignored in the column list for INSERT statements.
Finally I would avoid using spaces or - in object names so you don't have to continually use quoted identifiers or square brackets and can use the unquoted form.
INSERT INTO dbo.AlbumArtists
(AlbumId, ArtistId)
VALUES (10, 3);
When trying to change column from NVARCHAR(max) to VARCHAR(max) I'm getting error:
Cannot create a row of size 8063 which is greater than the allowable maximum row size of 8060.
This is correct because of different storage needed to store unicode characters. But is there any way to force SQL Server to make this conversion?
EDIT: Table definition and alter command:
CREATE TABLE [Descriptions](
[SerialNo] [varchar](20) NOT NULL,
[LanguageCode] [char](2) NOT NULL,
[Note] [nvarchar](max) NOT NULL,
[Address] [varchar](2000) NOT NULL,
[StoreId] [varchar](3) NULL,
CONSTRAINT [UK_Descriptions] UNIQUE NONCLUSTERED
(
[SerialNo] ASC,
[LanguageCode] ASC,
[StoreId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
-- Alter column:
ALTER TABLE [Descriptions] ALTER COLUMN [Note] varchar(max) NOT null;
Try to empty your table first then do the change because it seems that a row store the existing data which over its limit. Take your data out, do your change then re-insert the data.
DECLARE #H nvarchar(max) = N'ESŐNAP' -- rainy day in Hungarian
SELECT CONVERT(varchar(max),#H)
result:
ESONAP
Trying to add a table to my database in SQL Server Management Studio but it's throwing a wobbly. I'm sure it's real simple but my brain has gone to mush and I can't find the problem. Basically it's telling me the database already exists, yet it clearly doesn't.
Error(s):
Msg 3701, Level 11, State 5, Line 2
Cannot drop the table 'MySchema.mix_Case_Study-Module', because it does not exist or you do not have permission.
Msg 2714, Level 16, State 5, Line 4
There is already an object named 'mix_Case_Study-Module' in the database.
Msg 1750, Level 16, State 0, Line 4
Could not create constraint. See previous errors.
Msg 4902, Level 16, State 1, Line 2
Cannot find the object "MySchema.mix_Case_Study-Module" because it does not exist or you do not have permissions.
SQL:
USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP TABLE [MySchema].[mix_Case_Study-Module]
CREATE TABLE [MySchema].[mix_Case_Study-Module](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Active] [bit] NOT NULL,
[Case Study ID] [int] NOT NULL,
[Module ID] [int] NOT NULL,
[Position] [int] NOT NULL,
CONSTRAINT [mix_Case_Study-Module] PRIMARY KEY CLUSTERED (
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [MySchema].[mix_Case_Study-Module] ADD CONSTRAINT [DF_mix_Case_Study-Module_Active] DEFAULT ((1)) FOR [Active]
GO
Any help appreciated.
Your constraint name and table name are the same.
CREATE TABLE [MySchema].[mix_Case_Study-Module]
and
CONSTRAINT [mix_Case_Study-Module] PRIMARY KEY CLUSTERED
You are having issues because you're first trying to drop a table that does not exist.
You should be using something like this:
USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID([MySchema].[mix_Case_Study-Module], 'u') IS NOT NULL
DROP TABLE [MySchema].[mix_Case_Study-Module]
CREATE TABLE [MySchema].[mix_Case_Study-Module](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Active] [bit] NOT NULL,
[Case Study ID] [int] NOT NULL,
[Module ID] [int] NOT NULL,
[Position] [int] NOT NULL,
CONSTRAINT [mix_Case_Study-Module_PK] PRIMARY KEY CLUSTERED (
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [MySchema].[mix_Case_Study-Module] ADD CONSTRAINT [DF_mix_Case_Study-Module_Active] DEFAULT ((1)) FOR [Active]
GO
create table stud(
Student_Id int primary key,
Student_Name varchar(30),
Student_surname varchar(12),
Student_Initial varchar(10))
I had created a table stud. Now i want to add Identity to Student_Id column using alter query
alter table stud alter column student_Id int identity
I get error as
Incorrect syntax near the keyword 'identity'.
ALTER TABLE MyTable
ADD ID INT IDENTITY(1,1) NOT NULL
You cannot make an already existing column as an IDENTITY column. Either you drop and recreate the table with the column marked as IDENTITY', or drop the column and add a newIDENTITY` column.
If Stud contains data, you could always make a shadow table, e.g. Stud2, which contains the Identity column, then run
ALTER TABLE dbo.stud SWITCH TO dbo.stud2
Then you can reseed Stud2, drop Stud, and rename Stud2 to Stud.
That way you can keep the data while dropping/recreating the table with Identity.
Syntax:
IDENTITY [ (seed , increment) ]
alter your table like as this:
create table stud(
Student_Id int IDENTITY(1,1) primary key,
Student_Name varchar(30),
Student_surname varchar(12),
Student_Initial varchar(10));
you can use below query to set identity
CREATE TABLE [dbo].[stud](
[Student_Id] [int] IDENTITY(1,1) NOT NULL,
[Student_Name] [varchar](30) NULL,
[Student_surname] [varchar](12) NULL,
[Student_Initial] [varchar](10) NULL,
PRIMARY KEY CLUSTERED
(
[Student_Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
So I have a table in SQL Server w/ a primary key column, and 4 other columns. When I modify the table, and select the primary key column to be identity, it won't let me save the table.
How can I make it an identity column through T-SQL or something without going to the UI?
Thanks.
Here's the create
USE [db]
GO
/****** Object: Table [dbo].[tblMessages] Script Date: 04/05/2011 11:58:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblMessages](
[messageId] [int] NOT NULL,
[messageText] [varchar](500) NOT NULL,
[messageLatitude] [float] NOT NULL,
[messageLongitude] [float] NOT NULL,
[messageTimestamp] [datetime] NOT NULL,
PRIMARY KEY CLUSTERED
(
[messageId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
You cannot turn an existing column into an IDENTITY column after it's been created.
ALTER TABLE dbo.YourTable
ALTER COLUMN YourColumn INT IDENTITY
will cause an error:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword
'IDENTITY'.
You need to create a new column of type INT IDENTITY and then possibly drop the old one. Or if your table is still empty: drop it and re-create it with the correct settings for your ID column
ALTER TABLE MyTable
ADD NewIdentity INT IDENTITY;
ALTER TABLE MyTable
DROP COLUMN OldPK;
EDIT
If your table is empty, just drop it and add IDENTITY after INT on your PK column and be done with it.