Related
I have 3 tables . Orders, OrderLines, BillLines. The primary key of Orders is a FK in the 2 other tables.
I am trying to query the client debit and credit amount.
The Action table just describe the type of the transaction
The Subindex table stores the clients names and ids
CREATE TABLE [dbo].[Orders](
[OrderID] [int] IDENTITY(1,1) NOT NULL,
[Ordertype] [nvarchar](max) NULL,
[Orderdate] [datetime2](7) NOT NULL,
[transactionNo] [int] NOT NULL,
[DocumentNo] [int] NOT NULL,
[ordernote] [nvarchar](max) NULL,
[Profittax] [decimal](18, 2) NOT NULL,
[DescDR] [decimal](18, 2) NULL,
[DescCR] [decimal](18, 2) NULL,
[ActionsID] [smallint] NOT NULL,
[SubIndexesID] [smallint] NULL,
CONSTRAINT [PK_dbo.Orders] PRIMARY KEY CLUSTERED
(
[OrderID] 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 TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [FK_dbo.Orders_dbo.Actions_ActionsID] FOREIGN KEY([ActionsID])
REFERENCES [dbo].[Actions] ([ActionsID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_dbo.Orders_dbo.Actions_ActionsID]
GO
ALTER TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [FK_dbo.Orders_dbo.SubIndexes_SubIndexesID] FOREIGN KEY([SubIndexesID])
REFERENCES [dbo].[SubIndexes] ([SubIndexesID])
GO
ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_dbo.Orders_dbo.SubIndexes_SubIndexesID]
GO
CREATE TABLE [dbo].[Orderlines](
[OrderlinesID] [int] IDENTITY(1,1) NOT NULL,
[OrderID] [int] NOT NULL,
[Qin] [smallint] NOT NULL,
[Qout] [smallint] NOT NULL,
[Price] [decimal](18, 2) NOT NULL,
[currency] [nvarchar](max) NULL,
[Discount] [decimal](18, 2) NOT NULL,
[VAT] [decimal](18, 2) NOT NULL,
[ItemsID] [smallint] NOT NULL,
[Total] [decimal](18, 2) NOT NULL,
[Vatvalue] [decimal](18, 2) NOT NULL,
[Net] [decimal](18, 2) NOT NULL,
CONSTRAINT [PK_dbo.Orderlines] PRIMARY KEY CLUSTERED
(
[OrderlinesID] 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 TABLE [dbo].[Orderlines] ADD DEFAULT ((0)) FOR [Total]
GO
ALTER TABLE [dbo].[Orderlines] ADD DEFAULT ((0)) FOR [Vatvalue]
GO
ALTER TABLE [dbo].[Orderlines] ADD DEFAULT ((0)) FOR [Net]
GO
ALTER TABLE [dbo].[Orderlines] WITH CHECK ADD CONSTRAINT [FK_dbo.Orderlines_dbo.Items_ItemsID] FOREIGN KEY([ItemsID])
REFERENCES [dbo].[Items] ([ItemsID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Orderlines] CHECK CONSTRAINT [FK_dbo.Orderlines_dbo.Items_ItemsID]
GO
ALTER TABLE [dbo].[Orderlines] WITH CHECK ADD CONSTRAINT [FK_dbo.Orderlines_dbo.Orders_OrderID] FOREIGN KEY([OrderID])
REFERENCES [dbo].[Orders] ([OrderID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Orderlines] CHECK CONSTRAINT [FK_dbo.Orderlines_dbo.Orders_OrderID]
GO
CREATE TABLE [dbo].[BillLines](
[BillLinesID] [int] IDENTITY(1,1) NOT NULL,
[VALIN] [decimal](18, 2) NULL,
[VALOut] [decimal](18, 2) NULL,
[OrderID] [int] NOT NULL,
[SubIndexesID] [smallint] NULL,
CONSTRAINT [PK_dbo.BillLines] PRIMARY KEY CLUSTERED
(
[BillLinesID] 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].[BillLines] WITH CHECK ADD CONSTRAINT [FK_dbo.BillLines_dbo.Orders_OrderID] FOREIGN KEY([OrderID])
REFERENCES [dbo].[Orders] ([OrderID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[BillLines] CHECK CONSTRAINT [FK_dbo.BillLines_dbo.Orders_OrderID]
GO
ALTER TABLE [dbo].[BillLines] WITH CHECK ADD CONSTRAINT [FK_dbo.BillLines_dbo.SubIndexes_SubIndexesID] FOREIGN KEY([SubIndexesID])
REFERENCES [dbo].[SubIndexes] ([SubIndexesID])
GO
ALTER TABLE [dbo].[BillLines] CHECK CONSTRAINT [FK_dbo.BillLines_dbo.SubIndexes_SubIndexesID]
GO
CREATE TABLE [dbo].[Actions](
[ActionsID] [smallint] IDENTITY(1,1) NOT NULL,
[Actionname] [nvarchar](4000) NOT NULL,
CONSTRAINT [PK_dbo.Actions] PRIMARY KEY CLUSTERED
(
[ActionsID] 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
CREATE TABLE [dbo].[SubIndexes](
[SubIndexesID] [smallint] IDENTITY(1,1) NOT NULL,
[Subname] [nvarchar](max) NULL,
[AccountIndexID] [smallint] NOT NULL,
[Contacttype] [nvarchar](max) NULL,
[phones] [nvarchar](max) NULL,
[fax] [nvarchar](max) NULL,
[email] [nvarchar](max) NULL,
[address] [nvarchar](max) NULL,
[zip] [nvarchar](max) NULL,
[Website] [nvarchar](max) NULL,
CONSTRAINT [PK_dbo.SubIndexes] PRIMARY KEY CLUSTERED
(
[SubIndexesID] 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 TABLE [dbo].[SubIndexes] WITH CHECK ADD CONSTRAINT [FK_dbo.SubIndexes_dbo.AccountIndexes_AccountIndexID] FOREIGN KEY([AccountIndexID])
REFERENCES [dbo].[AccountIndexes] ([AccountIndexID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[SubIndexes] CHECK CONSTRAINT [FK_dbo.SubIndexes_dbo.AccountIndexes_AccountIndexID]
GO
My Query is
select distinct
o.OrderID ,
s.Subname as subindexname,
o.SubIndexesID as Subindex ,
sum(ol.Qout * (ol.Price - ol.Discount)* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALOut) -iif(o.ActionsID between 3 and 4, o.DescDR+o.DescCR,0) as Debit ,
sum(ol.Qin * (ol.Price - ol.Discount )* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALIN)-iif(o.ActionsID not between 3 and 4,o.DescCR + o.DescDR,0) as Credit
from Orders o
inner join
Orderlines ol on o.OrderID = ol.OrderID
inner join
BillLines bl on o.OrderID = bl.OrderID
inner join
SubIndexes s on o.SubIndexesID= s.SubIndexesID
group by o.OrderID,
o.ActionsID,
o.SubIndexesID,
o.DescCR,
o.DescDR,
ol.Qout,
ol.Qin,
s.Subname,
s.SubIndexesID,
ol.VAT,
ol.Price,
ol.Discount
having o.ActionsID=2
or o.ActionsID=3
or o.ActionsID=4
or o.ActionsID=5
I want to show the debit and the credit in one row even I have multiple orderlines for one order.
It is necessary to include all cols in Group-by in Select as well:
select distinct
o.OrderID ,
s.Subname as subindexname,
o.SubIndexesID as Subindex ,
o.ActionsID,
o.DescCR,
o.DescDR,
ol.Qout,
ol.Qin,
s.Subname,
s.SubIndexesID,
ol.VAT,
ol.Price,
ol.Discount
sum(ol.Qout * (ol.Price - ol.Discount)* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALOut) -iif(o.ActionsID between 3 and 4, o.DescDR+o.DescCR,0) as Debit ,
sum(ol.Qin * (ol.Price - ol.Discount )* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALIN)-iif(o.ActionsID not between 3 and 4,o.DescCR + o.DescDR,0) as Credit
from Orders o
inner join
Orderlines ol on o.OrderID = ol.OrderID
inner join
BillLines bl on o.OrderID = bl.OrderID
inner join
SubIndexes s on o.SubIndexesID= s.SubIndexesID
group by o.OrderID,
o.ActionsID,
o.SubIndexesID,
o.DescCR,
o.DescDR,
ol.Qout,
ol.Qin,
s.Subname,
s.SubIndexesID,
ol.VAT,
ol.Price,
ol.Discount
having o.ActionsID=2
or o.ActionsID=3
or o.ActionsID=4
or o.ActionsID=5
I have a query that is causing deadlocks on the primary key of a table. The query can run from multiple different code paths. New DB connections are created each time a query needs to be run, so this is not a connection reuse issue, as far as I can tell.
The query in question is
CREATE PROCEDURE [dbo].[upd_task] #taskid INTEGER
, #state INTEGER
, #args NVARCHAR( max )
, #retries INTEGER
, #started bit
, #finished bit
, #username NVARCHAR( 50 ) AS BEGIN BEGIN TRAN
UPDATE task WITH (updlock) SET
[state] = #state
, args = #args
, retries = #retries
, started = #started
, finished = #finished
, modifier = #username
, modified = GETDATE()
WHERE
task.task_id = #taskid COMMIT END
The table is defined as
CREATE TABLE [dbo].[task](
[task_id] [bigint] IDENTITY(1,1) NOT NULL,
[job_id] [bigint] NOT NULL,
[car_id] [int] NOT NULL,
[uuid] [nvarchar](60) NULL,
[user_priority] [bit] NOT NULL,
[args] [nvarchar](max) NULL,
[state] [int] NOT NULL,
[started] [bit] NOT NULL,
[finished] [bit] NOT NULL,
[failed] [bit] NOT NULL,
[error_msg] [nvarchar](max) NULL,
[retries] [int] NULL,
[exec_time] [datetime] NULL,
[created] [datetime] NULL,
[creator] [nvarchar](50) NULL,
[modified] [datetime] NULL,
[modifier] [nvarchar](50) NULL,
CONSTRAINT [PK_task_id] PRIMARY KEY CLUSTERED
(
[task_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] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [user_priority]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [state]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [started]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [finished]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [failed]
GO
ALTER TABLE [dbo].[task] ADD DEFAULT ((0)) FOR [retries]
GO
ALTER TABLE [dbo].[task] WITH CHECK ADD CONSTRAINT [FK_task_car] FOREIGN KEY([car_id])
REFERENCES [dbo].[car] ([car_id])
GO
ALTER TABLE [dbo].[task] CHECK CONSTRAINT [FK_task_car]
GO
ALTER TABLE [dbo].[task] WITH CHECK ADD CONSTRAINT [FK_task_job] FOREIGN KEY([job_id])
REFERENCES [dbo].[job] ([job_id])
GO
ALTER TABLE [dbo].[task] CHECK CONSTRAINT [FK_task_job]
GO
The dead lock graph, shown below, has two instances of the same query running [upd_task.e
I have two tables as follows where in the later is a table for storing the archived data.
CREATE TABLE [ERP].[Transaction](
[TransactionID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[TransactionDateTime] [datetime] NOT NULL,
[TransactionName] [int] NOT NULL,
[TransactionData] [xml] NOT NULL,
[AcknowledgementDateTime] [datetime] NULL,
[AcknowledgementCode] [int] NULL,
[AcknowledgementDescription] [nvarchar](max) NULL,
[TransactionGUID] [uniqueidentifier] NOT NULL,
[WorkCenter] [nvarchar](10) NULL,
CONSTRAINT [pkTransaction] PRIMARY KEY NONCLUSTERED
(
[TransactionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
GO
ALTER TABLE [ERP].[Transaction] ADD CONSTRAINT [defTransactionTransactionGUID] DEFAULT (newid()) FOR [TransactionGUID]
GO
CREATE TABLE [ERP].[TransactionArchived](
[TransactionID] [int] IDENTITY(1,1) NOT NULL,
[TransactionDateTime] [datetime] NOT NULL,
[TransactionName] [int] NOT NULL,
[TransactionData] [xml] NOT NULL,
[AcknowledgementDateTime] [datetime] NULL,
[AcknowledgementCode] [int] NULL,
[AcknowledgementDescription] [nvarchar](max) NULL,
[TransactionGUID] [uniqueidentifier] NOT NULL,
[WorkCenter] [nvarchar](10) NULL
)
GO
I have made partition as follows and create Indexes to support the partition.
CREATE PARTITION FUNCTION PartitionXFaceDataByMonthPF ( DATETIME )
AS RANGE RIGHT FOR VALUES ( '2010-01-01', '2010-02-01', '2010-03-01', '2010-04-01', '2010-05-01', '2010-06-01', '2010-07-01', '2010-08-01', '2010-09-01', '2010-10-01', '2010-11-01', '2010-12-01',
'2011-01-01', '2011-02-01', '2011-03-01', '2011-04-01', '2011-05-01', '2011-06-01', '2011-07-01', '2011-08-01', '2011-09-01', '2011-10-01', '2011-11-01', '2011-12-01',
'2012-01-01', '2012-02-01', '2012-03-01', '2012-04-01', '2012-05-01', '2012-06-01', '2012-07-01', '2012-08-01', '2012-09-01', '2012-10-01', '2012-11-01', '2012-12-01',
'2013-01-01', '2013-02-01', '2013-03-01', '2013-04-01', '2013-05-01', '2013-06-01', '2013-07-01', '2013-08-01', '2013-09-01', '2013-10-01', '2013-11-01', '2013-12-01',
'2014-01-01', '2014-02-01', '2014-03-01', '2014-04-01', '2014-05-01', '2014-06-01', '2014-07-01', '2014-08-01', '2014-09-01', '2014-10-01', '2014-11-01', '2014-12-01',
'2015-01-01', '2015-02-01', '2015-03-01', '2015-04-01', '2015-05-01', '2015-06-01', '2015-07-01', '2015-08-01', '2015-09-01', '2015-10-01', '2015-11-01', '2015-12-01' ) ;
--Create the PARTITION SCHEME
CREATE PARTITION SCHEME PartitionXFaceDataByMonthPS AS PARTITION PartitionXFaceDataByMonthPF ALL TO ( [PRIMARY] ) ;
Index Creation
ALTER TABLE [ERP].[Transaction] DROP CONSTRAINT [pkTransaction] ;
ALTER TABLE [ERP].[Transaction] ADD CONSTRAINT [pkTransaction] PRIMARY KEY NONCLUSTERED ( [TransactionID] ASC ) ON [PRIMARY] ;
/****** Object: Index [idxTransactionDateTime] Script Date: 01/14/2016 15:53:28 ******/
IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[ERP].[Transaction]') AND name = N'idxTransactionDateTime')
DROP INDEX [idxTransactionDateTime] ON [ERP].[Transaction] WITH ( ONLINE = OFF )
GO
CREATE CLUSTERED INDEX idxTransactionDateTime ON [ERP].[Transaction]( [TransactionDateTime] ) ON PartitionXFaceDataByMonthPS( [TransactionDateTime] ) ;
--DROP TABLE [ERP].[TransactionArchived] ; --This needs to execute only if we rerun the same set onace again on the same DB
SELECT * INTO [ERP].[TransactionArchived] FROM [ERP].[Transaction] WHERE 1 = 2 ;
CREATE CLUSTERED INDEX idxTransactionDateTime ON [ERP].[TransactionArchived] ( TransactionDateTime ) ON PartitionXFaceDataByMonthPS( [TransactionDateTime] ) ;
--CREATE NONCLUSTERED INDEX [pkTransaction] ON [ERP].[TransactionArchived] ( [TransactionID] ) ON PartitionXFaceDataByMonthPS( [TransactionDateTime] ) ;
GO
When I am switching data it gives an error as follows which I am struggling to find a solution. Please help
'ALTER TABLE SWITCH' statement failed. The table 'EPMSDataExchange.ERP.Transaction' is partitioned while index 'pkTransaction' is not partitioned.
thanks
pep
I created two tables , departments and employees with the following structure :
CREATE TABLE [dbo].[departments](
[department_id] [bigint] IDENTITY(10,10) NOT NULL,
[department_name] [nvarchar](30) NOT NULL,
[manager_id] [bigint] NULL,
[location_id] [bigint] NULL,
[department_notes] [varchar](150) NULL,
[created_by] [bigint] NULL,
[created_date] [datetime] NULL,
[last_updated_by] [bigint] NULL,
[last_updated_date1] [datetime] NULL,
[status] [varchar](12) NOT NULL,
CONSTRAINT [PK_departments] PRIMARY KEY CLUSTERED
(
[department_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UQ_NoRepeat] UNIQUE NONCLUSTERED
(
[department_name] ASC,
[location_id] ASC,
[status] 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
ALTER TABLE [dbo].[departments] ADD CONSTRAINT [DF_departments_created_date] DEFAULT (getdate()) FOR [created_date]
GO
ALTER TABLE [dbo].[departments] ADD CONSTRAINT [DF_departments_created_date1] DEFAULT (getdate()) FOR [last_updated_date1]
GO
ALTER TABLE [dbo].[departments] ADD CONSTRAINT [DF_departments_status] DEFAULT ('Active') FOR [status]
GO
ALTER TABLE [dbo].[departments] WITH CHECK ADD FOREIGN KEY([manager_id])
REFERENCES [dbo].[employees] ([employee_id])
GO
ALTER TABLE [dbo].[departments] WITH CHECK ADD CONSTRAINT [FK_departments_locations] FOREIGN KEY([location_id])
REFERENCES [dbo].[locations] ([location_id])
GO
ALTER TABLE [dbo].[departments] CHECK CONSTRAINT [FK_departments_locations]
GO
ALTER TABLE [dbo].[departments] WITH CHECK ADD CONSTRAINT [Ck_deptStatus] CHECK (([Status]='Deleted' OR [Status]='Active'))
GO
ALTER TABLE [dbo].[departments] CHECK CONSTRAINT [Ck_deptStatus]
GO
CREATE TABLE [dbo].[employees](
[employee_id] [bigint] NOT NULL,
[first_name] [nvarchar](20) NULL,
[last_name] [nvarchar](25) NOT NULL,
[email] [nvarchar](25) NOT NULL,
[phone_number] [nvarchar](20) NULL,
[hire_date] [date] NULL,
[job_id] [nvarchar](10) NOT NULL,
[salary] [numeric](8, 2) NOT NULL,
[commission_pct] [numeric](2, 2) NULL,
[manager_id] [bigint] NULL,
[department_id] [bigint] NOT NULL,
[allow_login] [bit] NOT NULL,
[user_id] [nvarchar](128) NULL,
[allow_email] [bit] NOT NULL,
[driv_lic_no] [nchar](20) NULL,
[reporting_to] [bigint] NULL,
[salutation] [nchar](10) NULL,
[date_of_birth] [date] NULL,
[gender] [nchar](10) NULL,
[blood_group] [nchar](10) NULL,
[Nationality] [nchar](10) NULL,
[gov_id] [nchar](25) NULL,
[passport_no] [nchar](25) NULL,
[passport_expir] [date] NULL,
[driv_lic_expir] [date] NULL,
[perm_address] [varchar](250) NULL,
[perm_city] [varchar](50) NULL,
[perm_state] [varchar](50) NULL,
[per_zip] [nchar](20) NULL,
[perm_country] [nvarchar](6) NULL,
[current_address] [varchar](250) NULL,
[current_city] [varchar](50) NULL,
[current_state] [varchar](50) NULL,
[current_zip] [nchar](20) NULL,
[current_country] [nvarchar](6) NULL,
[mobile_no] [nvarchar](20) NULL,
[notes] [varchar](250) NULL,
[added_by] [bigint] NULL,
[added_on] [date] NULL,
[send_cred_by_email] [bit] NOT NULL,
[user_name] [nvarchar](256) NULL,
CONSTRAINT [PK_employees] PRIMARY KEY CLUSTERED
(
[employee_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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[employees] ADD CONSTRAINT [DF_employees_allow_login] DEFAULT ((0)) FOR [allow_login]
GO
ALTER TABLE [dbo].[employees] ADD CONSTRAINT [DF_employees_allow_email] DEFAULT ((0)) FOR [allow_email]
GO
ALTER TABLE [dbo].[employees] ADD DEFAULT ('Mr.') FOR [salutation]
GO
ALTER TABLE [dbo].[employees] ADD DEFAULT ((1)) FOR [send_cred_by_email]
GO
ALTER TABLE [dbo].[employees] WITH CHECK ADD CONSTRAINT [FK_employees_AspNetUsers] FOREIGN KEY([user_id])
REFERENCES [dbo].[AspNetUsers] ([Id])
GO
ALTER TABLE [dbo].[employees] CHECK CONSTRAINT [FK_employees_AspNetUsers]
GO
ALTER TABLE [dbo].[employees] WITH CHECK ADD CONSTRAINT [FK_employees_countries] FOREIGN KEY([perm_country])
REFERENCES [dbo].[countries] ([country_id])
GO
ALTER TABLE [dbo].[employees] CHECK CONSTRAINT [FK_employees_countries]
GO
ALTER TABLE [dbo].[employees] WITH CHECK ADD CONSTRAINT [FK_employees_countries1] FOREIGN KEY([current_country])
REFERENCES [dbo].[countries] ([country_id])
GO
ALTER TABLE [dbo].[employees] CHECK CONSTRAINT [FK_employees_countries1]
GO
ALTER TABLE [dbo].[employees] WITH CHECK ADD CONSTRAINT [FK_employees_employees] FOREIGN KEY([employee_id])
REFERENCES [dbo].[employees] ([employee_id])
GO
ALTER TABLE [dbo].[employees] CHECK CONSTRAINT [FK_employees_employees]
GO
there were some existing employees which are already managers in departments table , now when I try to delete any of those manager employees, I get a foreign key constraint conflict error blocking the delete statement.
what is the problem please ?
The problem is that you're not allowed to do that.
A foreign key constraint is an integrity constraint meaning that it ensures data integrity across tables.
If one department row says that the ID of the employee that is the manager of this department is 73, it would not be good if there is no employee in the database with an ID of 73.
A foreign key constraint ensures this won't happen:
You cannot insert or update a department row to have a manager id that does not exist in the employee table
You cannot delete an employee that is referenced as a manager from a department row
You cannot update the ID of an employee that is referenced as a manager from a department row
The solution is to first fix the department by:
Switching to a different manager by updating the department row and adjusting the ID of the manager
NULL'ing out the manager, saying that this department has no manager, if that is allowed
After doing any of those two actions you should be able to delete the employee, assuming there's no other foreign key references as well.
I have a table with 6000 records but it could be larger in the future. I have attached screen dump of my current SQL query which takes 12 to 14 seconds to load 6000 rows of data.
There are already indexes on the columns which are queried most commonly
How can I improve query performance or modify query for the same?
CREATE TABLE [dbo].[Item]
(
[Srno] [BIGINT] IDENTITY(1, 1) NOT NULL,
[ITEMCode] [BIGINT] NOT NULL,
[BranchId] [BIGINT] NOT NULL,
[ITEM_No] [VARCHAR](50) NULL,
[ITEM_Desc] [VARCHAR](200) NULL,
[DeptId] [BIGINT] NULL,
[CatId] [BIGINT] NULL,
[SizeId] [BIGINT] NULL,
[CostPrice] [DECIMAL](18, 3) NULL,
[SalesPrice] [DECIMAL](18, 2) NULL,
[ITEM_InStock] [BIGINT] NULL,
[UserId] [BIGINT] NULL,
[Active] [BIT] NOT NULL,
[IsDeleted] [BIT] NOT NULL,
[Quantity] [INT] NULL,
[IsFavourite] [BIT] NULL,
[IsProductLink] [BIT] NULL,
CONSTRAINT [PK_Item_1] PRIMARY KEY CLUSTERED ( [ITEMCode] ASC, [BranchId] 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].[Item]
WITH CHECK ADD CONSTRAINT [FK_Item_Item] FOREIGN KEY([ITEMCode], [BranchId]) REFERENCES [dbo].[Item] ([ITEMCode], [BranchId])
GO
ALTER TABLE [dbo].[Item]
CHECK CONSTRAINT [FK_Item_Item]
GO
ALTER TABLE [dbo].[Item]
ADD CONSTRAINT [DF_Item_ITEM_UnitPrice] DEFAULT ((0)) FOR [CostPrice]
GO
ALTER TABLE [dbo].[Item]
ADD CONSTRAINT [DF_Item_ITEM_RETAILPRICE] DEFAULT ((0)) FOR [SalesPrice]
GO
ALTER TABLE [dbo].[Item]
ADD CONSTRAINT [DF_Item_ITEM_InStock] DEFAULT ((0)) FOR [ITEM_InStock]
GO
ALTER TABLE [dbo].[Item]
ADD CONSTRAINT [DF_Item_Active] DEFAULT ((0)) FOR [Active]
GO
ALTER TABLE [dbo].[Item]
ADD CONSTRAINT [DF_Item_DelFlag] DEFAULT ((0)) FOR [IsDeleted]
GO
I have updated query and removed join of Department table but still it takes 10 Seconds
SELECT Item.ITEMCode AS ItemId,
ISNULL(Item.ITEM_No, '0') AS ItemNo,
isnull(Item.Barcode, '') AS Barcode,
Item.ITEM_Desc AS ItemName,
isnull(item_Imagepath, '') AS ItemImage,
0 AS availableQty,
Item.SalesPrice AS Price,
isnull(Item.TaxApply, 0) AS isTax,
Item.TaxType,
ISNULL(Item.ITM_Type, '0') AS ITMType,
Item.Profit_Type AS ProfitType,
Item.Profit_Amt AS ProfitAmt,
Item.CostPrice,
ISNULL(Item.ITEM_Remarks, '') AS Remark
FROM Item
If it takes 10 seconds to read 6000 (small) records then I can only assume/hope that your connection is introducing a lot of delays here!?!
Could you try the following and see how long this takes?
SET STATISTICS TIME ON
SELECT * INTO #test FROM [Item]
If this runs quickly then I'd go look for ways to speed up your connection between your computer and the Azure severs. (network packet size?)