SQL create table stored procedure not setting defaults properly - sql-server

I am trying to create a table with a stored procedure. I scripted the table as a CREATE statement and copied the file contents into my procedure, which works great. Except that when it sets defaults, it sets the first columns default as the whole second line.
Here's the procedure:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC
('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldPath]
')
After execution, column [fldName] has a default value of FOR [fldName] ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldPath]
It makes sense that it's just not recognizing the second quotation mark in (N''), but why not? And how do I fix it?
UPDATE:
I have learned that the proper way to create tables is through reading and executing sql script files. This keeps data manipulation and database manipulation seperate, and easier to manage.

If you have an apostrophe within a string in SQL, you need to escape it with an additional apostrophe. Try:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC
('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldPath]
')
It's not clear (to me at least) why you're doing it this way though, instead of just:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'' FOR [fldPath]
Also not clear why you're creating a table within a procedure, especially without checking whether it already exists first - can you guarantee that this table won't already exist every time your procedure is run?

You have to double the quotation marks when inside a string:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC ('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldPath]
')`

Not saying it is the right thing to do to create a table via stored procedure but...you need to escape your quotes:
CREATE PROCEDURE [dbo].[spCreatetblLocation]
AS
EXEC
('
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tblLocation](
[pkLocationID] [int] IDENTITY(1,1) NOT NULL,
[fldName] [nvarchar](100) NOT NULL,
[fldPath] [nvarchar](1000) NOT NULL,
[fkYearID] [int] NOT NULL,
CONSTRAINT [PK_LocationID] PRIMARY KEY CLUSTERED
(
[pkLocationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldName]
ALTER TABLE [dbo].[tblLocation] ADD DEFAULT N'''' FOR [fldPath]
')

Related

SQL Table Schema for IdentityServer4 PersistedGrantStore

I'm writing a PersistedGrantStore for IdentityServer 4 and want to persist to a Table in SQL server.
PersistedGrant has a key of type string, not a great choice but I'll use binary collation to compensate. nvarchar(max) for a primary key is a no-go as long as I get to play the DBA role.
Could anyone give us an indication on how long this field and all other string fields should be?
Key
Type
SubjectId
SessionId
ClientId
Description
Data
It would also be great to know beforehand if we should add indexes for any of the fields ending with Id.
The table create SQL statement is:
USE [IdentityServer]
GO
/****** Object: Table [dbo].[PersistedGrants] Script Date: 2021-12-21 21:17:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PersistedGrants](
[Key] [nvarchar](200) NOT NULL,
[Type] [nvarchar](50) NOT NULL,
[SubjectId] [nvarchar](200) NULL,
[SessionId] [nvarchar](100) NULL,
[ClientId] [nvarchar](200) NOT NULL,
[Description] [nvarchar](200) NULL,
[CreationTime] [datetime2](7) NOT NULL,
[Expiration] [datetime2](7) NULL,
[ConsumedTime] [datetime2](7) NULL,
[Data] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_PersistedGrants] PRIMARY KEY CLUSTERED
(
[Key] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
You can find the SQL code for all the tables here

Deadlock occurs on multiple processes trying to insert a new row on the same table

CREATE TABLE [dbo].[WKF_EXEC_HTR](
[MODULE_ID] [int] NULL,
[SESSION_ID] [int] NULL,
[Request_id] [int] NULL,
[wkf_exec_htry_seq] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [pk_wkf_exec_htr] PRIMARY KEY CLUSTERED
(
[wkf_exec_htry_seq] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = OFF, ALLOW_PAGE_LOCKS = OFF)
ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF GO
ALTER TABLE [dbo].[WKF_EXEC_HTR] WITH CHECK ADD CONSTRAINT [FK_WKF_EXEC_HTR_SESSION_ID] FOREIGN KEY([SESSION_ID])
REFERENCES [dbo].[SESN] ([SESSION_ID])
GO
ALTER TABLE [dbo].[WKF_EXEC_HTR] CHECK CONSTRAINT [FK_WKF_EXEC_HTR_SESSION_ID]
I have a Primary Key and Clustered Index on [wkf_exec_htry_seq]. When multiple processes try to insert in this table it causes deadlock any idea how to avoid this deadlock.

Change NVARCHAR to VARCHAR column data type

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

SQL Server - Create Table script not working

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

Microsoft Entity Framework not showing foreign keys for some tables

I built the entity model from the database, using Entity Framework Version 5.0. The following DDL was used to create the tables:
CREATE TABLE [dbo].[Replenishment](
[replenishmentId] [int] IDENTITY(1,1) NOT NULL,
[locationID] [int] NOT NULL,
[inventoryItemId] [int] NOT NULL,
PRIMARY KEY CLUSTERED
([replenishmentId] 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
ALTER TABLE [dbo].[Replenishment]
WITH CHECK ADD CONSTRAINT [FKReplenishm163678]
FOREIGN KEY([inventoryItemId])
REFERENCES [dbo].[InventoryItem] ([inventoryItemId])
GO
ALTER TABLE [dbo].[Replenishment] CHECK CONSTRAINT [FKReplenishm163678]
GO
ALTER TABLE [dbo].[Replenishment]
WITH CHECK ADD CONSTRAINT [FKReplenishm580804]
FOREIGN KEY([locationID])
REFERENCES [dbo].[Location] ([locationID])
GO
ALTER TABLE [dbo].[Replenishment] CHECK CONSTRAINT [FKReplenishm580804]
GO
When I build an entity model with the tables Replenishment, Location, and InventoryItem, all three tables show up in the model, but none of the relationships do.
Does anyone know why the foreign keys or the navigation properties don't appear?
============================================================================
Here is the DDL from the associated tables:
Here is the ddl from the other two tables:
CREATE TABLE [dbo].[Location](
[locationID] [int] IDENTITY(1,1) NOT NULL,
[addressID] [int] NULL,
[availabilityStatusID] [int] NOT NULL,
[inLocationId] [int] NULL,
[locationTypeID] [int] NOT NULL,
[locationName] [varchar](40) NOT NULL,
[locationDescription] [varchar](100) NOT NULL,
[locationAnchorX] [int] NULL,
[locationAnchorY] [int] NULL,
[locationImage] [varchar](255) NULL,
[diagramLayerId] [int] NULL,
[locationShapeParameters] [varchar](max) NULL,
[locationHeight] [int] NULL,
[locationWidth] [int] NULL,
[locationColor] [varchar](20) NULL,
PRIMARY KEY CLUSTERED
(
[locationID] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Location] WITH CHECK ADD CONSTRAINT [FKLocation209934] FOREIGN KEY([inLocationId])
REFERENCES [dbo].[Location] ([locationID])
GO
ALTER TABLE [dbo].[Location] CHECK CONSTRAINT [FKLocation209934]
GO
CREATE TABLE [dbo].[InventoryItem](
[inventoryItemId] [int] IDENTITY(1,1) NOT NULL,
[itemTaxonomyId] [int] NOT NULL,
[itemDescription] [varchar](100) NOT NULL,
[itemCode] [varchar](40) NULL,
[batchControlled] [bit] NOT NULL,
[assemblyDefinitionID] [int] NULL,
PRIMARY KEY CLUSTERED
(
[inventoryItemId] 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
I believe that I found the problem. I had been doing some work with SQL Server authentication and some work with windows Authentication. The information entered using Windows Authentication was not seen by my Visual Studio, which was connecting using SQL Server authentication.
After further research I found that this was not the case. I am still bemused by this.

Resources