Monday, April 8, 2013

[T-SQL] Inserting missing records using multiple fields as the key

[T-SQL] Inserting missing records using multiple fields as the key


Inserting missing records using multiple fields as the key

Posted: 07 Apr 2013 10:00 PM PDT

Hi,I have a complex problem that I'm pretty sure should have a very simple solution. If I could just find it.I am trying to import data from a flat file aka Raw_Build_List:Contract Description varchar(30)Section Description varchar(25)Item Description varchar(30)Quantity integerI am trying to write a script that will pull it into a relational database to use with other data. The script will have to run every night. The Raw data may contain records processed before.I have 4 tables:Contracts (ID, Contract_Description, etc), Sections (ID, Section, etc), Items (ID, Description, Category, etc) and Build_list (ContractID, SectionID, ItemID, Qty, StartDate, etc)To import the data I do multiple steps.1. Add 3 extra fields to Raw_Build_List: ContractID, SectionID and ItemID.2. I run a conditional insert into each table (Contracts, Sections and Items) using a group by eg.[code="sql"]INSERT INTO Contracts (Contract_Description) SELECT ContractDesctiption GROUP BY ContractDesctiption WHERE ContractDescription NOT IN (SELECT Contract_Description FROM Contracts)[/code]3. Then using the IDs generated by the Identity Fields I update the new fields in the Raw_Build_List table. eg.[code="sql"]UPDATE Raw_Build_List SET ContractID = ID FROM Contracts LEFT OUTER JOIN Raw_Build_List ON Contracts.Contract_Description = Raw_Build_List.ContractDesctiption[/code]4. Now in the Raw_Build_List I have all the key fields against all the correct descriptions and I want to put all those keys into the Build_list table and not create duplicates.[b]Is there a non-[u]RBAR[/u] way of doing this?[/b]The one way I thought may work is to string all the IDs together and compare them to the string of all the IDs in the other table and it does work but as expected is really slow. [code="sql"]RIGHT('00000000'+LTRIM(RTRIM(ContractID))+RIGHT('00000000'+LTRIM(RTRIM(SectionID))+RIGHT('00000000'+LTRIM(RTRIM(ItemID))[/code]NOTE: I had to pad it as Contract=1, Section=24 and Item=45 (joined = 12445) will match up with Contract=12, Section=44 and Item 5 (joined = 12445).[i]There has to be a better way of doing this...:blink:[/i]

T-SQL Variable vs String Equivalent

Posted: 08 Apr 2013 12:47 AM PDT

Got a question with some coding standards. There are developers declaring a variable to a constant value and using that in multiple places in a stored proc rather that using the string equivalent. For example:[code="sql"]DECLARE @Yes VARCHAR(3) = 'Yes'SELECT [pk_value] ,[field_value] FROM [tempdb].[dbo].[table7] WHERE [field_value] = @Yes; [/code]rather than:[code="sql"]SELECT [pk_value] ,[field_value] FROM [tempdb].[dbo].[table7] WHERE [field_value] = 'Yes';[/code] I setup a quick and dirty example and ended up with the example below, the same execution plan was generated, but query using the variable was slower, using more CPU.[img]http://s20.postimg.org/o4by92ex9/Untitled.png[/img]Is there any scenario out there where a variable would be faster than the string equivalent?Thanks,Kevin

[SQL Server] Efficiently Reuse Gaps in an Identity Column



Weekly SQL Server newsletter with articles, forum posts, and blog posts via email. Subscribers receive our white paper with performance tips for developers.



SQLTeam.com Articles via RSS


SQLTeam.com Weblog via RSS



.

sqlteam.com

[SQL Server] Using REPLACE in an UPDATE statement



Weekly SQL Server newsletter with articles, forum posts, and blog posts via email. Subscribers receive our white paper with performance tips for developers.



SQLTeam.com Articles via RSS


SQLTeam.com Weblog via RSS



.

sqlteam.com

[SQL Server] Handling SQL Server Errors



Weekly SQL Server newsletter with articles, forum posts, and blog posts via email. Subscribers receive our white paper with performance tips for developers.



SQLTeam.com Articles via RSS


SQLTeam.com Weblog via RSS



.

sqlteam.com

[SQL Server] Using Dynamic SQL in Stored Procedures



Weekly SQL Server newsletter with articles, forum posts, and blog posts via email. Subscribers receive our white paper with performance tips for developers.



SQLTeam.com Articles via RSS


SQLTeam.com Weblog via RSS



.

sqlteam.com

[SQL Server Data Warehousing] Pricing of PDW Appliance


Hi All:


I have been searching hard for the pricing & details of SQL SERVER 2012 PDW Appliance (both Dell and HP) but got nothing. Can anyone show me where i can find some relevant info?


Cheers



.

social.technet.microsoft.com/Forums

[SQL server issues]Created Issue: SQL Server 2008 Developer edition sample database install error [18174]







description



I'm trying to install Adventureworks sample dB and i get an error.
I successfully downloaded the mdf file however when attach, add and click OK from SSMS I get the error:
 
Msg 5172, Level 16, State 15, Line 1
The header for file 'C:\Program Files\Microsoft SQL Server\AdventureWorks2012_Data.mdf' is not a valid database file header. The FILE SIZE property is incorrect.







.

sqlserversamples.codeplex.com

Search This Blog