Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Need Help with Merge Statement

Author  Topic 

7siva7
Starting Member

28 Posts

Posted - 2011-10-17 : 09:07:24
Hello All,

I wrote one code by using Cursor concept
but it is slow when i use 10 million records
so i want it to convert "using Merge Statement"
can any one help me
here is the code

Edited

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-17 : 09:17:49
so you're trying to populate two destination tables based on a common source table? if yes, you might have to create view involving them for doing the same through merge. why dont you use normal upserts instead?


DECLARE @INSERTED_COUNTRIES table
(
country varchar(100),
country_id int
)

INSERT INTO DIM_COUNTRY
OUTPUT INSERTED.country,INSERTED.country_id INTO @INSERTED_COUNTRIES
SELECT
Usage,country,region,ownership,enddate,users,sbu,sbg,sbe,buildingOrigin
FROM
SAMPLE s
LEFT JOIN DIM_COUNTRY c
ON c.country = s.country
WHERE c.country IS NULL


INSERT INTO FCT_DETAILS
SELECT ic.country_id,s.lid,s.Name,s.address,s.city,s.state,s.[area in sqft],s.latitude,s.longitude
FROM
SAMPLE s
JOIN @INSERTED_COUNTRIES ic
ON ic.country=s.country
LEFT JOIN FCT_DETAILS fd
ON fd.lid = s.lid
WHERE fd.lid IS NULL


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-10-17 : 12:39:48
Why do you need a MERGE statement when you are only doing INSERTs? Are you just investigating using a merge statement?
Ripping off some of Visakh's code, you can also do soemthing like this:
-- Insert Dimension
INSERT -- DISTINCT??
DIM_COUNTRY
SELECT
s.Usage,
s.country,
s.region,
s.ownership,
s.enddate,
s.users,
s.sbu,
s.sbg,
s.sbe,
s.buildingOrigin
FROM
SAMPLE AS s
LEFT JOIN
DIM_COUNTRY AS c
ON c.country = s.country
WHERE
c.country IS NULL


-- Insert Fact
INSERT
FCT_DETAILS
SELECT --DISTINCT??
c.country_id,
s.lid,
s.Name,
s.address,
s.city,
s.state,
s.[area in sqft],
s.latitude,
s.longitude
FROM
SAMPLE AS s
INNER JOIN
DIM_COUNTRY AS c
ON c.country = s.country
LEFT OUTER JOIN
FCT_DETAILS fd
ON fd.lid = s.lid
WHERE
fd.lid IS NULL
I hate to ask, but facts and dimensions.. Why would you use this flawed concept? Hopefuly, this is not a data warehouse.
Go to Top of Page

7siva7
Starting Member

28 Posts

Posted - 2011-10-17 : 23:32:15
I have one excel file which countain the following fields

lid Name Usage address city state country region area in sqft ownership enddate users latitude longitude sbu sbg sbe buildingOrigin


I want to Generate and Populate DIM_Country and FCT_Details using Surrogate keys


can you please help with data wareHousing Concepts?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-18 : 00:27:48
isnt that what we provided in solution above? didnt understand what else you're expecting

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

7siva7
Starting Member

28 Posts

Posted - 2011-10-18 : 00:49:31
is this the correct and Best way to use in datawarehousing?

im asking becoz i am new to datawarehousing

HI
Go to Top of Page

7siva7
Starting Member

28 Posts

Posted - 2011-10-18 : 01:08:08
can you tell me what you used in the sample code you wrote?

HI
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-18 : 01:59:25
quote:
Originally posted by 7siva7

can you tell me what you used in the sample code you wrote?

HI


do you mean you cant read the posted code?
didnt understand your question



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -