How to Fix Error Msg 41337, Level 16, State 100 in Create Memory Optimize table in SQL Server - sql

I want to use a memory optimize table and I got below error - how can I fix it? Any idea will be helpful.
Msg 41337, Level 16, State 100, Line 1
Cannot create memory optimized tables. To create memory optimized tables, the database must have a MEMORY_OPTIMIZED_FILEGROUP that is online and has at least one container.
Code:
CREATE TABLE dbo.Account
(
AccountID INT IDENTITY(1, 1) PRIMARY KEY NONCLUSTERED
) WITH (MEMORY_OPTIMIZED=ON)

you must create a file Group first :
Create FileGroup :
imoltp is a sampledatabase
ALTER DATABASE imoltp ADD FILEGROUP imoltp_mod
CONTAINS MEMORY_OPTIMIZED_DATA;
Add DatabaseFile To Your File Group :
ALTER DATABASE imoltp ADD FILE (
name='imoltp_mod1', filename='c:\data\imoltp_mod1')
TO FILEGROUP imoltp_mod;
Create Memory Optimize Table:
CREATE TABLE dbo.Account(
AccountID INT IDENTITY(1,1) PRIMARY KEY NONCLUSTERED
) WITH (MEMORY_OPTIMIZED=ON)
take a look at this for get more Info :
https://learn.microsoft.com/en-us/sql/relational-databases/in-memory-oltp/creating-a-memory-optimized-table-and-a-natively-compiled-stored-procedure?view=sql-server-ver15
for more detail

Related

ERROR: relation "schema.TableName_Id_seq" does not exist - when creating table in a new database

I'm having an issue where I used pgAdmin4's GUI to create a SQL table, and I want to use to generated CREATE TABLE script to create this same table in another database.
When I run the CREATE TABLE script generated by pgAdmin4 in my new database, I get the following error:
ERROR: relation "schema.TableName_Id_seq" does not exist
So, it appears that the issue is with my auto-incrementing id column that I created as type SERIAL.
The CREATE TABLE script as provided by pgAdmin4:
-- Table: myschema.TableName
-- DROP TABLE myschema."TableName";
CREATE TABLE myschema."TableName"
(
"Id" integer NOT NULL DEFAULT nextval('myschema."TableName_Id_seq"'::regclass),
/* Other columns here */
CONSTRAINT "TableName_pkey" PRIMARY KEY ("Id")
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE myschema."TableName"
OWNER to JoshuaSchlichting;
Why can't the CREATE TABLE script be used in another database? The relation "schema.TableName_Id_seq" didn't exist in the original database prior to be creating that table. What's happening that is different?
The DDL script provided by pgAdmin4 is not complete. When the table was created, there was an implicit creation of a sequence because of the SERIAL type being select for the Id column.
You can find this newly create sequence with pgAdmin4. To do this, go to
-> your server
-> your database
-> your schema
-> Sequences
-> Right click TableName_Id_seq
-> choose "Create script"
This reveals the script used to create this sequence. In this instance, the following was revealed:
-- SEQUENCE: myschema.TableName
-- DROP SEQUENCE myschema."TableName";
CREATE SEQUENCE myschema."TableName"
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 2147483647
CACHE 1;
The use of the CREATE SEQUENCE script can be avoided by changing the line of code used to create the Id column in the CREATE TABLE script. Example below:
original line:
"Id" integer NOT NULL DEFAULT nextval('myschema."TableName_Id_seq"'::regclass),
changed to: "Id" SERIAL NOT NULL,

unresolved reference warning - ON statement

When added a constraint in SQL, I am receiving an unresolved reference warning on the 'ON FG_LOGGING' line. Does it need to be declared as I'm not sure on this. This is an existing code I'm looking at that someone else built and cannot see when I search the file, any other mention of FG_LOGGING.
CREATE table Holidays.J2H.PriceCheckWorkList
(
PriceCheckWorkListId INT IDENTITY(1,1),
HotelID int,
HotelRoomID int,
PerPersonCost money,
CONSTRAINT [PK_PriceCheckWorkListId] PRIMARY KEY CLUSTERED (PriceCheckWorkListId)
) ON FG_LOGGING
The error message you are likely getting is:
Invalid filegroup 'FG_LOGGING' specified.
What the error effectively means is that the script was originally intended for a server which had FILEGROUPS set up to store tables in specific drives and folders on the server. Seemingly, the database on which you are now trying to create doesn't have these File Groups - apparantly the full database creation script hasn't been run.
You can either change the file group to run on the default file group (or remove it entirely), e.g.
CREATE table Holidays.J2H.PriceCheckWorkList
(
PriceCheckWorkListId INT IDENTITY(1,1),
HotelID int,
HotelRoomID int,
PerPersonCost money,
CONSTRAINT [PK_PriceCheckWorkListId] PRIMARY KEY CLUSTERED (PriceCheckWorkListId)
) ON [PRIMARY];
Alternatively, you (or your DBA) can recreate the filegroup and associated files on your server (replace the folder with an appropriate path for your server):
ALTER DATABASE Holidays
ADD FILEGROUP FG_Logging;
ALTER DATABASE Holidays
ADD FILE
(
NAME = FG_Logging,
FILENAME = 'c:\temp\FG_Logging.ndf')
TO FILEGROUP FG_Logging;

How to create Unique key for a varchar column for entity framework database?

I had watch youtube video tutorial teaching how to create unique key
http://www.youtube.com/watch?v=oqrsfatxTYE&list=PL08903FB7ACA1C2FB&index=9
In the video, he has created a unique key for Email(nvarchar) column, I could create it when I create database manually, but when I try create unique key for a database created with entity framework code first, using the next query
ALTER TABLE Peoples
ADD CONSTRAINT UQ_MyTable_Email UNIQUE (email)
It will generate a error:
Msg 1919, Level 16, State 1, Line 2
Column 'email' in table 'Peoples' is of a type that is invalid for use as a key column in an index.
What is problem? what can I do for create unique key for nvarchar(max) column?
say If you create this table
CREATE TABLE ConstTable
(ID INT,
Email VARCHAR(1000)
CONSTRAINT uc_Email UNIQUE (Email)
)
GO
you will get a warning :
Warning! The maximum key length is 900 bytes. The index 'uc_Email' has
maximum length of 1000 bytes. For some combination of large values,
the insert/update operation will fail
Your column on which you want to define a unique constraint should be less then or equal to 900 bytes, so you can have a VARCHAR(900) or NVARCHAR(450) column if you want to be able to create a unique constraint on that column
Same table above with VARCHAR(450) gets created without any warning
CREATE TABLE ConstTable
(ID INT,
Email VARCHAR(900)
CONSTRAINT uc_Email UNIQUE (Email)
)
GO
Result
Command(s) completed successfully.
Test For your Table
say this is your table
CREATE TABLE ConstTable
(ID INT,
Email VARCHAR(MAX)
)
GO
Now try to create any index on the VARCHAR(MAX) data type and you will get the same error.
CREATE INDEX ix_SomeIdex
ON ConstTable (Email)
Error Message
Msg 1919, Level 16, State 1, Line 1 Column 'Email' in table
'ConstTable' is of a type that is invalid for use as a key column in
an index.

Change length of column of table which have dependencies

I've got a table named Contacts with column Title varchar(50) . Now in the middle of development I want to change the length to varchar(100) of field Title .At the moment table Contacts has over 25 dependencies (other tables, views functions).
When I run following sql statement in sql server 2008 . I am getting errors.
ALTER TABLE [Contacts ] ALTER COLUMN [Title ] varchar(100)
Error Like
Msg 5074, Level 16, State 1, Line 2
The object 'Contacts_title' is dependent on column 'title'.
And more.
you have to drop are recreate the constrains on the Contact table to do that or (sometime not really recommended ) you can temporary disable the constrain, alter the length and enable them again
--disable all constraints for the Sales.SalesOrderHeader table
ALTER TABLE [yourtable] NOCHECK CONSTRAINT ALL
--do your stuff
--do something --enable all constraints for the Sales.SalesOrderHeader table
ALTER TABLE [yourtable] CHECK CONSTRAINT ALL
You have to remove the dependence, and then create it again.

"There is already an object named XXXX in the database" after creating New database

I have an SQL file which I am executing on an SQL Server instance which contains the schema for a database. The file creates a brand new database (as in, a database with this name does not exist on this server):
CREATE DATABASE PROJECT;
and begins to create a relation:
CREATE TABLE Courses (
CourseID INT NOT NULL PRIMARY KEY,
Name VARCHAR(64) NOT NULL UNIQUE,
Code CHAR(4) NOT NULL UNIQUE
);
...
and here is what SQL Server tells me right off the bat:
Msg 2714, Level 16, State 6, Line 3
There is already an object named 'Courses' in the database.
Any idea why SQL Server tells me that there already exists a relation by the name of Courses when clearly there isn't?
Thank you for your time.
Check the database if you are using PROJECT
CREATE DATABASE PROJECT
GO
USE PROJECT
GO
CREATE TABLE Courses
(
CourseID INT NOT NULL PRIMARY KEY,
Name VARCHAR(64) NOT NULL UNIQUE,
Code CHAR(4) NOT NULL UNIQUE
)
GO
You are likely missing USE PROJECT statement and therefore trying to create Courses table in the master database, not in the PROJECT database.
Long shot but try using:
Use project;
CREATE TABLE Courses....