I know this has been asked before, but none of the answers are fixing the issue, need another set of eyes. I'm trying to create a table and seed it with some values. I don't have any value constraints so I'm not sure why I'm getting the error I'm getting.
CREATE TABLE [dbo].[AvailableTime]
(
[Id] [INT] IDENTITY(1,1) NOT NULL,
[TimeString] [NCHAR] NOT NULL,
[TimeValue] [NCHAR] NOT NULL,
[CompanyId] [INT] NOT NULL,
[CompanyName] [NVARCHAR](MAX) NULL,
[LocationId] [INT] NOT NULL,
[LocationName] [NVARCHAR] NOT NULL,
[IsClaimed] [BIT] NOT NULL,
CONSTRAINT [PK_AvailableTime]
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]
GO
INSERT INTO dbo.AvailableTime (TimeString, TimeValue, CompanyId, CompanyName,
LocationId, LocationName, IsClaimed)
VALUES ('2019-01-07T00:00:00', '12:00 AM', 1, 'Company',
2, 'Inver Grove', 'FALSE');
You should never specify a CHAR, VARCHAR, NCHAR or NVARCHAR column (or variable, or parameter) without specifying an explicit length!
If you omit the specific length, in some cases you'll end up with variable or column of exactly ONE character of length! That's typically NOT what you want!
ALSO: why are you storing TimeString and TimeValue as NCHAR?? Doesn't make any sense - use the most appropriate datatype - and here, that would be DATETIME2(n) and TIME.
So define your table like this:
CREATE TABLE [dbo].[AvailableTime]
(
[Id] [INT] IDENTITY(1,1) NOT NULL,
[TimeString] DATETIM2(0) NOT NULL,
[TimeValue] TIME(0) NOT NULL,
[CompanyId] [INT] NOT NULL,
[CompanyName] [NVARCHAR](MAX) NULL,
[LocationId] [INT] NOT NULL,
[LocationName] [NVARCHAR](100) NOT NULL,
[IsClaimed] [BIT] NOT NULL,
and you should be fine.
You need declare the size of your CHAR & VARCHAR fields in the table:
CREATE TABLE [dbo].[AvailableTime]
(
[Id] [INT] IDENTITY(1,1) NOT NULL,
[TimeString] [NCHAR](50) NOT NULL,
[TimeValue] [NCHAR](50) NOT NULL,
[CompanyId] [INT] NOT NULL,
[CompanyName] [NVARCHAR](MAX) NULL,
[LocationId] [INT] NOT NULL,
[LocationName] [NVARCHAR](50) NOT NULL,
[IsClaimed] [BIT] NOT NULL,
CONSTRAINT [PK_AvailableTime]
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]
GO
You have a error in insert query, mismatch types in last column.
INSERT INTO dbo.AvailableTime (TimeString, TimeValue, CompanyId, CompanyName,
LocationId, LocationName, IsClaimed)
VALUES ('2019-01-07T00:00:00', '12:00 AM', 1, 'Company', 2, 'Inver Grove', 0);
Related
I am working in two table. one is AspNetUser and second is App_User so I am adding any records in aspnetuser then one trigger will be fire and it will add that same data in app_usr table.
But in my case there is one issue all other values of records is inserting in app_user table but there is 2 values is not inserting in new table
AspNetUser Table schema:
CREATE TABLE [dbo].[AspNetUsers](
[Id] [nvarchar](128) NOT NULL,
[Email] [nvarchar](256) NULL,
[UserName] [nvarchar](256) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[MiddleInitial] [nvarchar](1) NULL,
[LastName] [nvarchar](50) NOT NULL,
[AccountID] [int] NOT NULL,
[SystemLevel] [int] NOT NULL,
[CreatedBy] [int] NULL,
[active] [bit] NOT NULL,
[VendorAccountID] [int] NOT NULL,
[VendorUser] [bit] NOT NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED
(
[Id] 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
Please see below is App_User's schema:
CREATE TABLE [dbo].[App_User](
[ID] [int] IDENTITY(1000,1) NOT NULL,
[account_id] [int] NOT NULL,
[system_level] [int] NOT NULL,
[logon] [char](256) NOT NULL,
[first_name] [char](50) NULL,
[middle_initial] [char](1) NULL,
[last_name] [char](50) NULL,
[email] [char](256) NULL,
[description] [char](250) NULL,
[created_by] [int] NOT NULL,
[creation_date] [datetime] NULL,
[modified_by] [int] NULL,
[modification_date] [datetime] NULL,
[active] [bit] NOT NULL,
[default_approver] [bit] NULL,
[vendor_account_id] [int] NULL,
[vendor_user] [bit] NOT NULL,
PRIMARY KEY CLUSTERED
(
[ID] 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]
GO
Please verify I have inserted below trigger's code:
CREATE TRIGGER [dbo].[Tr_aspnet_Users_CreateUser] ON [dbo].[AspNetUsers]
AFTER INSERT
AS
DECLARE #AccountID int
DECLARE #VendorAccountID int
DECLARE #VendorUser bit
DECLARE #SystemLevel int
DECLARE #UserName varchar(256)
DECLARE #FirstName varchar(50)
DECLARE #MiddleInitial varchar(1)
DECLARE #LastName varchar(50)
DECLARE #Email varchar(256)
DECLARE #CreatedBy int
SELECT #AccountID = i.AccountID from inserted i;
SELECT #VendorAccountID = i.VendorAccountID from inserted i;
SELECT #VendorUser = i.VendorUser from inserted i;
SELECT #SystemLevel = i.SystemLevel from inserted i;
SELECT #UserName = i.UserName from inserted i;
SELECT #FirstName = i.FirstName from inserted i;
SELECT #MiddleInitial = i.MiddleInitial from inserted i;
SELECT #LastName = i.LastName from inserted i;
SELECT #Email = i.Email from inserted i;
SELECT #CreatedBy = i.CreatedBy from inserted i;
IF(#VendorAccountID=0)
SET #VendorAccountID=NULL
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[App_User]
([active]
,[account_id]
,[system_level]
,[logon]
,[first_name]
,[middle_initial]
,[last_name]
,[email]
,[vendor_account_id]
,[vendor_user]
,[created_by]
,[creation_date])
VALUES
(1
,#AccountID
,#SystemLevel
,#UserName
,#FirstName
,#MiddleInitial
,#LastName
,#Email
,#VendorAccountID
,#VendorUser
,#CreatedBy
,GETDATE())
END
GO
Now Issue is that while trigger fires then all other value is inserting properly but vendor_account_id and vendor_user is not inserting.
Can anyone please help what should be issue here.
I have a table with 9 columns sharing same data (GN_PLACE_ID,GN_AR_NAME,GN_EN_NAME)
is good why to save them in one table or create table for each one ?
CREATE TABLE [dbo].[GENERAL](
[GN_ID] [int] IDENTITY(1,1) NOT NULL,
[GN_PLACE_ID] [int] NULL,
[GN_AR_NAME] [nvarchar](1000) NULL,
[GN_EN_NAME] [nvarchar](1000) NULL,
[GN_IS_DEVICE_TYPE] [bit] NULL,
[GN_IS_ISSUE_TYPE] [bit] NULL,
[GN_IS_REPAIR_TYPE] [bit] NULL,
[GN_IS_CANCEL_REASON] [bit] NULL,
[GN_IS_SUSPEND_REASON] [bit] NULL,
[GN_IS_MANUFACTURER] [bit] NULL,
[GN_IS_DEPARTMENT] [bit] NULL,
[GN_IS_PRIORITY] [bit] NULL,
[GN_IS_SUPPORT_SECTION] [bit] NULL,
CONSTRAINT [PK_GENERAL] PRIMARY KEY CLUSTERED
(
[GN_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
thanks
I have a table with approximately 1 million rows. Part of our maintenance involves deleting old row each day, but this is taking about 40 minutes.
The delete statement is:
DELETE
FROM [dbGlobalPricingMatrix].[dbo].[tblPricing]
WHERE type = 'car'
AND capid NOT IN
(SELECT cder_id FROM PUB_CAR.dbo.CapDer WHERE cder_discontinued
IS NULL OR DATEDIFF(dd,cder_discontinued,GETDATE()) <= 7)
AND source = #source
Is there anything I can do to improve the performance?
Thanks
As Requested:
CREATE TABLE [dbo].[tblPricing](
[id] [int] IDENTITY(1,1) NOT NULL,
[type] [varchar](50) NULL,
[capid] [int] NULL,
[source] [varchar](50) NULL,
[product] [varchar](50) NULL,
[term] [int] NULL,
[milespa] [int] NULL,
[maintained] [bit] NULL,
[price] [money] NULL,
[created] [datetime] NULL,
[updated] [datetime] NULL,
[notes] [varchar](1000) NULL,
[painttype] [char](1) NULL,
[activeflag] [bit] NULL,
[DealerId] [int] NULL,
[FunderId] [int] NULL,
[IsSpecial] [bit] NULL,
[username] [varchar](50) NULL,
[expiry] [datetime] NULL,
CONSTRAINT [PK_tblPricing] 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]
CREATE TABLE [dbo].[CAPDer](
[cder_ID] [int] NOT NULL,
[cder_capcode] [char](20) NULL,
[cder_mancode] [int] NULL,
[cder_rancode] [int] NULL,
[cder_modcode] [int] NULL,
[cder_trimcode] [int] NULL,
[cder_name] [varchar](50) NULL,
[cder_introduced] [datetime] NULL,
[cder_discontinued] [datetime] NULL,
[cder_orderno] [int] NULL,
[cder_vehiclesector] [tinyint] NULL,
[cder_doors] [tinyint] NULL,
[cder_drivetrain] [char](1) NULL,
[cder_fueldelivery] [char](1) NULL,
[cder_transmission] [char](1) NULL,
[cder_fueltype] [char](1) NULL,
CONSTRAINT [PK_CapDer] PRIMARY KEY CLUSTERED
(
[cder_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]
Okay, as I suspected, you do not seem to have indexes for the fields that you reference in your DELETE query. So, add indexes for type, capid, cder_discontinued, and source.
Additionally, you might want to try AND capid IN (SELECT cder_id FROM PUB_CAR.dbo.CapDer WHERE cder_discontinued IS NOT NULL AND DATEDIFF(dd,cder_discontinued,GETDATE()) > 7). The optimizer of MS-SQL-Server should actually be doing this for you, but you never know, it is worth trying.
I have the following query that I am running on my database server but it takes about 30 seconds to run and I can't work out why this is.
SELECT *
FROM [dbo].[PackageInstance] AS packInst
INNER JOIN [dbo].[PackageDefinition] AS packageDef
ON packInst.[PackageDefinitionID] = packageDef.[PackageDefinitionID]
LEFT OUTER JOIN [dbo].[PackageInstanceContextDef] AS contextDef
ON packInst.[PackageInstanceID] = contextDef.[PackageInstanceID]
This produced the following execution plan which to me looks to be good....so I can't understand why it takes so much time to execute where the resulting data is only 100,000 records (which should be a walk in the park for SQL Server).
Any ideas what could be causing this long execution time?
I have looked at the query in Profiler to see what the stats where on it and they are as follows:
CPU - 4711
Reads - 744453
Writes - 9
Duration - 26329
The following are the table definitions:
CREATE TABLE [dbo].[PackageDefinition](
[PackageDefinitionID] [int] IDENTITY(1,1) NOT NULL,
[ts] [timestamp] NOT NULL,
[ProgramID] [int] NULL,
[VendorID] [int] NULL,
[PackageExecutionTypeID] [int] NULL,
[PackageDefinitionStatusID] [int] NOT NULL,
[IsInternal] [bit] NOT NULL,
[Name] [dbo].[D_Name] NOT NULL,
[Description] [dbo].[D_Description] NOT NULL,
[CreatedDate] [datetime] NOT NULL,
[PublishedDate] [datetime] NULL,
[OwnerUserGuid] [uniqueidentifier] NOT NULL,
[ProcessDefinitionMainID] [int] NULL,
[KeyInfoHtml] [nvarchar](max) NULL,
[DescriptionHtml] [nvarchar](max) NULL,
[WhatToExpectHtml] [nvarchar](max) NULL,
[BestPracticesHtml] [nvarchar](max) NULL,
[RecommendedJourneysHtml] [nvarchar](max) NULL,
[RequiresSLAAgreement] [bit] NOT NULL,
[SLAFileAssetID] [int] NULL,
[ImageDataID] [int] NULL,
[VideoHtml] [nvarchar](max) NULL,
[VideoAssetID] [int] NULL,
[UseMapCosts] [bit] NOT NULL,
[CostMin] [money] NOT NULL,
[CostMax] [money] NOT NULL,
[LandingPageVisitCount] [int] NOT NULL,
[IsDeleted] [dbo].[D_IsDeleted] NOT NULL,
[CreatedByUserGuid] [uniqueidentifier] NOT NULL,
[OrderHtml] [nvarchar](max) NULL,
CONSTRAINT [PK_PackageDefinition] PRIMARY KEY CLUSTERED
(
[PackageDefinitionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[PackageInstance](
[PackageInstanceID] [int] IDENTITY(1,1) NOT NULL,
[ts] [timestamp] NOT NULL,
[PackageDefinitionID] [int] NOT NULL,
[PackageStatusID] [int] NOT NULL,
[Name] [dbo].[D_Description] NOT NULL,
[CampaignID] [int] NULL,
[MarketingPlanID] [int] NULL,
[CountryID] [int] NULL,
[DateEntered] [datetime] NULL,
[DateExecuted] [datetime] NULL,
[ProcessID] [int] NULL,
[OrderedByUserGuid] [uniqueidentifier] NULL,
[RequestedByUserGuid] [uniqueidentifier] NULL,
[SLAEndDate] [datetime] NULL,
CONSTRAINT [PK_PackageInstance] PRIMARY KEY CLUSTERED
(
[PackageInstanceID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[PackageInstanceContextDef](
[PackageInstanceContextDefID] [int] IDENTITY(1,1) NOT NULL,
[ts] [timestamp] NOT NULL,
[PackageInstanceID] [int] NOT NULL,
[ContextObjectDefID] [int] NOT NULL,
[EnteredFieldValue] [varchar](max) NULL,
[SelectedListValueID] [int] NULL,
[AssetIdsString] [nvarchar](max) NULL,
[SelectedListValueIdsString] [nvarchar](max) NULL,
[ContextObjectFieldName] [nvarchar](30) NOT NULL,
CONSTRAINT [PK_PackageInstanceContextDef] PRIMARY KEY CLUSTERED
(
[PackageInstanceContextDefID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Remove the * in SELECT *
It will always scan because you ask for all columns. And do you have clustered indexes?
The answer turned out to be what #MartinSmith suggested. Because the PackageDefinition table contained about 8 NVARCHAR(MAX) columns, when the resulting join was created and that was over 100k rows, this was causing the varchar(max) values to be re-read over and over and they exist in out of row pages. Hence the large number of logical reads.
Thanks all for your support, just have to figure out to make the entity framework produce the query that I want.
What happens if you add the following index...
CREATE NONCLUSTERED INDEX ix ON PackageDefinition(PackageDefinitionID)
...and try the following to reduce the width of the data going into the sort?
SELECT packInst.*,
packageDef2.*,
contextDef.*
FROM [dbo].[PackageInstance] AS packInst
INNER MERGE JOIN [dbo].[PackageDefinition] AS packageDef
ON packInst.[PackageDefinitionID] = packageDef.[PackageDefinitionID]
LEFT OUTER MERGE JOIN [dbo].[PackageInstanceContextDef] AS contextDef
ON packInst.[PackageInstanceID] = contextDef.[PackageInstanceID]
INNER MERGE JOIN [dbo].[PackageDefinition] AS packageDef2
ON packageDef.[PackageDefinitionID] = packageDef2.[PackageDefinitionID]
OF course * should not be used as even if you need all columns you definitely won't need the same columns twice as the result of the JOIN but this is just to maintain the semantics of your original query.
Using this Spatial Query I am trying to get all the country information which intersects the point 78,22. Expected result is information of "India", but this query is returning no rows.
select * from countryspatial
where
geom.STIntersects((geometry::STGeomFromText('POINT (78 22)', 4326)))>0;
This is the table definition:
CREATE TABLE [dbo].[CountrySpatial](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ObjectID] [bigint] NULL,
[FIPS_CNTRY] [nvarchar](255) NULL,
[GMI_CNTRY] [nvarchar](255) NULL,
[ISO_2DIGIT] [nvarchar](255) NULL,
[ISO_3DIGIT] [nvarchar](255) NULL,
[ISO_NUM] [int] NULL,
[CNTRY_NAME] [nvarchar](255) NULL,
[LONG_NAME] [nvarchar](255) NULL,
[ISOSHRTNAM] [nvarchar](255) NULL,
[UNSHRTNAM] [nvarchar](255) NULL,
[LOCSHRTNAM] [nvarchar](255) NULL,
[LOCLNGNAM] [nvarchar](255) NULL,
[STATUS] [nvarchar](255) NULL,
[POP2005] [bigint] NULL,
[SQKM] [float] NULL,
[SQMI] [float] NULL,
[COLORMAP] [smallint] NULL,
[geom] [geometry] NULL,
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]
GO
ALTER TABLE [dbo].[CountrySpatial] WITH CHECK ADD CONSTRAINT [enforce_srid_geometry_CountrySpatial] CHECK (([geom].[STSrid]=(0)))
GO
ALTER TABLE [dbo].[CountrySpatial] CHECK CONSTRAINT [enforce_srid_geometry_CountrySpatial]
GO
The first thing to comment is that earth surface points should be stored using Geography, not Geometry. There are differences to the storage and how the functions work (even if similarly named)
Here is a working example:
Simplified table:
CREATE TABLE CountrySpatial(
ID int IDENTITY(1,1) NOT NULL PRIMARY KEY,
geog geography NULL)
GO
Insert something that resembles a diamond around India
INSERT INTO CountrySpatial(geog)
VALUES (geography::STGeomFromText('POLYGON((' +
'77.22702 28.67613, ' + -- new delhi (top)
'72.566071 23.059516, ' + -- ahmedabad (left)
'77.593689 13.005227, ' + -- bengaluru (bottom)
'88.374023 22.614011, ' + -- kolkata (right)
'77.22702 28.67613))', 4326));
Find the match. It is UNION-ed to the Point being sought. STBuffer increases the point to a 100km radius, so that it will show up when viewed together with the Geography record found (switch to Spatial tab in the output)
select geog
from countryspatial
where geog.STIntersects(geography::STGeomFromText('POINT (78 22)', 4326))>0
union all
select geography::STGeomFromText('POINT (78 22)', 4326).STBuffer(100000)