Related
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
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.
I have a new table I created for a membership application for a local club, anyway there existing software stored info in a DBF file I can import this into mssql as a table that's no trouble I am just stuck on merging the old data into the new table.
This is the old Table Structure (From DBF imported into mssql)
[NUMBER] [int] NULL,
[LASTNAME] [char](30) NULL,
[FIRSTNAME] [char](20) NULL,
[TITLE] [char](5) NULL,
[SALUTATION] [char](25) NULL,
[ADD1] [char](30) NULL,
[ADD2] [char](30) NULL,
[ADD3] [char](30) NULL,
[TOWN] [char](30) NULL,
[COUNTY] [char](30) NULL,
[POSTCODE] [char](8) NULL,
[TELEPHONE] [char](30) NULL,
[TYPE] [char](1) NULL,
[PAID] [char](1) NULL,
[COMMITTEE] [char](30) NULL,
[WARNINGS] [int] NULL,
[MONTHDRAW] [int] NULL,
[WOMANS] [char](1) NULL,
[SENIORCIT] [char](1) NULL,
[DOB] [datetime] NULL,
[AMOUNTCLUB] [decimal](6, 2) NULL,
[DATEPAY] [datetime] NULL,
[AMOUNTBRAN] [decimal](6, 2) NULL,
[AMOUNTDUE] [decimal](6, 2) NULL
This is the new Table:
[MemberID] [nvarchar](10) NOT NULL,
[Title] [nvarchar](10) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[Surname] [nvarchar](50) NOT NULL,
[HouseNumber] [nvarchar](25) NULL,
[Street] [nvarchar](25) NULL,
[City] [nvarchar](25) NULL,
[County] [nvarchar](25) NULL,
[Postcode] [nvarchar](10) NULL,
[TelephoneNumber] [nvarchar](15) NULL,
[MobileNumber] [nvarchar](15) NULL,
[EmailAddress] [nvarchar](50) NULL,
[DOB] [date] NULL,
[Age] [nvarchar](10) NULL,
[SeniorCitizen] [nvarchar](5) NULL,
[Warnings] [nvarchar](50) NULL,
[MembershipCategory] [nvarchar](20) NULL,
[ServiceBranch] [nvarchar](20) NULL,
[Paid] [nvarchar](10) NULL,
[ToPay] [float] NULL,
[DatePaidIn] [date] NULL,
[Entry] [tinyint] NULL,
[FOB] [nvarchar](10) NULL,
[JoinDate] [date] NULL,
[SubscriptionYear] [date] NULL,
[TotalPaid] [float] NULL,
I am writing the insert like;
INSERT INTO [dbo].[MemberDetails]
([MemberID],[Title],[FirstName],[Surname],[HouseNumber],[Street],[City],[County],[Postcode],[TelephoneNumber],[MobileNumber],[EmailAddress],[DOB],[Age],[SeniorCitizen],[Warnings],[MembershipCategory],[ServiceBranch],[Paid],[ToPay],[DatePaidIn],[Entry],[FOB],[JoinDate],[SubscriptionYear],[TotalPaid])
SELECT [NUMBER], [TITLE], [FIRSTNAME], [LASTNAME], [ADD1], [ADD2], [TOWN], [COUNTY], [POSTCODE], [TELEPHONE]
FROM [dbo].[MEMBERS]
GO
I got as far as that column then realized "how do I do empty values as no one in the old table had a mobile record?" also do I need to do Select AS "new column name" for the old table values?
Suppose you go from OldTable to NewTable, adding an answer column to the original question column:
insert NewTable
(question, answer)
select question
, '42' -- Specify a literal or default value
from OldTable
You can also omit the new column from the insert list:
insert NewTable
(question)
select question
from OldTable
This will copy the question column and put the default value null in the answer column.
You do not have to use aliases in the select list. It's strictly order based. This would work fine:
insert NewTable
(question)
select question as CeciNEstPasUnePipe
from OldTable
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 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: