SQl Server 2012 error - sql-server

I created a table manually and after that selected script table as new query and changed table name and executed the query. I am getting the error as
Msg 170, Level 15, State 1, Line 12 Line 12: Incorrect syntax near
'('.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE dbo.[KitCodeProperties](
[KitPropertiesId] [int] IDENTITY(1,1) NOT NULL,
[KitCodeName] [varchar](50) NULL,
[KitCodeDescription] [varchar](200) NULL,
[ShippingInstructions] [varchar](200) NULL,
[DepartmentId] [int] NULL,
[KitCodeActive] [bit] NULL,
CONSTRAINT [PK_KitCodeProperties] PRIMARY KEY CLUSTERED
(
[KitPropertiesId] 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 took you code and pasted it into my 2012 developer edition. Had no issues creating the table with and without SET commands.
Therefore, the syntax looks good.
Are you selecting on a section of the window?
Make sure you are not selection anything in the new query window and press F5 to execute the whole window as one batch.
If this works, you were highlighting only a section of the code.
A simplified version of the SSMS code.
-- Remove old existing table
IF OBJECT_ID('[dbo].[KitCodeProperties]') > 0
DROP TABLE [dbo].[KitCodeProperties];
-- Create new table
CREATE TABLE [dbo].[KitCodeProperties]
(
[KitPropertiesId] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
[KitCodeName] [varchar](50) NULL,
[KitCodeDescription] [varchar](200) NULL,
[ShippingInstructions] [varchar](200) NULL,
[DepartmentId] [int] NULL,
[KitCodeActive] [bit] NULL,
);
The cut down version works fine in SQL Fiddler.

Related

Cannot delete regs on a SQL table in Access

I have a SQL Server table linked in an Access app. If I try to delete records with a delete query there is no problem. But if I try to delete records directly in table or using a select query in datasheet mode Access doesn't allow me to delete the records and throws the following warning:
"The microsoft access database engine stopped the process because you and another user are attempting to change the same data at the same time."
The same happens when I try to update data. There is no other user modifying the data.
The problem is that we still have a lot of legacy forms that uses datasheet mode to alter o delete records instead of using queryes and, for now, changing all these forms is unthinkable.
¿Has anyone any idea of what could be happening?
Thanks!
FINAL EDIT:
The problem was a bit field that was set to nullable that, thanks to Kostas K. I discovered is not convertable to Access.
So, instead of this:
[FIELD] [bit] NULL
We need tis:
[FIELD] [bit] NOT NULL
ALTER TABLE [dbo].[TABLE] ADD DEFAULT ((0)) FOR [FIELD] GO
UPDATE: This locking only happens with new records added from Access, but not with the original records of the SQL table.
This is the script to create the table:
`
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Chapas].[INFO_CHAPAS](
[ID_INFO_CHAPA] [int] IDENTITY(1,1) NOT NULL,
[COD_EQUIPO] [int] NULL,
[EQUIPO] [nvarchar](255) NULL,
[NUMERO_SERIE] [nvarchar](255) NULL,
[FASES] [nvarchar](255) NULL,
[VOLTAJE] [nvarchar](255) NULL,
[FRECUENCIA] [nvarchar](255) NULL,
[POTENCIA] [nvarchar](255) NULL,
[AÑO] [int] NULL,
[IMPRESO] [bit] NULL,
[SELECTOR_REGISTRO] [bit] NULL,
[USUARIO] [int] NULL,
[FECHA_IMPRESION] datetime NULL
CONSTRAINT [INFO_CHAPAS_PK] PRIMARY KEY NONCLUSTERED
(
[ID_INFO_CHAPA] 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 [Chapas].[INFO_CHAPAS] ADD DEFAULT ((0)) FOR [IMPRESO]
GO
`

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

SQL Server error with Identity value when inserting row

Weird situation here. I'm inserting a row in a table with a primary key with IDENTITY (1,1), but the value that it uses is waaay wrong. This is the table:
CREATE TABLE [dbo].[adm_tm_modulo](
[id_modulo] [int] IDENTITY(1,1) NOT NULL,
[codigo] [varchar](255) NULL,
[descripcion] [varchar](255) NULL,
[estado] [int] NOT NULL,
[fecha_actualizacion] [datetime2](7) NULL,
[fecha_creacion] [datetime2](7) NULL,
[id_usuario_actualizacion] [int] NOT NULL,
[id_usuario_creacion] [int] NOT NULL,
[nombre] [varchar](255) NULL,
[ruta] [varchar](255) NULL,
PRIMARY KEY CLUSTERED
(
[id_modulo] 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]
GO
Right now, it has 4 rows as you can see:
But whenever I insert a new row, the primary key ends up as if the IDENTITY value starts at 1000. This is what happens after I execute the INSERT:
I am certain that I have never inserted that many rows before, nor anyone else as this is a private DB in my own PC. Also, adding all the rows of all the tables, they are around 400 (not even close to 1000). And I tried inserting in other tables but the same thing is happening, only that in some tables it inserts a value from 3001 foward, or 4001, etc. It always starts with the first number after a thousand.
Any help about why this is happening would be very appreciated.
You may want to use a SEQUENCE object, which gives you more control on behavior of values generating.
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver15
Another option (which I prefer better) is to create an INSERT TRIGGER and define the logic you want in it

How to fix "There is already an object named ' ' in the database" error in sql server

I have created this table, I can't enter data manually because of this error.
USE [Butterfly]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[VM_Vehicles](
[VehicleID] [nvarchar](100) NOT NULL,
[VehicleType] [nvarchar](100) NULL,
[RegistrationNo] [nvarchar](100) NULL,
[PurchaseDate] [date] NULL,
[Make] [nvarchar](100) NULL,
[Model] [nvarchar](100) NULL,
[ChassisNo] [nvarchar](100) NULL,
[EngineNo] [nvarchar](100) NULL,
[EngineCapacity] [nvarchar](100) NULL,
[YearofManufacture] [nvarchar](100) NULL,
[SeatingCapacity] [nvarchar](100) NULL,
[ContactName] [nvarchar](100) NULL,
[Phone] [nvarchar](50) NULL,
[VendorID] [int] NOT NULL,
[Picture] [image] NULL,
[VoucherNo] [int] NOT NULL,
CONSTRAINT [PK_VM_Vehicles1] PRIMARY KEY CLUSTERED
(
[VehicleID] 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
I have tried using this code to find what's wrong with my database. so far no luck finding error.
IF object_id("tempdb..#VM_Vehicles") is not null
DROP TABLE #VM_Vehicles
CREATE TABLE #VM_Vehicles (vehicleID nvarchar(100), ...);
I already tried changing constraint name and table name. That didn't provide me a answer either.
You are creating a persistent table VM_Vehicles in database Butterfly. However, you are checking a temporary table #VM_Vehicles in database TempDB:
IF object_id("tempdb..#VM_Vehicles") is not null
So you are checking another table from another database and so you have a such error:
There is already an object named ' ' in the database
The correct check statement should look like this:
USE Butterfly
IF OBJECT_ID("VM_Vehicles") IS NOT NULL DROP TABLE VM_Vehicles
CREATE TABLE [dbo].[VM_Vehicles](VehicleID nvarchar(100), ...);

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

Resources