How careful do I need to be using Brent Ozar's free SQL health check scripts on a SQL Server 2016 production machine that's very full and very busy already? Has anyone run into memory, CPU or I/O issues running any of the following? Thanks for any feedback.
sp_Blitz
sp_BlitzCache
sp_BlitzFirst
sp_BlitzIndex
sp_BlitzLock
sp_BlitzQueryStore
sp_BlitzWho
sp_WhoIsActive (Adam Machanic)
I would not be without it. We have Redgate and I use Brents tools more often.
Our database is 3 Tb and has approx. 1500 queries per second and have never experienced any problems.
Made a custom sp_BlitzWho_Ext which relys on the sp_BlitzWho.
It takes the result of sp_BlitzWho. Puts it in a temp table so I can add a filter. Also cursors are identified and converted so you can see the actual query.
The SQL for dropping the plan_cache of each of them are there - so whenever we get a dumb plan its easy to find and drop the right one.
It now only takes 5 seconds to identify the culprit and get the plan dropped.
First I created a Function to get the query text from the Cursor
create function [dbo].[CursorQuery]
(
#session_id int
) returns nvarchar(255)
as
begin
declare #ret nvarchar(255) = null
SELECT #ret = t.text
FROM sys.dm_exec_cursors (#session_id) c
LEFT JOIN sys.dm_exec_sessions AS es ON c.session_id = es.session_id
CROSS APPLY sys.dm_exec_sql_text (c.sql_handle) t
return #ret
end
Then the stored procedure
CREATE PROCEDURE [dbo].[sp_BlitzWho_Ext]
AS
BEGIN
CREATE TABLE #BlitzWhoResult
(
[run_date] VARCHAR(255),
[elapsed_time] [varchar](41) NULL,
[session_id] [smallint] NOT NULL,
[database_name] [nvarchar](128) NULL,
[query_text] [nvarchar](max) NULL,
[query_plan] [xml] NULL,
[live_query_plan] [xml] NULL,
[query_cost] [float] NULL,
[status] [nvarchar](30) NOT NULL,
[wait_info] [nvarchar](max) NULL,
[top_session_waits] [nvarchar](max) NULL,
[blocking_session_id] [smallint] NULL,
[open_transaction_count] [int] NULL,
[is_implicit_transaction] [int] NOT NULL,
[nt_domain] [nvarchar](128) NULL,
[host_name] [nvarchar](128) NULL,
[login_name] [nvarchar](128) NOT NULL,
[nt_user_name] [nvarchar](128) NULL,
[program_name] [nvarchar](128) NULL,
[fix_parameter_sniffing] [nvarchar](150) NULL,
[client_interface_name] [nvarchar](32) NULL,
[login_time] [datetime] NOT NULL,
[start_time] [datetime] NULL,
[request_time] [datetime] NULL,
[request_cpu_time] [int] NULL,
[request_logical_reads] [bigint] NULL,
[request_writes] [bigint] NULL,
[request_physical_reads] [bigint] NULL,
[session_cpu] [int] NOT NULL,
[session_logical_reads] [bigint] NOT NULL,
[session_physical_reads] [bigint] NOT NULL,
[session_writes] [bigint] NOT NULL,
[tempdb_allocations_mb] [decimal](38, 2) NULL,
[memory_usage] [int] NOT NULL,
[estimated_completion_time] [bigint] NULL,
[percent_complete] [real] NULL,
[deadlock_priority] [int] NULL,
[transaction_isolation_level] [varchar](33) NOT NULL,
[degree_of_parallelism] [smallint] NULL,
[last_dop] [bigint] NULL,
[min_dop] [bigint] NULL,
[max_dop] [bigint] NULL,
[last_grant_kb] [bigint] NULL,
[min_grant_kb] [bigint] NULL,
[max_grant_kb] [bigint] NULL,
[last_used_grant_kb] [bigint] NULL,
[min_used_grant_kb] [bigint] NULL,
[max_used_grant_kb] [bigint] NULL,
[last_ideal_grant_kb] [bigint] NULL,
[min_ideal_grant_kb] [bigint] NULL,
[max_ideal_grant_kb] [bigint] NULL,
[last_reserved_threads] [bigint] NULL,
[min_reserved_threads] [bigint] NULL,
[max_reserved_threads] [bigint] NULL,
[last_used_threads] [bigint] NULL,
[min_used_threads] [bigint] NULL,
[max_used_threads] [bigint] NULL,
[grant_time] [varchar](20) NULL,
[requested_memory_kb] [bigint] NULL,
[grant_memory_kb] [bigint] NULL,
[is_request_granted] [varchar](39) NOT NULL,
[required_memory_kb] [bigint] NULL,
[query_memory_grant_used_memory_kb] [bigint] NULL,
[ideal_memory_kb] [bigint] NULL,
[is_small] [bit] NULL,
[timeout_sec] [int] NULL,
[resource_semaphore_id] [smallint] NULL,
[wait_order] [varchar](20) NULL,
[wait_time_ms] [varchar](20) NULL,
[next_candidate_for_memory_grant] [varchar](3) NOT NULL,
[target_memory_kb] [bigint] NULL,
[max_target_memory_kb] [varchar](30) NULL,
[total_memory_kb] [bigint] NULL,
[available_memory_kb] [bigint] NULL,
[granted_memory_kb] [bigint] NULL,
[query_resource_semaphore_used_memory_kb] [bigint] NULL,
[grantee_count] [int] NULL,
[waiter_count] [int] NULL,
[timeout_error_count] [bigint] NULL,
[forced_grant_count] [varchar](30) NULL,
[workload_group_name] [sysname] NULL,
[resource_pool_name] [sysname] NULL,
[context_info] [varchar](128) NULL,
[query_hash] [binary](8) NULL,
[query_plan_hash] [binary](8) NULL,
[sql_handle] [varbinary] (64) NULL,
[plan_handle] [varbinary] (64) NULL,
[statement_start_offset] INT NULL,
[statement_end_offset] INT NULL
)
INSERT INTO #BlitzWhoResult
EXEC [dbo].[sp_BlitzWho] #ShowSleepingSPIDs = 1, #ExpertMode = 1, #MinElapsedSeconds = 1
SELECT [program_name],
[session_id],
CASE WHEN [query_text] LIKE 'FETCH API_CURSOR%'
THEN CursorQuery(session_id)
ELSE query_text
END AS [query_text],
[query_plan],
[fix_parameter_sniffing] AS [drop_cached_plan_sql],
[status],
[wait_info],
[login_name]
from #BlitzWhoResult
where database_name = 'PUT_YOUR_DATABASENAME_HERE' -- and... add more filters?
DROP TABLE #BlitzWhoResult
END
So.. I really enjoy Brents tool as well as his videos and sense of humour
Related
I made a copy of a table with the same indexes in the same database of the same server.
Then it executes the same query in one table and in the other but the execution plan is different.
Why?
These are my queries:
select top 1 * from SFMatl
where AppUpdated = 0 and UpdLock = 0 and CompanyId = 'ent'
order by recid
select top 1 * from SFMatl_Backup20042017
where AppUpdated = 0 and UpdLock = 0 and CompanyId = 'ent'
order by recid
And these execution plans:
Query1
Query2
My create tables:
/****** Object: Table [dbo].[SFMatl_Backup20042017] Script Date: 20/04/2017 14:40:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SFMatl_Backup20042017](
[RecId] [int] NOT NULL,
[CompanyId] [char](3) NOT NULL,
[PrOdId] [varchar](10) NOT NULL,
[OprNum] [tinyint] NOT NULL,
[WrkCtrId] [varchar](10) NOT NULL,
[ItemId] [varchar](20) NOT NULL,
[SizeId] [varchar](20) NOT NULL,
[SizeId2] [varchar](20) NOT NULL,
[ColorId] [varchar](20) NOT NULL,
[MatlPropId] [varchar](8) NOT NULL,
[LineNum] [tinyint] NOT NULL,
[SubForLineNum] [tinyint] NOT NULL,
[SerialNum] [varchar](20) NOT NULL,
[PalletNum] [varchar](20) NOT NULL,
[BatchNum] [varchar](20) NOT NULL,
[PQty] [numeric](28, 12) NOT NULL,
[PUnit] [varchar](10) NOT NULL,
[SQty] [numeric](28, 12) NOT NULL,
[SUnit] [varchar](10) NOT NULL,
[TQty] [numeric](28, 12) NOT NULL,
[TUnit] [varchar](10) NOT NULL,
[StkQty] [numeric](28, 12) NOT NULL,
[StkUnit] [varchar](10) NOT NULL,
[FtQty] [numeric](28, 12) NOT NULL,
[LbQty] [numeric](28, 12) NOT NULL,
[MQty] [numeric](28, 12) NOT NULL,
[KgQty] [numeric](28, 12) NOT NULL,
[Yield] [numeric](28, 12) NOT NULL,
[YieldUnit] [varchar](10) NOT NULL,
[WPQty] [numeric](28, 12) NOT NULL,
[WPUnit] [varchar](10) NOT NULL,
[WSQty] [numeric](28, 12) NOT NULL,
[WSUnit] [varchar](10) NOT NULL,
[SetNum] [int] NOT NULL,
[LaneNum] [tinyint] NOT NULL,
[RowNum] [smallint] NOT NULL,
[TranType] [varchar](10) NOT NULL,
[WasteId] [varchar](10) NOT NULL,
[Shift] [varchar](10) NOT NULL,
[ShiftTime] [datetime] NOT NULL,
[ShiftDate] [datetime] NOT NULL,
[PRGCode] [varchar](10) NOT NULL,
[UserRemark] [text] NOT NULL,
[JobType] [varchar](10) NOT NULL,
[WareHouseId] [varchar](10) NOT NULL,
[LocationId] [varchar](10) NOT NULL,
[AppUpdated] [tinyint] NOT NULL,
[OnOffUpdated] [tinyint] NOT NULL,
[InventTransId] [varchar](20) NOT NULL,
[OrgSerialNum] [varchar](100) NOT NULL,
[ReworkFlag] [tinyint] NOT NULL,
[Reworked] [tinyint] NOT NULL,
[Category] [varchar](50) NOT NULL,
[GroupTranNum] [varchar](10) NOT NULL,
[SystemCreated] [tinyint] NOT NULL,
[OfflineTran] [tinyint] NOT NULL,
[UpdLock] [tinyint] NOT NULL,
[UpdLockTime] [datetime] NOT NULL,
[SFBOMRevision] [int] NOT NULL,
[UDF01] [varchar](50) NOT NULL,
[UDF02] [varchar](50) NOT NULL,
[AddEdit] [tinyint] NOT NULL,
[CalcGauge] [numeric](28, 12) NOT NULL,
[MasterSerial] [varchar](100) NOT NULL,
[OprSerialCount] [smallint] NOT NULL,
[QACheck] [tinyint] NOT NULL,
[SpliceCount] [tinyint] NOT NULL,
[NumAcross] [tinyint] NOT NULL,
[PermitNum] [int] NOT NULL,
[TransAs] [char](1) NOT NULL,
[CreateTime] [datetime] NOT NULL,
[ModifyTime] [datetime] NOT NULL,
[OSUser] [varchar](20) NOT NULL,
[ComputerName] [varchar](40) NOT NULL,
[Reassign] [tinyint] NOT NULL,
[Reassigned] [tinyint] NOT NULL,
[OprCode] [varchar](10) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: Table [dbo].[SFMatl] Script Date: 20/04/2017 14:40:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SFMatl](
[RecId] [int] IDENTITY(1,1) NOT NULL,
[CompanyId] [char](3) NOT NULL,
[PrOdId] [varchar](10) NOT NULL,
[OprNum] [tinyint] NOT NULL,
[WrkCtrId] [varchar](10) NOT NULL,
[ItemId] [varchar](20) NOT NULL,
[SizeId] [varchar](20) NOT NULL,
[SizeId2] [varchar](20) NOT NULL,
[ColorId] [varchar](20) NOT NULL,
[MatlPropId] [varchar](8) NOT NULL,
[LineNum] [tinyint] NOT NULL,
[SubForLineNum] [tinyint] NOT NULL,
[SerialNum] [varchar](20) NOT NULL,
[PalletNum] [varchar](20) NOT NULL,
[BatchNum] [varchar](20) NOT NULL,
[PQty] [numeric](28, 12) NOT NULL,
[PUnit] [varchar](10) NOT NULL,
[SQty] [numeric](28, 12) NOT NULL,
[SUnit] [varchar](10) NOT NULL,
[TQty] [numeric](28, 12) NOT NULL,
[TUnit] [varchar](10) NOT NULL,
[StkQty] [numeric](28, 12) NOT NULL,
[StkUnit] [varchar](10) NOT NULL,
[FtQty] [numeric](28, 12) NOT NULL,
[LbQty] [numeric](28, 12) NOT NULL,
[MQty] [numeric](28, 12) NOT NULL,
[KgQty] [numeric](28, 12) NOT NULL,
[Yield] [numeric](28, 12) NOT NULL,
[YieldUnit] [varchar](10) NOT NULL,
[WPQty] [numeric](28, 12) NOT NULL,
[WPUnit] [varchar](10) NOT NULL,
[WSQty] [numeric](28, 12) NOT NULL,
[WSUnit] [varchar](10) NOT NULL,
[SetNum] [int] NOT NULL,
[LaneNum] [tinyint] NOT NULL,
[RowNum] [smallint] NOT NULL,
[TranType] [varchar](10) NOT NULL,
[WasteId] [varchar](10) NOT NULL,
[Shift] [varchar](10) NOT NULL,
[ShiftTime] [datetime] NOT NULL,
[ShiftDate] [datetime] NOT NULL,
[PRGCode] [varchar](10) NOT NULL,
[UserRemark] [text] NOT NULL,
[JobType] [varchar](10) NOT NULL,
[WareHouseId] [varchar](10) NOT NULL,
[LocationId] [varchar](10) NOT NULL,
[AppUpdated] [tinyint] NOT NULL,
[OnOffUpdated] [tinyint] NOT NULL,
[InventTransId] [varchar](20) NOT NULL,
[OrgSerialNum] [varchar](100) NOT NULL,
[ReworkFlag] [tinyint] NOT NULL,
[Reworked] [tinyint] NOT NULL,
[Category] [varchar](50) NOT NULL,
[GroupTranNum] [varchar](10) NOT NULL,
[SystemCreated] [tinyint] NOT NULL,
[OfflineTran] [tinyint] NOT NULL,
[UpdLock] [tinyint] NOT NULL,
[UpdLockTime] [datetime] NOT NULL,
[SFBOMRevision] [int] NOT NULL,
[UDF01] [varchar](50) NOT NULL,
[UDF02] [varchar](50) NOT NULL,
[AddEdit] [tinyint] NOT NULL,
[CalcGauge] [numeric](28, 12) NOT NULL,
[MasterSerial] [varchar](100) NOT NULL,
[OprSerialCount] [smallint] NOT NULL,
[QACheck] [tinyint] NOT NULL,
[SpliceCount] [tinyint] NOT NULL,
[NumAcross] [tinyint] NOT NULL,
[PermitNum] [int] NOT NULL,
[TransAs] [char](1) NOT NULL,
[CreateTime] [datetime] NOT NULL,
[ModifyTime] [datetime] NOT NULL,
[OSUser] [varchar](20) NOT NULL,
[ComputerName] [varchar](40) NOT NULL,
[Reassign] [tinyint] NOT NULL,
[Reassigned] [tinyint] NOT NULL,
[OprCode] [varchar](10) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
The main difference with those queries is they are not the same.
The table SELECTed is not the same.
This means two things in term of Query Plan.
Different statistics due to different amount of datas for example.
Different indexes or index fragmentation.
You can check indexes and their fragmentation (and maybe rebuild/reorganize them if required), you can also recalculate the statistics.
If it still keeps the same query plans,
then the difference is mainly due to data contained in the tables.
one of the main parameter ,sql server uses while creating a plan is statistics.So even if the data is same in both the tables you have ,statistics will be different,which leads to different execution plans
To view statistics for the tables ,you can use below
dbcc show_statistics('SFMatl','AppUpdated')
dbcc show_statistics('SFMatl','SFMatl_Backup20042017')
If you now,how to infer the above results, you will be able to see ,statistics are different for the above two tables ,which further decides plan quality
I have an application which has a listing page with search functions which lists the records that match the query. This application is working perfectly well with MySQL. But with same data on a Windows server and MS SQL. It is giving the following error (the query is included in the error).
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion
failed when converting the nvarchar value 'contract_i' to data type
int.
SELECT ctbl_contract_funding_info.*,
ctbl_contract_funding_mgmt.funding_manager,
ctbl_contract_workflow.id AS id1,
ctbl_contract_workflow.family_id AS main_id,
ctbl_contract_workflow.parent_type
FROM ctbl_contract_workflow
LEFT JOIN ctbl_contract_funding_info
ON ctbl_contract_funding_info.contract_id =
ctbl_contract_workflow.id
LEFT JOIN ctbl_contract_funding_mgmt
ON ctbl_contract_funding_mgmt.contract_id =
ctbl_contract_workflow.id
LEFT JOIN ctbl_contract_funding_fin
ON ctbl_contract_funding_fin.contract_id =
ctbl_contract_funding_mgmt.contract_id
ORDER BY ctbl_contract_workflow.id ASC
The table definitions are as below
/***** Object: Table [dbo].[ctbl_contract_compliance_mgmt] Script Date: 01/03/2017 14:58:54 *****/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ctbl_contract_compliance_mgmt](
[id] [int] IDENTITY(1,1) NOT NULL,
[save_flag] [nvarchar](255) NULL,
[contract_id] [nvarchar](10) NULL,
[opening_meeting] [nvarchar](255) NULL,
[closing_meeting] [nvarchar](255) NULL,
[Procurement_rules] [nvarchar](max) NULL,
[budget_variance] [nvarchar](max) NULL,
[audit] [nvarchar](max) NULL,
[end_use_equipment] [nvarchar](max) NULL,
[bank_interest] [nvarchar](max) NULL,
[visibility_requiremnt] [nvarchar](max) NULL,
[special_requiremet] [nvarchar](max) NULL,
[donor_guide] [nvarchar](max) NULL,
[donor_guide_text] [nvarchar](max) NULL,
[special_requiremet_text] [nvarchar](max) NULL,
[visibility_requiremnt_text] [nvarchar](max) NULL,
[type_topic] [nvarchar](max) NULL,
[type_scope] [nvarchar](max) NULL,
[type_incident] [nvarchar](max) NULL,
[identified_through] [nvarchar](max) NULL,
[date_initital_report] [nvarchar](255) NULL,
[incident_status] [nvarchar](max) NULL,
[createdby] [int] NULL,
[createddate] [datetime] NULL,
[flag] [int] NULL,
[updatedby] [int] NULL,
[updatedtime] [datetime] NULL,
[retention_period] [nvarchar](max) NULL,
[sub-awarding-permitted] [nvarchar](max) NULL,
[sub_awarding_permitted] [nvarchar](255) NULL,
[reporting_language] [nvarchar](255) NULL,
[currency_type] [nvarchar](255) NULL,
[comments_procurement_rules] [nvarchar](255) NULL,
[comments_budget_variance] [nvarchar](255) NULL,
[comments_audit] [nvarchar](255) NULL,
[comments_end_use_equipment] [nvarchar](255) NULL,
[comments_bank_interest] [nvarchar](255) NULL,
[comments_sub_awarding_permitted] [nvarchar](255) NULL,
[comments_visibility_requiremnt] [nvarchar](255) NULL,
[comments_retention_period] [nvarchar](255) NULL,
[comments_special_requiremet] [nvarchar](255) NULL,
[comments_donor_guide] [nvarchar](255) NULL,
[special_provisions] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[ctbl_contract_compliance_mgmt] ADD CONSTRAINT [DF_ctbl_contract_compliance_mgmt_flag] DEFAULT ((0)) FOR [flag]
GO
***** Object: Table [dbo].[ctbl_contract_funding_fin] Script Date: 01/03/2017 14:59:28 *****/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ctbl_contract_funding_fin](
[id] [int] IDENTITY(1,1) NOT NULL,
[save_flag] [nvarchar](255) NULL,
[contract_id] [nvarchar](10) NULL,
[funding_mechanism] [nvarchar](255) NULL,
[expense_recovery] [nvarchar](255) NULL,
[type_in_kind_donation] [nvarchar](255) NULL,
[type_financing_mechanis] [nvarchar](255) NULL,
[funding_currency] [nvarchar](255) NULL,
[funding_ceiling_currency] [nvarchar](255) NULL,
[funding_ceiling_gbp] [nvarchar](255) NULL,
[total_budget_currency] [nvarchar](255) NULL,
[total_budget_gbp] [nvarchar](255) NULL,
[total_budget_exchange_rate_date] [nvarchar](max) NULL,
[total_budget_exchange_rate] [nvarchar](255) NULL,
[modified_total_budget_currency] [nvarchar](255) NULL,
[modified_total_budget_gbp] [nvarchar](255) NULL,
[modified_budget_exchange_rate] [nvarchar](255) NULL,
[modified_total_budget] [nvarchar](255) NULL,
[obligated_amount] [nvarchar](max) NULL,
[total_obligated_amount_currency] [nvarchar](255) NULL,
[total_obligated_amount_gbp] [nvarchar](255) NULL,
[end_date_finance_period] [nvarchar](255) NULL,
[desc_partial_obligated_amount] [nvarchar](255) NULL,
[period_financed_partial_obligated_months] [nvarchar](255) NULL,
[partial_obligated_amount_currency] [nvarchar](255) NULL,
[expenditure_latest_accounts_currency] [nvarchar](255) NULL,
[expenditure_latest_closed_accounts_gbp] [nvarchar](255) NULL,
[balance_latest_closed_accounts_gbp] [nvarchar](255) NULL,
[balance_latest_closed_accounts_currency] [nvarchar](255) NULL,
[payment_mechanism] [nvarchar](255) NULL,
[periodicity] [nvarchar](255) NULL,
[payment_requested] [nvarchar](255) NULL,
[payment_requested_date] [datetime] NULL,
[payment_received_date] [datetime] NULL,
[payment_requested_amount_currency] [nvarchar](255) NULL,
[payment_received_amount_currency] [nvarchar](255) NULL,
[recovery_cost] [nvarchar](255) NULL,
[cost_recovery_cost_currency] [nvarchar](255) NULL,
[cost_recovery_cost_gbp] [nvarchar](255) NULL,
[direct_charge] [nvarchar](255) NULL,
[percentage_total_budget] [nvarchar](255) NULL,
[indirect_cost] [nvarchar](255) NULL,
[percentage_direct_cost_currency] [nvarchar](255) NULL,
[waiver_approver_position] [nvarchar](max) NULL,
[waiver_approver] [nvarchar](max) NULL,
[co_financing_requirements] [nvarchar](max) NULL,
[co_financing_amount] [nvarchar](255) NULL,
[co_financing_total_budget] [nvarchar](255) NULL,
[co_financing_conditions] [nvarchar](max) NULL,
[createdby] [int] NULL,
[createddate] [datetime] NULL,
[updatedby] [int] NULL,
[updatedtime] [datetime] NULL,
[flag] [int] NULL,
[location] [nvarchar](255) NULL,
[financed_period_date] [nvarchar](255) NULL,
[type_financing_mechanis_others] [nvarchar](max) NULL,
[periodicity_comment] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
/***** Object: Table [dbo].[ctbl_contract_funding_mgmt] Script Date: 01/03/2017 15:00:40 *****/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ctbl_contract_funding_mgmt](
[id] [int] IDENTITY(1,1) NOT NULL,
[save_flag] [nvarchar](255) NULL,
[contract_id] [nvarchar](10) NULL,
[funding_manager] [nvarchar](max) NULL,
[programme_name] [nvarchar](max) NULL,
[programme_position] [nvarchar](max) NULL,
[programme_responsibility] [nvarchar](max) NULL,
[operations_name] [nvarchar](max) NULL,
[operations_position] [nvarchar](max) NULL,
[operations_responsibility] [nvarchar](max) NULL,
[international_operations_auth_app_name] [nvarchar](max) NULL,
[international_operations_auth_app_position] [nvarchar](max) NULL,
[international_operations_auth_app_responsibility] [nvarchar](max) NULL,
[project_accountant_name] [nvarchar](max) NULL,
[project_accountant_position] [nvarchar](max) NULL,
[project_accountant_responsibility] [nvarchar](max) NULL,
[donor_compliance_name] [nvarchar](max) NULL,
[donor_compliance_position] [nvarchar](max) NULL,
[donor_compliance_responsibility] [nvarchar](max) NULL,
[donor_lead_name] [nvarchar](max) NULL,
[donor_lead_position] [nvarchar](max) NULL,
[donor_lead_responsibility] [nvarchar](max) NULL,
[focal_point_name] [nvarchar](max) NULL,
[focal_point_position] [nvarchar](max) NULL,
[focal_point_responsibility] [nvarchar](max) NULL,
[funding_officer] [nvarchar](max) NULL,
[donor_phone] [nvarchar](max) NULL,
[donor_email] [nvarchar](max) NULL,
[donor_address] [nvarchar](max) NULL,
[external_funding_contact] [nvarchar](max) NULL,
[external_funding_phone] [nvarchar](max) NULL,
[external_funding_email] [nvarchar](max) NULL,
[external_funding_address] [nvarchar](max) NULL,
[createdby] [int] NULL,
[createddate] [datetime] NULL,
[flag] [int] NULL,
[updatedby] [int] NULL,
[updatedtime] [datetime] NULL,
[sub-awarding-permitted] [nvarchar](255) NULL,
[workflow_programme_name] [nvarchar](255) NULL,
[workflow_programme_position] [nvarchar](255) NULL,
[workflow_operations_name] [nvarchar](255) NULL,
[workflow_operations_position] [nvarchar](255) NULL,
[international_workflow_operations_auth_app_name] [nvarchar](255) NULL,
[international_workflow_operations_auth_app_position] [nvarchar](255) NULL,
[workflow_project_accountant_name] [nvarchar](255) NULL,
[workflow_project_accountant_position] [nvarchar](255) NULL,
[workflow_donor_compliance_name] [nvarchar](255) NULL,
[workflow_donor_compliance_position] [nvarchar](255) NULL,
[workflow_donor_compliance_name_extra] [nvarchar](max) NULL,
[workflow_donor_compliance_position_extra] [nvarchar](max) NULL,
[workflow_signatory_name] [nvarchar](max) NULL,
[workflow_signatory_position] [nvarchar](max) NULL,
[workflow_msi_support_office_name] [nvarchar](max) NULL,
[workflow_msi_support_office_position] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[ctbl_contract_funding_mgmt] ADD CONSTRAINT [DF_ctbl_contract_funding_mgmt_flag] DEFAULT ((0)) FOR [flag]
GO
/***** Object: Table [dbo].[ctbl_contract_workflow] Script Date: 01/03/2017 15:01:11 *****/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ctbl_contract_workflow](
[id] [int] IDENTITY(1,1) NOT NULL,
[record_id] [int] NULL,
[save_flag] [nvarchar](255) NULL,
[entry_id] [nvarchar](5) NULL,
[fundingstatus] [nvarchar](max) NULL,
[project_code] [nvarchar](max) NULL,
[fundingHierarchy] [nvarchar](max) NULL,
[ContractStatus] [int] NULL,
[OperationResponsible] [nvarchar](max) NULL,
[createdby] [int] NULL,
[createddate] [datetime] NULL,
[updatedby] [int] NULL,
[updatedtime] [datetime] NULL,
[flag] [int] NULL,
[parent_id] [int] NULL,
[parent_type] [nvarchar](50) NULL,
[family_id] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Just using my psychic abilities, one of the columns called contract_id is defined as nvarchar(10) and contains the value 'contract_i', because someone accidentally coded inserted/updated a row with the literal "contract_id" when they intended to update/insert with a variable called contract_id (eg may have left off the leading $ when using PHP etc).
MySQL "converts" (coerces) text to numeric by converting all the leading numeric characters (if any) to a number, discarding the rest, eg
123abc -> 123
12ab34 -> 12
abc -> 0
So MySQL converted 'contract_i' to 0, of course not matching anything, but not giving an error either.
How can I alter a table's primary identity column to include a letter before the number? I have a table named vendor_master with a primary key VendorID that I would like to store as v1,v2,v3 and so on
CREATE TABLE [dbo].[vendor_master](
[vendorID] [int] IDENTITY(1,1) NOT NULL,
[Vname] [nvarchar](50) NOT NULL,
[Email] [nvarchar](50) NOT NULL,
[Mobile] [bigint] NULL,
[Landline] [bigint] NULL,
[Address] [nvarchar](max) NOT NULL,
[Pincode] [int] NOT NULL)
If it's important to you to have the field in your table, try the following:
CREATE TABLE [dbo].[vendor_master]
(
[vendorID] [int] IDENTITY(1,1) NOT NULL,
[Vname] [nvarchar](50) NOT NULL,
[Email] [nvarchar](50) NOT NULL,
[Mobile] [bigint] NULL,
[Landline] [bigint] NULL,
[Address] [nvarchar](max) NOT NULL,
[Pincode] [int] NOT NULL,
[VendorKey] AS ('v' +CONVERT([varchar](10),[vendorID])) PERSISTED
)
Of course, that will yield you some weird results when sorting, so you might consider padding the data with zeroes to make it a consistent length:
CREATE TABLE [dbo].[vendor_master]
(
[vendorID] [int] IDENTITY(1,1) NOT NULL,
[Vname] [nvarchar](50) NOT NULL,
[Email] [nvarchar](50) NOT NULL,
[Mobile] [bigint] NULL,
[Landline] [bigint] NULL,
[Address] [nvarchar](max) NOT NULL,
[Pincode] [int] NOT NULL,
[VendorKey] AS ('v' +RIGHT('000000000' + CONVERT([varchar](10),[vendorID]),10)) PERSISTED
)
You can't actually do what you are asking. Anyway, there is no reason to add the same letter to the front of a fields value. Instead add the letter in your SELECT statment:
SELECT CONCAT('v', vendorID) AS VendorIDWithPrefix, Vname, Email, Mobile
FROM vendor_master
I have a query in db A which shows the index fragmentation level and recent rebuilt or re-organised status, it cross apply a function B in db C, A and C are on the same instance.
Function B is:
ALTER function [dbo].[B]
(
#db_id int
,#object_id int
,#index_id int
,#partition_number int
,#mode varchar (20)
)
returns #results TABLE (
[database_id] [smallint] NULL,
[object_id] [int] NULL,
[index_id] [int] NULL,
[partition_number] [int] NULL,
[index_type_desc] [nvarchar](60) NULL,
[alloc_unit_type_desc] [nvarchar](60) NULL,
[index_depth] [tinyint] NULL,
[index_level] [tinyint] NULL,
[avg_fragmentation_in_percent] [float] NULL,
[fragment_count] [bigint] NULL,
[avg_fragment_size_in_pages] [float] NULL,
[page_count] [bigint] NULL,
[avg_page_space_used_in_percent] [float] NULL,
[record_count] [bigint] NULL,
[ghost_record_count] [bigint] NULL,
[version_ghost_record_count] [bigint] NULL,
[min_record_size_in_bytes] [int] NULL,
[max_record_size_in_bytes] [int] NULL,
[avg_record_size_in_bytes] [float] NULL,
[forwarded_record_count] [bigint] NULL,
[compressed_page_count] [bigint] NULL
)
begin
insert into #results
select *
from sys.dm_db_index_physical_stats (#db_id, #object_id, #index_id ,#partition_number
,#mode )
return
end
This query takes a long time to execute. I read the query plan, the plan contains a "Remote Scan". I don't understand why is that.
Can anyone help?
Thanks,
I have this big database table that tracks (currently in production), a persons medical certification by: Date expires (exp), the company, if we have it on file and the location of a digital copy. We track in many (growing) categories such as CPR, CPR child, AED, Lifeguarding etc...
How do I make this easier to manage and how can I migrate existing data over? I'm coldfusion web app running SQL server 2008.
CREATE TABLE [dbo].[mod_StudentCertifications](
[certificationID] [int] IDENTITY(1,1) NOT NULL,
[profileID] [int] NOT NULL,
[cprAdultExp] [datetime] NULL,
[cprAdultcompany] [nvarchar](250) NULL,
[cprAdultImage] [nvarchar](4000) NULL,
[cprAdultOnFile] [bit] NULL,
[cprInfantChildExp] [datetime] NULL,
[cprInfantChildcompany] [nvarchar](250) NULL,
[cprInfantChildImage] [nvarchar](4000) NULL,
[cprInfantChildOnFile] [bit] NULL,
[cprFPRExp] [datetime] NULL,
[cprFPRcompany] [nvarchar](250) NULL,
[cprFPRImage] [nvarchar](4000) NULL,
[cprFPROnFile] [bit] NULL,
[aedExp] [datetime] NULL,
[aedcompany] [nvarchar](250) NULL,
[aedImage] [nvarchar](4000) NULL,
[aedOnFile] [bit] NULL,
[firstAidExp] [datetime] NULL,
[firstAidcompany] [nvarchar](250) NULL,
[firstAidImage] [nvarchar](4000) NULL,
[firstAidOnFile] [bit] NULL,
[emtExp] [datetime] NULL,
[emtcompany] [nvarchar](250) NULL,
[emtImage] [nvarchar](4000) NULL,
[emtOnFile] [bit] NULL,
[waterSafetyInstructionExp] [datetime] NULL,
[waterSafetyInstructioncompany] [nvarchar](250) NULL,
[waterSafetyInstructionImage] [nvarchar](4000) NULL,
[waterSafetyInstructionOnFile] [bit] NULL,
[bloodPathogensExp] [datetime] NULL,
[bloodPathogenscompany] [nvarchar](250) NULL,
[bloodPathogensImage] [nvarchar](4000) NULL,
[bloodPathogensOnFile] [bit] NULL,
[oxygenAdminExp] [datetime] NULL,
[oxygenAdmincompany] [nvarchar](250) NULL,
[oxygenAdminImage] [nvarchar](4000) NULL,
[oxygenAdminOnFile] [bit] NULL,
[lifegaurdingExp] [datetime] NULL,
[lifegaurdingcompany] [nvarchar](250) NULL,
[lifegaurdingImage] [nvarchar](4000) NULL,
[lifegaurdingOnFile] [bit] NULL,
[wildernessResponderExp] [datetime] NULL,
[wildernessResponderCompany] [nvarchar](250) NULL,
[wildernessResponderImage] [nvarchar](4000) NULL,
[wildernessResponderOnFile] [bit] NULL,
[certNotes] [nvarchar](4000) NULL,
[isActive] [bit] NULL,
[certClassRegistered] [bit] NULL,
[lifeguardInstrcutorExp] [datetime] NULL,
[lifeguardInstrcutorCompany] [nvarchar](250) NULL,
[lifeguardInstrcutorImage] [nvarchar](4000) NULL,
[lifeguardInstrcutorOnFile] [bit] NULL
Notice how all of your certifications have the same repeating columns of information: Expiration, Company, Image, OnFile? That's a big clue that you're in need of further normalization of your design.
In a perfect world (one where you're able to make schema changes), I'd create a generic student/certification table, with those common elements as columns, a foreign key to another table that enumerates the certificates (Water Safety, Wilderness, etc.) and another foreign key linking the students to those certifications. Something like: