deleting an employee conflicts with department manager foreign key constraint - sql-server

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.

Related

Group by In SQL query is not working well in my query

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

Delete Statement Doesn't Use Index

I've got a table with the following schema...
CREATE TABLE [dbo].[Object1](
[id] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[Column1] [datetime] NOT NULL,
[Column2] [int] NOT NULL,
[Column4] [char](4) NOT NULL,
[Column5] [money] NOT NULL,
[Column6] [bigint] NOT NULL,
[Column7] [char](6) NOT NULL,
[Column3] [bit] NOT NULL,
[Column8] [bigint] NULL,
CONSTRAINT [pk_Object1] PRIMARY KEY NONCLUSTERED
([id] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF
, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON))
GO
ALTER TABLE [dbo].[Object1] ADD CONSTRAINT [DF_Object1_Column1] DEFAULT (getutcdate()) FOR [Column1]
GO
ALTER TABLE [dbo].[Object1] ADD CONSTRAINT [DF_Object1_Column3] DEFAULT ((0)) FOR [Column3]
GO
Each night, a delete query runs against this table...
delete from Object1
where ( Column1 < DATEADD(dd, -40, GETUTCDATE()) )
OR ( Column3 = ? and Column1 < DATEADD(dd, -3, GETUTCDATE()))
The table has
Index2 (id) - clustered, unique located on PRIMARY
Index3 (id) - nonclustered located on PRIMARY
Index1 (id) - nonclustered, unique, primary key located on
PRIMARY
Index4 (Column7) - nonclustered located on PRIMARY
Index5 (Column2, Column7, Column4, Column5) - nonclustered
located on PRIMARY
The query produces the following plan...
Execution Plan for Delete Statement
The statement causes blocking in a highly concurrent table. The blocking ripples into system errors.
If I recreate "Object1_IX" to cover the "Object1" field, this still happens. The table has 6,568,449-rows and trims about 200,000 a night.

Dead lock on table's primary key when running multiple of the same query

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

Cannot insert duplicate key row in object 'dbo.CFD' with unique index

I am running into an error on the insertion of a single row into a table, dbo.CFD. The complete error states:
Cannot insert duplicate key row in object 'dbo.CFD' with unique index 'IX_CFD_NRSC_FXTR_N'. The duplicate key value is (BP2001, 1990, CHK, 01, 3456).
Looking at the index in question, IX_CFD_NRSC_FXTR_N, it is a non-unique, non-clustered index on a single column (The column with BP2001 in the error).
Looking at the data, there are many records with the values of BP2001, CHK, 01, and 3456, however none with 1990 (but various other values), so it doesn't seem I am conflicting with a unique constraint.
I also verified that all foreign key constraints are being met and the primary key constraint is not being violated.
How can I figure out what exactly is causing this issue? Every example I can find results in there being duplicate data being inserted, but I don't think that is the issue here. Thanks!
Edit----------
The table create script:
CREATE TABLE [dbo].[CFD](
[PRPSL_I] [int] NOT NULL,
[FILE_TYPE_C] [char](3) NOT NULL,
[CAD_FLR_C] [char](2) NOT NULL,
[FXTR_SERL_I] [int] NOT NULL,
[ADJC_I] [int] NULL,
[CMP_I] [int] NULL,
[VER_I] [int] NULL,
[NRSC_FXTR_N] [varchar](15) NULL,
[MDSE_SPCE_HT_I] [int] NULL,
[MDSE_SPCE_LGTH_I] [int] NULL,
[MDSE_SPCE_DPT_I] [int] NULL,
[NRSC_FXTR_SIDE_I] [int] NULL,
[NRSC_FXTR_SECT_I] [int] NULL,
[BOFM_INCL_F] [bit] NOT NULL,
[DIVI_I] [int] NULL,
[PRTY_I] [int] NULL,
[REUSE_I] [int] NULL,
[FXTR_ISTL_LOC_N] [varchar](6) NULL,
[PHYS_SECT_NUM_I] [int] NULL,
[SAFL_LOC_N] [varchar](4) NULL,
[SEQ_I] [int] NULL,
[SIDE_ASLE_I] [int] NULL,
[LDIN_I] [int] NULL,
[LDIN_STAT_I] [int] NULL,
CONSTRAINT [PK_CFD] PRIMARY KEY CLUSTERED
(
[PRPSL_I] ASC,
[FILE_TYPE_C] ASC,
[CAD_FLR_C] ASC,
[FXTR_SERL_I] ASC
)WITH (PAD_INDEX = ON, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[CFD] ADD CONSTRAINT [DF_CFD_BOFM_INCL_F] DEFAULT ((0)) FOR [BOFM_INCL_F]
GO
ALTER TABLE [dbo].[CFD] WITH CHECK ADD CONSTRAINT [FK1_CFD] FOREIGN KEY([PRPSL_I], [FILE_TYPE_C], [CAD_FLR_C])
REFERENCES [dbo].[CAD_FILE] ([PRPSL_I], [FILE_TYPE_C], [CAD_FLR_C])
GO
ALTER TABLE [dbo].[CFD] CHECK CONSTRAINT [FK1_CFD]
GO
ALTER TABLE [dbo].[CFD] WITH CHECK ADD CONSTRAINT [FK2_CFD] FOREIGN KEY([CMP_I])
REFERENCES [dbo].[CMP] ([CMP_I])
GO
ALTER TABLE [dbo].[CFD] CHECK CONSTRAINT [FK2_CFD]
GO
ALTER TABLE [dbo].[CFD] WITH CHECK ADD CONSTRAINT [FK3_CFD] FOREIGN KEY([ADJC_I])
REFERENCES [dbo].[ADJC_E] ([ADJC_I])
GO
ALTER TABLE [dbo].[CFD] CHECK CONSTRAINT [FK3_CFD]
GO
ALTER TABLE [dbo].[CFD] WITH CHECK ADD CONSTRAINT [CK_CFD] CHECK (([FILE_TYPE_C]<>'SFP'))
GO
ALTER TABLE [dbo].[CFD] CHECK CONSTRAINT [CK_CFD]
GO
CREATE NONCLUSTERED INDEX [IX_CFD_NRSC_FXTR_N] ON [dbo].[CFD]
(
[NRSC_FXTR_N] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

Improve SQL Server query performance with table having medium data

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?)

Resources