I have created accouting database as per my accounting knowledge.
I want to check Is that database is correct which i design for accounting system.
I have table called Mst_AccountGroup(Asset,Liabilities,Capital/Equity,Income & Expense data to create chart of account).
The database structure give below.
CREATE TABLE [dbo].[Mst_AccountGroup](
[AccountGrp_No] [int] NULL,
[AccountGrp_Name] [nvarchar](50) NULL,
[AccountGrp_Type] [nvarchar](50) NULL,
[AccountGrp_Increase] [nvarchar](50) NULL,
[AccountGrp_Decrease] [nvarchar](50) NULL,
[AccountGrp_Sign] [nvarchar](50) NULL,
[AccountGrp_SalesSign] [nvarchar](50) NULL,
[AccountGrp_ReceiptSign] [nvarchar](50) NULL,
[AccountGrp_PurchaseSign] [nvarchar](50) NULL,
[AccountGrp_PaymentSign] [nvarchar](50) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Mst_AccountCategory](
[AccountCat_No] [int] NULL,
[AccountCat_Name] [nvarchar](500) NULL,
[AccountCat_AccountGrpNo] [int] NULL,
[AccountCat_Sign] [nvarchar](50) NULL,
[AccountCat_SalesSign] [nvarchar](50) NULL,
[AccountCat_ReceiptSign] [nvarchar](50) NULL,
[AccountCat_PurchaseSign] [nvarchar](50) NULL,
[AccountCat_PaymentSign] [nvarchar](50) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Mst_AccountSubCategory](
[AccountSubCat_No] [int] NULL,
[AccountSubCat_AccountCatNo] [int] NULL,
[AccountSubCat_Name] [nvarchar](500) NULL,
[AccountSubCat_SalesSign] [nvarchar](50) NULL,
[AccountSubCat_ReceiptSign] [nvarchar](50) NULL,
[AccountSubCat_PurchaseSign] [nvarchar](50) NULL,
[AccountSubCat_PaymentSign] [nvarchar](50) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Trn_Ledger](
[Ledger_No] [int] NULL,
[Ledger_Name] [nvarchar](500) NULL,
[Ledger_AccountSubCatNo] [int] NULL,
[Ledger_VATNumber] [nvarchar](250) NULL,
[Ledger_CreditDays] [numeric](18, 0) NULL,
[Ledger_OpeningBalance] [numeric](18, 3) NULL,
[Ledger_BankName] [nvarchar](500) NULL,
[Ledger_BankAC] [nvarchar](500) NULL,
[Ledger_SwiftCode] [nvarchar](500) NULL,
[Ledger_BankAddress] [nvarchar](500) NULL,
[Ledger_Countory] [nvarchar](500) NULL,
[Ledger_VATPercent] [numeric](18, 3) NULL
) ON [PRIMARY]
GO
I have only one transaction table which include Sales,Receipt and Purchase, Payment and Journal Entries. Vouchers are seprating through [AccTran_VchType] field. Note that all the transaction are double entery.
Sales,Receipt and Purchase enteries, on those voucher user cannot make mistake to put the ledger on credit or debit side because from the chart of account once set then when select particular ledger then either it will be credit or debit system will tell the user.
CREATE TABLE [dbo].[Trn_AccountTransactionList](
[AccTran_No] [int] NULL,
[AccTran_Number] [nvarchar](50) NULL,
[AccTran_Date] [date] NULL,
[AccTran_Remark] [nvarchar](500) NULL,
[AccTran_VchType] [nvarchar](50) NULL,
[AccTran_RefNo] [nvarchar](250) NULL,
[AccTran_BillDate] [date] NULL,
[AccTran_Post] [numeric](18, 0) NULL,
[AccTran_Number2] [numeric](18, 0) NULL,
[AccTran_InvoiceNo] [numeric](18, 0) NULL,
[AccTran_PoNo] [numeric](18, 0) NULL,
[AccTran_PDC] [numeric](18, 0) NULL,
[Acc_TranLedgerNo] [numeric](18, 0) NULL,
[Acc_TranAmount] [numeric](18, 3) NULL,
[Acc_CompNo] [numeric](18, 0) NULL,
[Acc_Updatedby] [nvarchar](50) NULL,
[Acc_UpdatedDatetime] [datetime] NULL,
[Acc_BankDate] [date] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Trn_AccountTransaction](
[AccTran_No] [int] NULL,
[AccTran_No_N] [int] NULL,
[AccTran_AccountLedgerNo] [int] NULL,
[AccTran_AmountDR] [numeric](18, 3) NULL,
[AccTran_AmountCR] [numeric](18, 3) NULL,
) ON [PRIMARY]
GO
Trial balance query.
CREATE VIEW [dbo].[View_TrialBalance]
AS
SELECT TOP (100) PERCENT AccountCat_Name, AccountGrp_Name, DR, CR, AccountGrp_Type, AccountGrp_No, Balance, AccountSubCat_No, CASE WHEN AccountGrp_No IN (1,
5) THEN Balance ELSE 0 END AS TrialDebit, CASE WHEN AccountGrp_No IN (2, 3, 4) THEN Balance ELSE 0 END AS TrialCredit, AccountSubCat_Name, Ledger_No,
Ledger_Name, AccTran_Date, Comp_No, Comp_Name, Acc_CompNo, Comp_Add, Comp_Tel, Comp_Fax, Comp_Country
FROM dbo.View_Ledger
ORDER BY AccountSubCat_No
GO
View_P&LandBalanceSheet
CREATE VIEW [dbo].[View_P&LandBalanceSheet]
AS
SELECT TOP (100) PERCENT AccountCat_Name, AccountGrp_Name, DR, CR, AccountGrp_Type, Balance, TrialDebit, TrialCredit, AccountSubCat_No,
CASE WHEN AccountGrp_Type = 'P&L' THEN TrialDebit ELSE 0 END AS InDebit, CASE WHEN AccountGrp_Type = 'P&L' THEN TrialCredit ELSE 0 END AS InCredit,
CASE WHEN AccountGrp_Type = 'BS' THEN TrialDebit ELSE 0 END AS BSDebit, CASE WHEN AccountGrp_Type = 'BS' THEN TrialCredit ELSE 0 END AS BSCredit,
AccountSubCat_Name, Ledger_No, Ledger_Name, AccTran_Date, Comp_No, Comp_Name, Acc_CompNo, Comp_Add, Comp_Tel, Comp_Fax, Comp_Country,
YEAR(AccTran_Date) AS YRS
FROM dbo.View_TrialBalance
WHERE (NOT (AccountCat_Name IS NULL))
ORDER BY AccountSubCat_No
GO
Note that all other reports are coming correctly based on above Transaction table. I want to ask the database structure is perfect?
Related
I have an issue with one of my MSSQL tables. The table has 1.2 Trillion rows about 1TB of data and growing. The table is partitioned into 8000 partitions, only about 800 are used. The others were created for expanded growth.
Inserts and selects are < 1s
My problem is Updates are very slow. To updated 1 record varchar(100) using the exact partition key and Identity Column key, it is 3s.
If I place the update code in a Stored Procedure, goes in less than 1s. If I add option (recompile), about 1s. Is there a way to fix this without adding recomile?
Thank you.
CREATE TABLE [dbo].[_tabletest](
[data_id] [bigint] IDENTITY(10000,1) NOT NULL,
[idx1_id] [smallint] NOT NULL,
[idx2_id] [bigint] NOT NULL,
[idx3_id] [bigint] NOT NULL,
[template_id] [bigint] NOT NULL,
[reference_id] [varchar](200) NOT NULL,
[data] [nvarchar](400) NULL,
[data_1] [tinyint] NULL,
[data_2] [varchar](max) NULL,
[data_3] [real] NULL,
[data_4] [tinyint] NULL,
[data_5] [tinyint] NULL,
[data_6] [bit] NULL,
[data_7] [nvarchar](50) NULL,
[data_8] [datetime] NULL,
[data_9] [nvarchar](50) NULL,
[data_10] [varchar](100) NULL,
[data_11] [varchar](100) NULL,
[data_12] [varchar](100) NULL,
[data_13] [varchar](300) NULL,
[data_14] [varchar](200) NULL,
[data_15] [uniqueidentifier] NULL,
[data_16] [varchar](600) NULL,
[data_17] [varchar](100) NULL,
[data_18] [varchar](100) NULL,
[data_19] [decimal](10, 5) NULL,
[data_20] [decimal](10, 5) NULL,
[data_21] [decimal](10, 5) NULL,
[data_22] [decimal](10, 5) NULL,
[data_23] [decimal](10, 5) NULL,
[data_24] [int] NULL,
[data_25] [int] NULL,
[data_26] [int] NULL,
[data_27] [int] NULL,
[data_28] [decimal](10, 5) NULL,
[data_29] [decimal](10, 5) NULL,
[data_30] [decimal](10, 5) NULL,
[data_31] [datetime] NULL,
[data_32] [decimal](10, 5) NULL,
[data_3] [bit] NULL,
[data_34] [varchar](max) NULL,
[data_35] [smallint] NULL,
[data_36] [bigint] NULL,
[data_37] [int] NULL,
[data_38] [real] NULL,
[data_39] [datetime] NULL,
[data_40] [varchar](2500) NULL,
CONSTRAINT [PK_data_id] PRIMARY KEY CLUSTERED
(
[idx1_id] ASC,
[idx2_id] ASC,
[idx3_id] ASC,
[data_id] ASC
)WITH (PAD_INDEX = ON, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90)
)
GO
ALTER TABLE [dbo].[_tabletest] SET (LOCK_ESCALATION = AUTO)
GO
ALTER TABLE [dbo].[_tabletest] ADD CONSTRAINT [DF_1] DEFAULT ((0)) FOR [data_1]
GO
ALTER TABLE [dbo].[_tabletest] ADD CONSTRAINT [DF_2] DEFAULT (getutcdate()) FOR [data_39]
GO
ALTER TABLE [dbo].[_tabletest] ADD DEFAULT (newid()) FOR [data_15]
GO
UPDATE [_tabletest] SET [data_40] = 'test-data'
WHERE [idx1_id] = 1209 AND [idx2_id] = 113795 AND [idx3] = 41195716 AND [data_id] = 1329110156
;
We are trying to insert 20000 or more records using SqlBulkCopy.WriteToServer method, but it is taking 140 sec to insert into the table.
Is there any way we can improve the time using SqlBulkCopy as this is widely used for bulk insertion?
Calling code:
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(dConn))
{
bulkCopy.ColumnMappings.Add("SamplingFileId", "SamplingFileId");
/*... 74 more columns ...*/
bulkCopy.BulkCopyTimeout = 500; //Assign Destination Table Name
bulkCopy.DestinationTableName = "cvr_ReviewSamplingLoans";
bulkCopy.BatchSize = 5000;
bulkCopy.WriteToServer(dtLoansToUpload);
}
Sql Table Definition:
CREATE TABLE [dbo].[ReviewSamplingLoans](
[Id] [int] IDENTITY(1000,1) NOT NULL,
[SamplingFileId] [int] NOT NULL,
[ReviewId] [int] NOT NULL,
[LoanType] [nvarchar](10) NOT NULL,
[LoanNumber] [nvarchar](50) NOT NULL,
[BorrowerName] [nvarchar](150) NOT NULL,
[OriginalUPB] [money] NOT NULL,
[CurrentUPB] [money] NOT NULL,
[OriginalMonthlyPayment] [money] NULL,
[CurrentMonthlyP&I] [money] NULL,
[PaymentFrequency] [nvarchar](2) NULL,
[OriginalNoteRate] [float] NULL,
[CurrentInterestRate] [float] NULL,
[OriginationDate] [date] NULL,
[MaturityDate] [date] NULL,
[FirstPaymentDate] [date] NULL,
[InterestPaidThruDate] [date] NULL,
[NextPaymentDate] [date] NULL,
[DelinquencyStatus] [nvarchar](10) NULL,
[LatePaymentCount] [int] NULL,
[StatedTerm] [int] NULL,
[OriginalAmortizationTerm] [int] NULL,
[NoteType] [nvarchar](10) NULL,
[RateType] [nvarchar](10) NULL,
[LoanProductType] [nvarchar](10) NULL,
[BalloonDueDate] [date] NULL,
[BalloonPaymentAmount] [money] NULL,
[PropertyCity] [nvarchar](50) NULL,
[PropertyState] [nchar](2) NULL,
[PropertyZipcode] [nvarchar](10) NULL,
[PropertyType] [nvarchar](10) NULL,
[PropertyTypeDescription] [nvarchar](50) NULL,
[LienPosition] [tinyint] NULL,
[OriginalLTV] [float] NULL,
[CurrentLTV] [float] NULL,
[SeniorLienBalance] [money] NULL,
[OriginalCommitmentAmount] [money] NULL,
[CurrentCommitmentAmount] [money] NULL,
[OriginalCombinedLTV] [float] NULL,
[CurrentCombinedLTV] [float] NULL,
[OriginalPropertyValue] [money] NULL,
[CurrentPropertyValue] [money] NULL,
[AppraisalDate] [date] NULL,
[AppraisalType] [nvarchar](10) NULL,
[PMI] [nchar](1) NULL,
[LoanPurpose] [nchar](1) NULL,
[OccupancyType] [nchar](1) NULL,
[DocumentationType] [nvarchar](10) NULL,
[OriginalFICO] [smallint] NULL,
[CurrentFICO] [smallint] NULL,
[LastFICOUpdatedDate] [date] NULL,
[DebtToIncomeRatio] [float] NULL,
[InterestOnlyPeriod] [smallint] NULL,
[Modication] [nchar](1) NULL,
[Foreclosure] [nchar](1) NULL,
[ArmProductType] [nvarchar](10) NULL,
[ArmMargin] [float] NULL,
[ArmIndexType] [nvarchar](10) NULL,
[FirstRateAdjustmentDate] [date] NULL,
[NextRateAdjustmentDate] [date] NULL,
[LifeFloor] [float] NULL,
[LifeCeiling] [float] NULL,
[InitialRateCap] [float] NULL,
[PeriodicRateCap] [float] NULL,
[InternalRiskRating] [nvarchar](10) NULL,
[CurrentDebtServiceRatio] [float] NULL,
[CurrentNetOperIncome] [float] NULL,
[OriginalDebtServiceRatio] [float] NULL,
[OriginalNetOperIncome] [float] NULL,
[CurrentOccupancy] [float] NULL,
[PropertyTotalSquareFootage] [int] NULL,
[UnitCount] [smallint] NULL,
[PrepaymentPenalty] [nchar](1) NULL,
[PrepaymentPenaltyType] [int] NULL,
[PrepaymentPenaltyTerm] [smallint] NULL,
CONSTRAINT [PK_ReviewSamplingLoans] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ReviewSamplingLoans] WITH NOCHECK ADD CONSTRAINT [FK_ReviewSamplingLoans_ReviewCollateralTypes] FOREIGN KEY([LoanType])
REFERENCES [dbo].[ReviewSamplingLoans] ([Code])
GO
ALTER TABLE [dbo].[ReviewSamplingLoans] CHECK CONSTRAINT [FK_ReviewSamplingLoans_ReviewCollateralTypes]
GO
ALTER TABLE [dbo].[ReviewSamplingLoans] WITH NOCHECK ADD CONSTRAINT [FK_ReviewSamplingLoans_ReviewSamplingFiles] FOREIGN KEY([ReviewId])
REFERENCES [dbo].[Reviews] ([Id])
GO
ALTER TABLE [dbo].[ReviewSamplingLoans] CHECK CONSTRAINT [FK_ReviewSamplingLoans_ReviewSamplingFiles]
GO
Regarding Size, excel file contains 20000 rows which are added to datatable upon validation and using sqlbulkcopy inserting into table.
SqlBulkCopy can be performed faster when you enable the Tablelock on the options.
Have a look here SqlBulkCopyOptions Enumeration
But keep in mind that once the operation has started, no one will be able to use the table until it is finished. Microsoft says that this tenchnique improves the speed when inserting large amounts of data.
select
a.COUNTY_FIPS
,COUNT(e.PROPERTY_ID) as house_count
,AVG(cast(e.AVM_FINAL_VALUE as bigint)) as avg_avm
,max(cast(e.AVM_FINAL_VALUE as bigint)) as max_avm
,min(cast(e.AVM_FINAL_VALUE as bigint)) as min_avm
from
RAW_Equity e
left join
(SELECT
SA_PROPERTY_ID, MM_FIPS_STATE_CODE, MM_FIPS_MUNI_CODE,
CASE
WHEN MM_FIPS_STATE_CODE < 10
THEN '0' + CAST(MM_FIPS_STATE_CODE as VARCHAR)
ELSE CAST(MM_FIPS_STATE_CODE as VARCHAR)
END
+ CASE
WHEN MM_FIPS_MUNI_CODE < 10
THEN '00' + CAST(MM_FIPS_MUNI_CODE as VARchar)
WHEN MM_FIPS_MUNI_CODE < 100
THEN '0' + CAST(MM_FIPS_MUNI_CODE as VARchar)
ELSE CAST(MM_FIPS_MUNI_CODE as VARchar)
END AS COUNTY_FIPS
FROM
RAW_Address) a ON a.SA_PROPERTY_ID = e.PROPERTY_ID
where
AVM_CONFIDENCE_SCORE >= 70
group by
a.COUNTY_FIPS
Is there any way I can improve the performance of this query? Schema for both the tables are shown below. I am was thinking about creating non clustered index on AVM_CONFIDENCE_SCORE but I think it will only going to increase the query time. Any help will be greatly appreciated.
RAWADDRESS table:
CREATE TABLE [dbo].[RAW_Address]
(
[SA_PROPERTY_ID] [int] NOT NULL,
[SA_SCM_ID] [int] NOT NULL,
[MM_STATE_CODE] [varchar](2) NOT NULL,
[MM_MUNI_NAME] [varchar](24) NOT NULL,
[MM_FIPS_STATE_CODE] [tinyint] NOT NULL,
[MM_FIPS_MUNI_CODE] [smallint] NOT NULL,
[MM_FIPS_COUNTY_NAME] [varchar](35) NOT NULL,
[SA_SITE_HOUSE_NBR] [varchar](20) NULL,
[SA_SITE_FRACTION] [varchar](10) NULL,
[SA_SITE_DIR] [varchar](2) NULL,
[SA_SITE_STREET_NAME] [varchar](40) NULL,
[SA_SITE_SUF] [varchar](4) NULL,
[SA_SITE_POST_DIR] [varchar](2) NULL,
[SA_SITE_UNIT_PRE] [varchar](10) NULL,
[SA_SITE_UNIT_VAL] [varchar](6) NULL,
[SA_SITE_CITY] [varchar](30) NULL,
[SA_SITE_STATE] [varchar](2) NOT NULL,
[SA_SITE_ZIP] [int] NULL,
[SA_SITE_PLUS_4] [smallint] NULL,
[SA_SITE_CRRT] [varchar](4) NULL,
[SA_MAIL_HOUSE_NBR] [varchar](20) NULL,
[SA_MAIL_FRACTION] [varchar](10) NULL,
[SA_MAIL_DIR] [varchar](2) NULL,
[SA_MAIL_STREET_NAME] [varchar](50) NULL,
[SA_MAIL_SUF] [varchar](4) NULL,
[SA_MAIL_POST_DIR] [varchar](2) NULL,
[SA_MAIL_UNIT_PRE] [varchar](10) NULL,
[SA_MAIL_UNIT_VAL] [varchar](6) NULL,
[SA_MAIL_CITY] [varchar](50) NULL,
[SA_MAIL_STATE] [varchar](2) NULL,
[SA_MAIL_ZIP] [int] NULL,
[SA_MAIL_PLUS_4] [smallint] NULL,
[SA_MAIL_CRRT] [varchar](4) NULL,
[SA_SITE_MAIL_SAME] [varchar](1) NULL
) ON [PRIMARY]
RAW Equity table:
CREATE TABLE [dbo].[RAW_Equity]
(
[PROPERTY_ID] [int] NOT NULL,
[SCM_ID] [int] NOT NULL,
[MM_STATE_CODE] [varchar](2) NOT NULL,
[MM_MUNI_NAME] [varchar](24) NOT NULL,
[MM_FIPS_STATE_CODE] [int] NOT NULL,
[MM_FIPS_MUNI_CODE] [int] NOT NULL,
[MM_FIPS_COUNTY_NAME] [varchar](35) NOT NULL,
[AVM_FINAL_VALUE] [int] NULL,
[AVM_LOW_VALUE] [int] NULL,
[AVM_HIGH_VALUE] [int] NULL,
[AVM_CONFIDENCE_SCORE] [int] NULL,
[FINAL_VALUE] [float] NULL,
[FIRST_POSITION_SR_UNIQUE_ID] [int] NULL,
[FIRST_POSITION_LOAN_DATE] [int] NULL,
[FIRST_POSITION_DOC_NBR] [varchar](20) NULL,
[FIRST_POSITION_LOAN_VAL] [int] NULL,
[FIRST_POSITION_LENDER_CODE] [int] NULL,
[FIRST_POSITION_LNDR_LAST_NAME] [varchar](50) NULL,
[FIRST_POSITION_LNDR_FIRST_NAME] [varchar](50) NULL,
[FIRST_POSITION_LENDER_TYPE] [varchar](1) NULL,
[FIRST_POSITION_LOAN_TYPE] [varchar](1) NULL,
[FIRST_POSITION_INTEREST_RATE_TYPE] [varchar](1) NULL,
[FIRST_POSITION_ESTIMATED_INTEREST_RATE] [float] NULL,
[FIRST_POSITION_LNDR_CREDIT_LINE] [varchar](1) NULL,
[FIRST_POSITION_MODELED_MORTGAGE_TYPE] [varchar](1) NULL,
[SECOND_POSITION_SR_UNIQUE_ID] [int] NULL,
[SECOND_POSITION_LOAN_DATE] [int] NULL,
[SECOND_POSITION_DOC_NBR] [varchar](20) NULL,
[SECOND_POSITION_LOAN_VAL] [int] NULL,
[SECOND_POSITION_LENDER_CODE] [int] NULL,
[SECOND_POSITION_LNDR_LAST_NAME] [varchar](50) NULL,
[SECOND_POSITION_LNDR_FIRST_NAME] [varchar](50) NULL,
[SECOND_POSITION_LENDER_TYPE] [varchar](1) NULL,
[SECOND_POSITION_LOAN_TYPE] [varchar](1) NULL,
[SECOND_POSITION_INTEREST_RATE_TYPE] [varchar](1) NULL,
[SECOND_POSITION_ESTIMATED_INTEREST_RATE] [float] NULL,
[SECOND_POSITION_LNDR_CREDIT_LINE] [varchar](1) NULL,
[SECOND_POSITION_MODELED_MORTGAGE_TYPE] [varchar](1) NULL,
[THIRD_POSITION_SR_UNIQUE_ID] [int] NULL,
[THIRD_POSITION_LOAN_DATE] [int] NULL,
[THIRD_POSITION_DOC_NBR] [varchar](20) NULL,
[THIRD_POSITION_LOAN_VAL] [int] NULL,
[THIRD_POSITION_LENDER_CODE] [int] NULL,
[THIRD_POSITION_LNDR_LAST_NAME] [varchar](50) NULL,
[THIRD_POSITION_LNDR_FIRST_NAME] [varchar](50) NULL,
[THIRD_POSITION_LENDER_TYPE] [varchar](1) NULL,
[THIRD_POSITION_LOAN_TYPE] [varchar](1) NULL,
[THIRD_POSITION_INTEREST_RATE_TYPE] [varchar](1) NULL,
[THIRD_POSITION_ESTIMATED_INTEREST_RATE] [float] NULL,
[THIRD_POSITION_LNDR_CREDIT_LINE] [varchar](1) NULL,
[THIRD_POSITION_MODELED_MORTGAGE_TYPE] [varchar](1) NULL,
[TOTAL_OUTSTANDING_LOANS] [bigint] NULL,
[LTV] [int] NULL,
[AVAILABLE_EQUITY] [int] NULL,
[LENDABLE_EQUITY] [int] NULL,
[PROCESS_ID] [int] NOT NULL,
[FILLER] [varchar](4) NULL,
CONSTRAINT [PK_RAW_Equity] PRIMARY KEY CLUSTERED
(
[PROPERTY_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]
You could try this way :-
(willing to know, how much this one, helpful to you)
Declare #result Table
(
RowId Int Identity(1,1) Primary Key
,COUNTY_FIPS Varchar(100)
,MM_FIPS_STATE_CODE Int
,MM_FIPS_MUNI_CODE Int
,house_count Int
,avg_avm Int
,max_avm Int
,min_avm Int
)
Insert Into #result(MM_FIPS_STATE_CODE,MM_FIPS_MUNI_CODE,house_count,avg_avm,max_avm,min_avm)
Select a.MM_FIPS_STATE_CODE
,a.MM_FIPS_MUNI_CODE
,Count(e.PROPERTY_ID) as house_count
,Avg(Cast(e.AVM_FINAL_VALUE as bigint)) as avg_avm
,Max(Cast(e.AVM_FINAL_VALUE as bigint)) as max_avm
,Min(Cast(e.AVM_FINAL_VALUE as bigint)) as min_avm
From RAW_Equity As e With (Nolock)
Left Join RAW_Address As a With (Nolock) On e.PROPERTY_ID = a.SA_PROPERTY_ID
Where e.AVM_CONFIDENCE_SCORE >= 70
Group by a.MM_FIPS_STATE_CODE
,a.MM_FIPS_MUNI_CODE
Update r
Set r.COUNTY_FIPS = REPLICATE('0',2-LEN(RTRIM(r.MM_FIPS_STATE_CODE))) + RTRIM(r.MM_FIPS_STATE_CODE) + REPLICATE('0',3-LEN(RTRIM(r.MM_FIPS_MUNI_CODE))) + RTRIM(r.MM_FIPS_MUNI_CODE)
From #result As r
Select r.COUNTY_FIPS
,r.house_count
,r.avg_avm
,r.max_avm
,r.min_avm
From #result As r
1st try without any index, and after that create clustered index as mentioned and try AGAIN the above same query
CREATE INDEX IX_RAW_Address_SA_PROPERTY_ID ON RAW_Address(SA_PROPERTY_ID)
I would put an index on:
Table: RAW_Equity
Columns: PROPERTY_ID, AVM_CONFIDENCE_SCORE
and
Table: RAW_Address
Columns: SA_PROPERTY_ID
Include: MM_FIPS_STATE_CODE,MM_FIPS_MUNI_CODE
Your query can be simplified, which will likely make it faster:
select
REPLICATE('0',2-LEN(RTRIM(a.MM_FIPS_STATE_CODE)))
+ RTRIM(a.MM_FIPS_STATE_CODE)
+ REPLICATE('0',3-LEN(RTRIM(a.MM_FIPS_MUNI_CODE)))
+ RTRIM(a.MM_FIPS_MUNI_CODE)
AS COUNTY_FIPS
,COUNT(e.PROPERTY_ID) as house_count
,AVG(cast(e.AVM_FINAL_VALUE as bigint)) as avg_avm
,max(cast(e.AVM_FINAL_VALUE as bigint)) as max_avm
,min(cast(e.AVM_FINAL_VALUE as bigint)) as min_avm
from
RAW_Equity e
left join RAW_Address a
ON a.SA_PROPERTY_ID = e.PROPERTY_ID
where
e.AVM_CONFIDENCE_SCORE >= 70
group by
a.MM_FIPS_STATE_CODE, a.MM_FIPS_MUNI_CODE
If RAW_Address doesn't already have a CLUSTERED INDEX with SA_PROPERTY_ID as the first key of the index, then this may help:
CREATE INDEX IX_RAW_Address_SA_PROPERTY_ID ON RAW_Address(SA_PROPERTY_ID)
INCLUDE (MM_FIPS_STATE_CODE, MM_FIPS_MUNI_CODE)
This is maddening! Code in question has been running for over 5 years.
Here's the scoop....
I am doing an INSERT...SELECT into a table with a primary key that is an identity column. I do not specify the key when I insert - SQL Server generates it as expected.
I am doing the insert in a stored procedure that I call in a loop (for loop in SSIS, actually). The stored procedure will insert rows in batches (configurable). It might insert 1000 rows at a time or it might insert 50,000 - doesn't matter. It will work for a random number of calls (inserting thousands of rows) and then it will fail, out of the blue, with a
Violation of primary key / duplicate
error. If I check the identity seed - it is correct. If I kick off the process again it will work fine, for a while.
The values being inserted are coming from 2 tables that I join together, as if that matters.
The bulk of my code is below:
WHILE #pk <= #max_pk
BEGIN
INSERT INTO tbl_claim_line (fk_batch_control_group, fk_claim, fk_provider, service_from_date, service_to_date, allowed, net_paid, COB, flex_1, flex_2, flex_3, flex_4)
SELECT
#fk_batch_control_group
, c.pk_claim
, p.pk_provider
, i.date_of_service_from
, i.date_of_service_to
, i.allowed_amount
, i.net_paid_amount
, i.cob_amount
, i.claimline_flex_1
, i.claimline_flex_2
, i.claimline_flex_3
, i.claimline_flex_4
FROM
tbl_import i
INNER JOIN
tbl_import__claim c ON i.claim_number = c.claim_number
LEFT JOIN
tbl_import__provider p ON ISNULL(i.provider_type,'') = ISNULL(p.provider_type,'')
AND ISNULL(i.provider_specialty,'') = ISNULL(p.provider_specialty,'')
AND ISNULL(i.provider_zip_code,'') = ISNULL(p.provider_zip_code,'')
WHERE
pk_import = #pk
UPDATE tbl_import
SET fk_claim_line = SCOPE_IDENTITY()
WHERE pk_import = #pk
SET #pk += 1
END
--TABLE DEFINITIONS...
CREATE TABLE [dbo].[tbl_claim_line](
[fk_batch_control_group] [int] NOT NULL,
[fk_claim] [int] NOT NULL,
[fk_provider] [int] NULL,
[service_from_date] [date] NULL,
[service_to_date] [date] NULL,
[allowed] [money] NULL,
[net_paid] [money] NULL,
[COB] [money] NULL,
[flex_1] [varchar](200) NULL,
[flex_2] [varchar](200) NULL,
[flex_3] [varchar](200) NULL,
[flex_4] [varchar](200) NULL,
[pk_claim_line] [int] IDENTITY(1,1) NOT NULL,
[insert_date] [datetime] NOT NULL,
CONSTRAINT [PK_tbl_claim_line] PRIMARY KEY NONCLUSTERED
(
[pk_claim_line] 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].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_batch_control_group]
FOREIGN KEY([fk_batch_control_group])
REFERENCES [dbo].[tbl_batch_control_group] ([pk_batch_control_group])
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_batch_control_group]
GO
ALTER TABLE [dbo].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_claim]
FOREIGN KEY([fk_claim])
REFERENCES [dbo].[tbl_claim] ([pk_claim])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_claim]
GO
ALTER TABLE [dbo].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_provider]
FOREIGN KEY([fk_provider])
REFERENCES [dbo].[tbl_provider] ([pk_provider])
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_provider]
GO
ALTER TABLE [dbo].[tbl_claim_line] ADD CONSTRAINT [DF_tbl_claim_line__insert_date] DEFAULT (getdate()) FOR [insert_date]
GO
----second table
CREATE TABLE [dbo].[tbl_import](
[fk_claim_line] [int] NULL,
[member_id] [varchar](50) NULL,
[member_card_id] [varchar](50) NULL,
[member_first_name] [varchar](50) NULL,
[member_last_name] [varchar](50) NULL,
[member_dob] [varchar](50) NULL,
[member_gender] [varchar](50) NULL,
[member_subscriber_relationship_code] [varchar](50) NULL,
[member_address_line_1] [varchar](100) NULL,
[member_address_line_2] [varchar](100) NULL,
[member_city] [varchar](50) NULL,
[member_state] [varchar](50) NULL,
[member_zip] [varchar](50) NULL,
[member_phone] [varchar](50) NULL,
[member_email] [varchar](50) NULL,
[subscriber_id] [varchar](50) NULL,
[group_line_of_business] [varchar](50) NULL,
[group_product] [varchar](50) NULL,
[group_employer] [varchar](50) NULL,
[provider_first_name] [varchar](50) NULL,
[provider_last_or_full_name] [varchar](200) NULL,
[provider_type] [varchar](200) NULL,
[provider_specialty] [varchar](400) NULL,
[provider_zip_code] [varchar](50) NULL,
[provider_tax_id] [varchar](50) NULL,
[medical_code_1] [varchar](10) NULL,
[medical_code_1_description] [varchar](500) NULL,
[medical_code_2] [varchar](10) NULL,
[medical_code_2_description] [varchar](500) NULL,
[medical_code_3] [varchar](10) NULL,
[medical_code_3_description] [varchar](500) NULL,
[medical_code_4] [varchar](10) NULL,
[medical_code_4_description] [varchar](500) NULL,
[medical_code_5] [varchar](10) NULL,
[medical_code_5_description] [varchar](500) NULL,
[medical_code_6] [varchar](10) NULL,
[medical_code_6_description] [varchar](500) NULL,
[medical_code_7] [varchar](10) NULL,
[medical_code_7_description] [varchar](500) NULL,
[medical_code_8] [varchar](10) NULL,
[medical_code_8_description] [varchar](500) NULL,
[medical_code_9] [varchar](10) NULL,
[medical_code_9_description] [varchar](500) NULL,
[medical_code_10] [varchar](10) NULL,
[medical_code_10_description] [varchar](500) NULL,
[medical_code_11] [varchar](10) NULL,
[medical_code_11_description] [varchar](500) NULL,
[medical_code_12] [varchar](10) NULL,
[medical_code_12_description] [varchar](500) NULL,
[medical_code_13] [varchar](10) NULL,
[medical_code_13_description] [varchar](500) NULL,
[medical_code_14] [varchar](10) NULL,
[medical_code_14_description] [varchar](500) NULL,
[medical_code_15] [varchar](10) NULL,
[medical_code_15_description] [varchar](500) NULL,
[medical_code_16] [varchar](10) NULL,
[medical_code_16_description] [varchar](500) NULL,
[date_of_service_from] [varchar](50) NULL,
[date_of_service_to] [varchar](50) NULL,
[claim_number] [varchar](50) NULL,
[claim_line_number] [varchar](50) NULL,
[original_claim_number] [varchar](50) NULL,
[allowed_amount] [varchar](50) NULL,
[net_paid_amount] [varchar](50) NULL,
[cob_amount] [varchar](50) NULL,
[date_paid] [varchar](50) NULL,
[member_flex_1] [varchar](200) NULL,
[member_flex_2] [varchar](200) NULL,
[member_flex_3] [varchar](200) NULL,
[member_flex_4] [varchar](200) NULL,
[claim_flex_1] [varchar](200) NULL,
[claim_flex_2] [varchar](200) NULL,
[claim_flex_3] [varchar](200) NULL,
[claim_flex_4] [varchar](200) NULL,
[claimline_flex_1] [varchar](200) NULL,
[claimline_flex_2] [varchar](200) NULL,
[claimline_flex_3] [varchar](200) NULL,
[claimline_flex_4] [varchar](200) NULL,
[pk_import] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_tbl_import] PRIMARY KEY NONCLUSTERED
(
[pk_import] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I ran into this and much like user3170349, it was a seed issue on the column. I'm adding some additional info, however.
First, you can run this to figure out if you have a seed problem:
DBCC CHECKIDENT ('TABLE_NAME_GOES_HERE', NORESEED);
This will give you information which will read something like this:
Checking identity information: current identity value 'XXXX', current column value 'YYYY'.
If YYYY is larger than XXXX, then you have a problem and need to RESEED the table to get things going again. You can do so with the following command:
DBCC CHECKIDENT ('TABLE_NAME_GOES_HERE', RESEED, ZZZZZ);
Where ZZZZ is the reseed value. That value should be at least one higher than YYYY. YMMV, so pick a value that is appropriate for your situation.
"Code in question has been running for over 5 years."
"It might insert 1000 records at a time or it might insert 50,000 "
Is it possible you have finally overflowed the integer type of the primary key?
Did it wrap around and is now starting over? That would cause duplicate primary keys.
I need to add days to a varchar representation in the yyyymmdd format.
I'm getting an error when converting a varchar(8) value to datetime in SQL Server 2005.
I don't have a clue why!
Here is what works:
select
convert (datetime, '20111019')
from _table
This does not work, although the value in the column is 20111019
select
convert (datetime, RechDatum)
from _table
I already tried:
convert (datetime, RechDatum, 112)
and
SET DATEFORMAT ymd
I was able to solve the problem with an own function. But I'm still confused and would be glad for answers of you!
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'getDateFromString' and type = 'FN')
DROP FUNCTION getDateFromString
GO
CREATE FUNCTION getDateFromString (#Cyear VARCHAR(4), #Cmonth smallint, #Cday smallint, #offsetdays smallint)
RETURNS DATETIME
AS BEGIN
DECLARE #day SMALLINT
DECLARE #month SMALLINT
DECLARE #year SMALLINT
SET #day = CAST (#Cday AS SMALLINT)
SET #month = CAST (#Cmonth AS SMALLINT)
SET #year = CAST (#Cyear AS SMALLINT)
RETURN DateAdd(day, #offsetdays,
(SELECT
DateAdd (day, #day -1,
DateAdd (month, #month -1,
DateAdd (Year, #year-1900,0))) ))
END
GO
Here is the table definition, it's part of an german ERP system:
CREATE TABLE [dbo].[AuftrKopf](
[ID] [int] IDENTITY(1,1) NOT NULL,
[IsSammelRechnung] [bit] NULL,
[AuftragsNr] [int] NULL,
[Kd_ID] [int] NULL,
[Fahrer_ID] [int] NULL,
[Debitor] [int] NULL,
[Fz_ID] [int] NULL,
[FHFz_ID] [int] NULL,
[AuftrArt_ID] [int] NULL,
[AuftrHerkunft_ID] [int] NULL,
[AuftrZustand_ID] [int] NULL,
[RechnungsArt_ID] [int] NULL,
[AuftragVom] [varchar](8) NULL,
[FertigAmUm] [varchar](12) NULL,
[BringenAmUm] [varchar](12) NULL,
[AbgeschlossenAmUm] [varchar](12) NULL,
[StempelkartenNr] [int] NULL,
[HuetchenNr] [int] NULL,
[LeistungsDatum] [varchar](8) NULL,
[RechnDatum] [varchar](8) NULL,
[ValutaDatum] [varchar](8) NULL,
[NettoSummenspalte1] [decimal](18, 2) NULL,
[NettoSummenspalte2] [decimal](18, 2) NULL,
[NettoSummenspalte3] [decimal](18, 2) NULL,
[NettoSummenspalte4] [decimal](18, 2) NULL,
[NettoSummenspalte5] [decimal](18, 2) NULL,
[NettoSummenspalte6] [decimal](18, 2) NULL,
[NettoSummenspalte7] [decimal](18, 2) NULL,
[NettoSummenspalte8] [decimal](18, 2) NULL,
[NettoSummenspalte9] [decimal](18, 2) NULL,
[NettoSummenspalte10] [decimal](18, 2) NULL,
[NettoOhneSummenSpalte] [decimal](18, 2) NULL,
[MwStSummenspalte1] [decimal](18, 2) NULL,
[MwStSummenspalte2] [decimal](18, 2) NULL,
[MwStSummenspalte3] [decimal](18, 2) NULL,
[MwStBasisspalte1] [decimal](18, 2) NULL,
[MwStBasisspalte2] [decimal](18, 2) NULL,
[MwStBasisspalte3] [decimal](18, 2) NULL,
[BasisspalteOhneMwSt] [decimal](18, 2) NULL,
[KdNr] [int] NULL,
[KdNachname] [nvarchar](50) NULL,
[KdVorname] [nvarchar](50) NULL,
[KdTelefon] [nvarchar](30) NULL,
[KdMobilTelefon] [nvarchar](30) NULL,
[KdEmail] [nvarchar](100) NULL,
[Kennz] [nvarchar](20) NULL,
[BerKennz] [nvarchar](20) NULL,
[KM] [int] NULL,
[Laufleistung] [int] NULL,
[RabattStufe] [int] NULL,
[Adresse_ID] [int] NULL,
[CPrLieferschein] [int] NULL,
[BincMwSt] [bit] NULL,
[LandWKuerzel] [nvarchar](4) NULL,
[Bemerkung] [nvarchar](50) NULL,
[Verkaeufer_ID] [int] NULL,
[Sachbearbeiter_ID] [int] NULL,
[PersIDFaktura] [int] NULL,
[Monteur_ID] [int] NULL,
[Team_ID] [int] NULL,
[CPrRepAngebot] [int] NULL,
[CPrRepAuftr] [int] NULL,
[CPrWerkstattkarte] [int] NULL,
[CPrRechnung] [int] NULL,
[CPrRechnungFormularName] [nvarchar](50) NULL,
[CPrRepRechnKopie] [int] NULL,
[CPrPickerzettel] [int] NULL,
[CPrVorabRechn] [int] NULL,
[CPrAuftrBestaet] [int] NULL,
[BOhneFzDaten] [bit] NULL,
[BOhneNr] [bit] NULL,
[PrOhneEinzelpreis] [bit] NULL,
[FHFzNW] [bit] NULL,
[FHDifferenzbesteuert] [bit] NULL,
[StornoZuAuftrNr] [int] NULL,
[Storno_Datum] [varchar](8) NULL,
[Buchungsdatum] [varchar](14) NULL,
[VakBuchungsModus] [int] NULL,
[DiffNettoKompZuSumme] [decimal](18, 2) NULL,
[Vorgangsart_ID] [int] NULL,
[Beschreibung] [ntext] NULL,
[IsErreichbarTel1perSMS] [bit] NULL,
[IsErreichbarTel1perTel] [bit] NULL,
[IsErreichbarMobilperSMS] [bit] NULL,
[IsErreichbarMobilperTel] [bit] NULL,
[IsErreichbarEmail] [bit] NULL,
[StundenGeschaetzt] [decimal](18, 2) NULL,
[WKZ_Zweitwaehrung] [nvarchar](3) NULL,
[Kurs_Zweitwaehrung] [decimal](18, 7) NULL,
[EndbetragInZweitwaehrung] [bit] NULL,
[Kulanzursprung] [int] NULL,
[IsKulanzgesplittet] [bit] NULL,
[SplitUrsprung] [int] NULL,
[IsGesplittet] [bit] NULL,
[FlottenReparaturkostenId] [int] NULL,
[NovaSatz] [decimal](18, 2) NULL,
[NovaGesamt] [decimal](18, 2) NULL,
[NovaBasis] [decimal](18, 2) NULL,
[NovaBonusMalus] [decimal](18, 2) NULL,
[FilialNr] [int] NULL,
[Angebotverfall] [varchar](8) NULL,
[IvecoSonderverkauf] [bit] NULL,
[LetzteAenderung] [varchar](14) NULL,
[IsAnzahlung] [bit] NULL,
[AnzahlungSumme] [decimal](18, 2) NULL,
[SADESperre] [int] NULL,
[SADEVersendet] [varchar](14) NULL,
[MP2Angebotsnummer] [int] NULL,
[SchadenNummer] [nvarchar](20) NULL,
[ReferenzAudaNet] [nvarchar](40) NULL,
[LetzterBearbeiter] [nvarchar](50) NULL,
[Rechnername] [nvarchar](50) NULL,
[ReferenzAudaNetCaseId] [nvarchar](40) NULL,
[AuftragsanlageOrder] [bit] NULL,
[RabattDrucken] [bit] NULL,
[AuftragStatus1_ID] [int] NULL,
[AuftragStatus2_ID] [int] NULL,
[AuftragStatus3_ID] [int] NULL,
[AuftragStatus4_ID] [int] NULL,
[TeileZurueckBeiGutschrift] [bit] NULL,
[StornoGrund_ID] [int] NULL,
[Anleger_ID] [int] NULL,
[Betriebsstunden] [int] NULL,
[SperreSADE] [bit] NULL,
[FiOnlineContractId] [nvarchar](20) NULL,
[DWNichtMehrFragen] [bit] NULL,
[BestellZaehler] [int] NULL,
[NovaOhneAufschlag] [bit] NULL,
[DefaultLager_ID] [int] NULL,
[RechnungBezahltKennzeichnen] [bit] NULL,
[AnVWBackboneAsNewReported] [bit] NULL,
[VWBackboneRequestID] [nvarchar](20) NULL,
[VWBackboneConversationID] [nvarchar](70) NULL,
[VWBackboneStatusID] [int] NULL,
[VWBackboneSaga2ClaimNr] [nvarchar](2) NULL,
[VWBackboneSaga2GarantieAntragNr] [int] NULL,
[VWBackboneSaga2MaxClaims] [int] NULL,
CONSTRAINT [PK_AuftrKopf] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
The strange thing is, that the conversions work on my computer with german-swiss settings, but not on the client-computer, which has a french setup.
This works:
DECLARE #datevar varchar(8)
SET #datevar = '20111019'
SELECT CONVERT(DATETIME,#datevar,114) AS RechDatum
And this:
SELECT '20111019' AS RechDatum INTO #tmp
SELECT CONVERT(DATETIME,Rechdatum,114) AS RechDate from #tmp
works fine too. Are you sure you table column has the right type/format?
I found the solution to the problem with the input of Laszlo Tenki.
I read the SET DATEFORMAT-article of the Microsoft Help again:
http://msdn.microsoft.com/en-us/library/ms189491.aspx
It seems that there are some character string formats, that are independent of SET DATEFORMAT.
From the Microsoft-Article:
Some character strings formats, for example ISO 8601, are interpreted independently of the DATEFORMAT setting. For more information about how to use DATEFORMAT with different formats see the "String Literal Date and Time Formats" section in Using Date and Time Data.
The solution answer can be found here:
http://msdn.microsoft.com/en-us/library/ms180878.aspx
I could solve the problem with
SET LANGUAGE
Set language sets the SQL-Server-Settings for the Current Session.
The interpretation of String representations by the SQL-Server is dependent on SET DATEFORMAT and SET LANGUAGE.
The format I chose ISO 8601 is 'SET DATETIME'-independent, thats why that didn't work.
Check the info here:
http://msdn.microsoft.com/en-us/library/ms180878.aspx
However: Microsoft recommends the usage of ISO 8601, because it is multilanguage-compatible. That raises the question, why I needed to change the LANGUAGE at all.
SQL server has its own date formatting mechanism that is independent from the regional settings of the computer.
I would say that it is probably different on your developer machine and the production server. It looks that you can process dates in either format, your only problem is that it changes and you don't know for sure what to prepare for.
For SET DATEFORMAT, See the details here:
http://msdn.microsoft.com/en-us/library/ms189491.aspx