How to fix "There is already an object named ' ' in the database" error in sql server - 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), ...);

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

The table does not support the 'updatedAt' system property

I recently lost an Azure database (there's no recovering it...) and had to recreate from scratch. This database is supposed to sync to our iOS app, which allows us to keep the data between the DB and Device in sync. Unfortunately, after recreating the database and tables manually (because I'm not sure how else to do it?), my App Service is able to connect to the SQL database, and from the app side it looks like it's syncing, but when I look at the logs, I'm seeing the following error for each table, and I get no data:
The table 'tablename' does not support the 'updatedAt' system
property.
I've searched everywhere for this system property and cannot for the life of me find out where it's at or how to relate it to the database tables.
Here's an example of a schema for a table I'm having issues with:
CREATE TABLE [dbo].[CurrentItems](
[id] [nvarchar](128) NOT NULL,
[siteID] [nvarchar](max) NOT NULL,
[residentUnitNumber] [nvarchar](50) NULL,
[residentID] [nvarchar](max) NULL,
[pinUsedToCheckOut] [nvarchar](max) NULL,
[itemType] [nvarchar](max) NULL,
[itemTrackingNumber] [nvarchar](max) NULL,
[itemNotes] [nvarchar](max) NULL,
[itemID] [nvarchar](max) NULL,
[deviceSerialNumber] [nvarchar](max) NULL,
[dateRecordCreated] [datetime] NOT NULL,
[dateLastModified] [datetime] NULL,
[dateItemCheckedOut] [datetime] NULL,
[dateItemCheckedIn] [datetime] NULL,
[currentStatus] [nvarchar](max) NOT NULL,
[checkedOutEmpID] [nvarchar](max) NULL,
[checkedInEmpID] [nvarchar](max) NOT NULL,
[carrierName] [nvarchar](max) NULL,
[barcodeValue] [nvarchar](max) NULL,
[version] [timestamp] NOT NULL,
[deleted] [bit] NOT NULL,
[createdAt] [datetimeoffset](7) NOT NULL,
[updatedAt] [datetimeoffset](7) NOT NULL,
CONSTRAINT [PK_CurrentItems] PRIMARY KEY CLUSTERED
(
[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] TEXTIMAGE_ON [PRIMARY]
I really need to get this app back to syncing, even if that means starting from scratch, but I can't seem to find the instructions for that on the Azure website anymore, so I don't know what else to do.
Any help or guidance anyone can provide would be greatly appreciated!

Elmah.MVC + NLog - store all errors in one place

I'm working on an ASP.NET MVC 5 app in Visual Studio 2015. We use NLog to write some errors and other information to the database in our try/catch blocks. However, it would be nice to also implement ELMAH.MVC, so that any/all uncaught exceptions get caught/logged and the user redirected to a friendly page.
Here's our NLog table structure; note that ApplicationId is not equivalent to Application on the ELMAH_Error table; people apply for things through the app, and this is the ID we assign when they start the process. And RouteId is not an MVC route.
CREATE TABLE [dbo].[Log](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ApplicationId] [int] NULL,
[RouteId] [int] NULL,
[MachineName] [varchar](50) NULL,
[TimeStamp] [datetime2](7) NULL,
[LogLevel] [varchar](5) NULL,
[Logger] [nvarchar](max) NULL,
[Message] [nvarchar](max) NULL,
[Exception] [nvarchar](max) NULL,
[StackTrace] [nvarchar](max) NULL,
CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED
(
[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] TEXTIMAGE_ON [PRIMARY]
The SQL for ELMAH's table looks like this (from the script here):
CREATE TABLE [dbo].[ELMAH_Error]
(
[ErrorId] UNIQUEIDENTIFIER NOT NULL,
[Application] NVARCHAR(60) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Host] NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Type] NVARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Source] NVARCHAR(60) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Message] NVARCHAR(500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[User] NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[StatusCode] INT NOT NULL,
[TimeUtc] DATETIME NOT NULL,
[Sequence] INT IDENTITY (1, 1) NOT NULL,
[AllXml] NTEXT COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
)
ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Is it possible to have the ELMAH_Error table contain the additional columns found on the NLog Log table, or vice versa? How can I write errors from NLog to the ELMAH table?
Thank you.
Update 1: Came across this: https://github.com/ccellar/nlog-elmah-target, but it lacks any documentation on how you use it. Would this redirect all NLog exceptions to ELMAH?
Update 2: A related question has been asked about adding the Exception.Data dictionary to ELMAH. This way, any number of key/value pairs can be added to what Elmah stored. This way, all the data now stored with NLog would be stored on ELMAH.
Found a solution that works very nicely: https://github.com/NLog/NLog.Elmah/
To install,
Install-Package NLog.Elmah
Update-Package NLog
I also installed NLog.Web package, but isn't required for this.
Modify NLog.config to include:
<targets><target name="target1" xsi:type="Elmah" LogLevelAsType"false" /></targets>
<rules><logger name="*" minlevel="Info" writeTo="target1" /></rules>
Now just decorate any controller/class that uses NLog with the target:
[Target("Elmah")]
public class HomeController : Controller
{
private static Logger _logger = LogManager.GetCurrentClassLogger();
public ActionResult Index()
{
// Other code...
var logEvent = new LogEventInfo(LogLevel.Info, _logger.Name, "Some information...");
_logger.Log(logEvent);
// Other code...
}
}
You can also throw the attribute on a base controller/class and cover all your application.

Can't create relationships in SQL Server diagram

I'm using SQL Server Management Studio 2008 R2 to create a database diagram. I've dropped in the tables that already have foreign key relationships; in addition, all the tables have primary keys. However, when I try to drag and drop from either table onto the other, I get the following error popup:
Primary key or UNIQUE constraint must be defined for table 'Results' before it can participate in a relationship.
Again, both tables have primary keys. The database was imported via Microsoft's SQL Server Migration Assistant for Access tool to preserve relationships and keys. What am I doing wrong?
Update: Here's the table:
CREATE TABLE [dbo].[Results](
[Result_AN] [int] IDENTITY(1,1) NOT NULL,
[Detail_AN] [int] NULL,
[Drug] [nvarchar](10) NULL,
[LDrug] [nvarchar](10) NULL,
[Lab_Result] [nvarchar](10) NULL,
[MRO_Result] [nvarchar](10) NULL,
[Confirm] [nvarchar](6) NULL,
[CutOff] [nvarchar](6) NULL,
[Quant] [nvarchar](10) NULL,
[Screen] [nvarchar](6) NULL,
[Unit] [nvarchar](6) NULL,
[Deleted] [bit] NULL,
[Scrn_Result] [nvarchar](10) NULL,
[SSMA_TimeStamp] [timestamp] NOT NULL,
CONSTRAINT [Results$PrimaryKey] PRIMARY KEY CLUSTERED
(
[Result_AN] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

SQl Server 2012 error

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.

Resources