Tuesday, April 12, 2016

MERGE into tables with FK Constraints: how to MERGE Workaround examples:

Updated with work-around examples for "

Unable to perform Slowly Changing Dimension updates on a dimension table with a foreign key"

-- MERGE into tables with FK Constraints: how to MERGE Workaround examples:
-- see below sql examples

/****************************************
-- Temp Table Example
*****************************************/

/****************************************
-- Drop Check Constraint Examples 

-- Code:

/****************************************
-- Temp Table Example
*****************************************/
declare @Extract_Datetime datetime, @Expire_Datetime datetime, @auditKey uniqueidentifier
Select @Extract_Datetime = getdate()
, @Expire_Datetime = CoReStage.dbo.udfGetExpireDatetimeDefault()
,@auditKey = '2583EA74-70EF-E211-AE25-00090FFE0001'

select* into #Material_Status from [Material_Status] where 1=0

INSERT INTO #Material_Status ([Material_Status_UID],[Code],[Description],[Effective_Datetime],[Expire_Datetime],[IsCurrent],[Audit_Key])
SELECT  Material_Status_UID,[Code],[Description], [Effective_Datetime], [Expire_Datetime], [IsCurrent], [Audit_Key]  
FROM
  (
   MERGE [Material_Status] as targ
   USING material_status AS src
   ON (targ.[Code] = src.[Code])
   --WHEN MATCHED and checksum(targ.[Description],targ.[code])  <> checksum(src.[Description],src.[code]) and targ.IsCurrent='1'THEN
   WHEN MATCHED and (targ.[Description] <> src.[Description] and targ.IsCurrent='1') THEN
    UPDATE SET targ.IsCurrent = '0',  targ.[Expire_Datetime] = @Extract_Datetime
   WHEN NOT MATCHED THEN
    /********** NEW ROW ***********/
    INSERT VALUES (newid(), src.[Code], src.[Description], @Extract_Datetime, @Expire_Datetime, '1', @auditKey)
   OUTPUT $action Action_Out, inserted.Material_Status_UID, src.[Code], src.[Description], @Extract_Datetime [Effective_Datetime] , @Expire_Datetime Expire_Datetime, '1' IsCurrent, @auditKey [Audit_Key]
  ) AS Changes
WHERE Changes.Action_Out = 'UPDATE';
   
select * from #Material_Status
insert into  [Material_Status] ([Code],[Description],[Effective_Datetime],[Expire_Datetime],[IsCurrent],[Audit_Key])
select [Code],[Description],[Effective_Datetime],[Expire_Datetime],[IsCurrent],[Audit_Key] from #Material_Status

drop table #Material_Status
select * from [Material_Status]
select * from material_status


/****************************************
-- Drop Check Constraint Examples 
*****************************************/

-- 1) Create tables 
USE [AdventureWorks2012]
GO
CREATE TABLE [dbo].[Supplier](
       [SupplierCode] CHAR(8) PRIMARY KEY,
       [SupplierName] [varchar](50) NULL,
       [Address] [varchar](50) NULL,
) ON [PRIMARY]
GO
INSERT INTO [dbo].[Supplier] ([SupplierCode], [SupplierName], [Address])
VALUES
('S0000001', 'ABC Company', 'USA'),
('S0000002', 'XYZ Corporation', 'USA')
GO
SELECT * FROM [dbo].[Supplier]
 

USE [AdventureWorks2012]
GO

CREATE TABLE [dbo].[DimSupplier](
       [SupplierId] [int] IDENTITY(1,1) NOT NULL,
       [SupplierCode] CHAR(8),
       [SupplierName] [varchar](50) NULL,
       [Address] [varchar](50) NULL,
       [EffectiveDate] [date] NULL,
       [ExpirationDate] [date] NULL,
       [CurrentFlag] [char](1) NULL,
       CONSTRAINT [PK_DimSupplier] PRIMARY KEY CLUSTERED ([SupplierId] ASC)
) ON [PRIMARY]
GO


CREATE TABLE [dbo].[DimSupplierAttributes](
[ID] [int] IDENTITY(1,1) NOT NULL,
[DimSupplierID] [int] NOT NULL,
 CONSTRAINT [PK_DimSupplierAttributes] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 70) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [DimSupplierAttributes]  WITH CHECK ADD  CONSTRAINT [R_10355] FOREIGN KEY([DimSupplierID])
REFERENCES [DimSupplier] ([SupplierId])
GO


select * from [dbo].[DimSupplier]
select * from [dbo].[Supplier]

--2) Type 1 changes

MERGE INTO [dbo].[DimSupplier] Dest
USING [dbo].[Supplier] Src
     ON (Dest.[SupplierCode] = Src.[SupplierCode])
WHEN MATCHED AND (Dest.[Address] != Src.[Address])
THEN UPDATE
  SET Dest.[Address] = Src.[Address];

select * from [dbo].[DimSupplier]



--3) Type 2 changes (creates error)

-- This inserts another record to the dimension for SCD Type changes
INSERT INTO [dbo].[DimSupplier] ([SupplierCode], [SupplierName], [Address], [EffectiveDate], [ExpirationDate], [CurrentFlag])
SELECT [SupplierCode], [SupplierName], [Address], [EffectiveDate], [ExpirationDate], [CurrentFlag]
FROM
(
       MERGE [dbo].[DimSupplier] TT
       USING [dbo].[Supplier] ST
              ON (TT.[SupplierCode] = ST.[SupplierCode])
       -- This inserts new records in the dimension table
       WHEN NOT MATCHED THEN
              INSERT ([SupplierCode], [SupplierName], [Address], [EffectiveDate],   [ExpirationDate], [CurrentFlag])
              VALUES ([SupplierCode], [SupplierName], [Address], '01/01/1900', NULL, 'Y')
       -- This marks the older record to be outdated for SCD Type 2
       WHEN MATCHED AND TT.[CurrentFlag] = 'Y' AND (ISNULL(TT.[SupplierName], '') != ISNULL(ST.[SupplierName], '')) THEN
              UPDATE SET TT.[CurrentFlag] = 'N', TT.[ExpirationDate] = GETDATE() - 1
              OUTPUT $Action Action_Taken, ST.[SupplierCode], ST.[SupplierName], 
              ST.[Address], GETDATE() AS [EffectiveDate], NULL AS [ExpirationDate], 'Y' AS [CurrentFlag]
)
AS MERGE_OUT
WHERE MERGE_OUT.Action_Taken = 'UPDATE';
GO
SELECT * FROM [dbo].[DimSupplier]

-- type 2 change
USE [AdventureWorks2012]
GO
UPDATE [dbo].[Supplier]
SET [SupplierName] = 'ABC Company LtdZ.'
WHERE [SupplierCode] = 'S0000001'
GO
SELECT * FROM [dbo].[Supplier]
GO
SELECT * FROM [dbo].[DimSupplier]

-- exec type 2 merge above
SELECT * FROM [dbo].[Supplier]
GO
SELECT * FROM [dbo].[DimSupplier]


--4) Type 2 changes (fix error with NOCHECK CONSTRAINT)
-- MERGE with Drop FK on all Child tables
ALTER TABLE [dbo].[DimSupplierAttributes] NOCHECK CONSTRAINT ALL
GO


INSERT INTO [dbo].[DimSupplier] ([SupplierCode], [SupplierName], [Address], [EffectiveDate], [ExpirationDate], [CurrentFlag])
SELECT [SupplierCode], [SupplierName], [Address], [EffectiveDate], [ExpirationDate], [CurrentFlag]
FROM
(
       MERGE [dbo].[DimSupplier] TT
       USING [dbo].[Supplier] ST
              ON (TT.[SupplierCode] = ST.[SupplierCode])
       -- This inserts new records in the dimension table
       WHEN NOT MATCHED THEN
              INSERT ([SupplierCode], [SupplierName], [Address], [EffectiveDate],   [ExpirationDate], [CurrentFlag])
              VALUES ([SupplierCode], [SupplierName], [Address], '01/01/1900', NULL, 'Y')
       -- This marks the older record to be outdated for SCD Type 2
       WHEN MATCHED AND TT.[CurrentFlag] = 'Y' AND (ISNULL(TT.[SupplierName], '') != ISNULL(ST.[SupplierName], '')) THEN
              UPDATE SET TT.[CurrentFlag] = 'N', TT.[ExpirationDate] = GETDATE() - 1
              OUTPUT $Action Action_Taken, ST.[SupplierCode], ST.[SupplierName], 
              ST.[Address], GETDATE() AS [EffectiveDate], NULL AS [ExpirationDate], 'Y' AS [CurrentFlag]
)
AS MERGE_OUT
WHERE MERGE_OUT.Action_Taken = 'UPDATE';
GO
SELECT * FROM [dbo].[DimSupplier]

ALTER TABLE [dbo].[DimSupplierAttributes] WITH NOCHECK CHECK CONSTRAINT ALL
GO

Win 8 Performance Tweaks, Drivers Updates and what to avoid

Quick Summary of Tweaks for Win8 for Programming or Video Editing:

1. Service Tweaks


2. Regedit Tweaks
and
and

3. Driver Updates: 
Network Card from here

Display Driver NVidia
*** Important, do NOT install GeForce Experience
Also, here is more info on NVidia, only install the driver, and even after that u need to watch out for a service exe that runs associated with the driver, u can disable by seeing this article bottom of article.

Private Cloud: How to use it and login to your own private share!

So I got the seagatecentrol 4TB Private cloud and am giving it a spin on my home netwiork, after some setup, which was fairly whoch includes the OTB 1) local share setup and 2) remote access wizard. Now that the setup is complete here is the step by step on how to access the PrivateCloud :

Saturday, March 5, 2016

ASP.Net Update Panel GridView does not fire Child Trigger events due to Validator in Template Column

ASP.Net Update Panel GridView does not fire Child Trigger events due to Validator in Template Column

Found this issue while working on a GridView page, when we have a RequiredFieldValidator which should work in a FooterTemplate
for new records, this prevents Child control events to fire such as Button, the safe bet is to avoid Asp.Net Validators and use
JS client side to validate when dealing with Update Panels with GridViews.

This Doesnt Work:
  <asp:TemplateField HeaderText="Resource Last Name" ItemStyle-Width = "100px" SortExpression="Resource_Last_Name">
                                                       <ItemTemplate>
                                                           <asp:Label ID="lblResource_Last_Name" runat="server" Text='<%# Eval("Resource_Last_Name") %>'></asp:Label>
                                                           <asp:TextBox ID="txtResource_Last_Name" runat="server" Text='<%# Eval("Resource_Last_Name") %>' Visible="false"></asp:TextBox>
                                                       </ItemTemplate>
                                                       <FooterTemplate>
                                                           <asp:TextBox ID="txtResource_Last_Name" runat="server"></asp:TextBox>
                                                           <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtResource_Last_Name"
                                                               Display="Dynamic" ErrorMessage="You must enter a last name." ForeColor="">*</asp:RequiredFieldValidator>
                                                       </FooterTemplate>
                                                       <FooterStyle Wrap="False" />
                                                   </asp:TemplateField>




This is OK:
 <asp:TemplateField HeaderText="Resource Last Name" ItemStyle-Width = "100px" SortExpression="Resource_Last_Name">
                                                       <ItemTemplate>
                                                           <asp:Label ID="lblResource_Last_Name" runat="server" Text='<%# Eval("Resource_Last_Name") %>'></asp:Label>
                                                           <asp:TextBox ID="txtResource_Last_Name" runat="server" Text='<%# Eval("Resource_Last_Name") %>' Visible="false"></asp:TextBox>
                                                       </ItemTemplate>
                                                       <FooterTemplate>
                                                           <asp:TextBox ID="txtResource_Last_Name" runat="server"></asp:TextBox>
                                                       </FooterTemplate>
                                                       <FooterStyle Wrap="False" />
                                                   </asp:TemplateField>

Visual Studio Environment Tuning, Tweak Debug and Build Settings to speed up Dev

Found this very helpful, especially if ur stuck on some VS 2010 projects like I am:
Slash Compile Times!

Here are a few more notes:
Added some other tune items found on : geocortex here and a few other places

More refs for VS 2010 HELL:
Compilation:
http://stackoverflow.com/questions/280751/what-is-the-best-practice-for-copy-local-and-with-project-references

Add
  <compilation ... batch="false"  optimizeCompilations="true"  />

 modifying your symbol settings.
uncheck  "Only Build startup projects and dependencies on Run" checkbox in Tools\Options.

Overall Performance
Disable Track Changes. (Tools\Options\Text Editor\General\Track changes)

Disable Navigation Bar (Tools\Options\Text Editor\C#)

Disable "Track Active Item in Solution Explorer" (Tools\Options\Projects and Solutions)

Disable Detect when file is changed outside the environment

(Tools\Options\Environment \Documents)


DEBUGGING:
an disable it by setting the environment variable _NO_DEBUG_HEAP to 1. You can set it at the system level: Control Panel ? System ? Advanced system settings ? Environment Variables.

VIA:
http://preshing.com/20110717/the-windows-heap-is-slow-when-launched-from-the-debugger/


temp Files:
Delete: C:\Users\krintoul\AppData\Local\Microsoft\WebSiteCache

and

C:\Users\krintoul\AppData\Local\Temp\Temporary ASP.NET Files\siteName

disable Edit & Continue option.. Tools -> Options -> Debugging -> Edit & Continue

Go to OPTIONS>ENVIRONMENT..under Visual Experience, untick “Use Hardware Graphics Acceleration If Available” for some weird reason your dev environment should loosen / speed straight up. Works on all our boxes

Disable Addins:
Goto Tools>Options>Environment>Add In Security
You will see a checkbox with “allow add in components to load” .Just uncheck it. Restart the IDE and check the lightning speed of the IDE now…


Web Debug:
Switching from Source to Design in my VS 2012 went very slow.
The solution of this problem was as follow
- Click on the down arrow right at the menu option Start (in the standard bar)

Compat:
Windows Compatibility tab, Now select this option “Disable Desktop Composition”..
- Select Browse with…
- Select Internal Web Browser


Compatibility tab, Now select this option “Disable Desktop Composition”..

Auto Updates
Hey guys, if none of these work for you try “Tools”-”Options”-”Enviroments and Updates” and disable “Automatically Check for Updates.

Default Item
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0 –> DefaultOpenProjItemLocation. Check this registry key.


CPU Tuning:
most CPU time used by method:
Features.FindVsixVersion from System.Core.ni.dll
full chain: mscorlib.ni.dll -> IsEnabled -> ShouldEnable -> FindVsixVersion
tried remove old vs 2012 and 2013 same pure perfomance…
http://answers.microsoft.com/en-us/windows/forum/windows_10-other_settings/vs-2015-comunity-pure-startup-time/fd8b196d-1465-4339-9735-1c9604eacfe6



Logging:
Add the following values to

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion
Add:
DWORD ForceLog set value to 1
DWORD LogFailures set value to 1
DWORD LogResourceBinds set value to 1
DWORD EnableLog set value to 1
String LogPath set value to folder for logs (e.g. C:\FusionLog\)


OR:

'll find the "Fusion Log Viewer" under Microsoft SDK\Tools (just type "Fusion" in the start menu on Vista or Windows 7/8). Launch it, click the Settings button, and select "Log bind failure" or "Log all binds".

Friday, February 26, 2016

OpenDNS on Linksys Routers SmartWIFI, How to make it just WORK!

If you have a SmartWIFI router version like me , then you most likely are not able to use OpenDNS the nice and easy way as shown here from OpenDNS: Configuration for Linksys Cisco AC175, EA6300, EA6500, ..... 
Well guess what these directions don't tell you HOW to fix the KNOWN issue (or the as designed feature that breaks OpenDNS) with Linksys SmartWifi and OpenDNS not working together. Instead the article just states: "If the OpenDNSn addresses are not displaying here, the solution at this time is to roll back the firmware..." Well that is a flash in the pan hail mary to solve this issue, because barely anyone has gotten success from rolling back the firmware! 

 Well I wasn't about to rollback without a fight, so I looked around theses forums and found that many people also cant get OpenDNS to work with SmartWifi EA* routers,  people tried everything from firmware, varying DNS entries, StaticIP assignments, ect ect. Of course most forum posts were little on detail and damn near none had screenshots.  Both the Linksys and OpenDNS communities lack a consistent documented work-around anywhere, just message board blurbs, and that is what I had to start with, from these I found a solution:

Some of the message board links describing the SmartWIFI LinkSys routers/DNS issues:
EA6350 DHCP/DNS Issues
OpenDNS Testing

So using these message board blurbs (and many more) from various sources and around 4 hours of my life I finally got a solution so I can use my EA6350 LinkSys Router to work with OpenDNS!!!

PreReq: My firmware version is: 1.0.4.164719
1. Set IPv4 connection
2. Set Local Network DNS's
3. Disable IPv6











Related Links

1. OpenDNS FamilyShield
Preferred DNS Server: 208.67.222.123
Alternate DNS Server: 208.67.220.123

2. BrightHouse DNS Servers