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 2005 Forums
 Transact-SQL (2005)
 I'm Stuck!!!

Author  Topic 

rcr69er
Constraint Violating Yak Guru

327 Posts

Posted - 2008-08-17 : 14:10:54
Hi Guys

I am having trouble making a stored procedure with the following scenario.

I have two tables, WebStock and ProductVariant. Shown below with sample data:

(please ignore the links, just used for example data)

Webstock:
WebID, InternetOnly, InternetAddress
1,Yes,www.sample.com
2,Yes,www.sample.com
3,No,,
4,No,www.sample.com
5.No,,
6. No,www.sample.com
7. No,www.sample.com


ProductVariant
VariantID, ProductID,FormatID,InternetAddress
1,1,3,www.sample.com
2,2,3,www.sample.com
3,3,1,,
4,4,1,www.sample.com
5,4,3,www.sample.com
6,5,1,,
7,6,1,www.sample.com
8,6,3,www.sample.com
9,7,3,www.sample.com
10,7,3,www.sample.com

The WebID in the Webstock table is linked to the ProductID in the Product Variant table.

The Webstock table is a form of temp table where the table is truncated and data is imported to and then transferred to the ProductVariant table.

When the item in the Webstock table has an InternetOnly value of NO and no value in the Internet it is entered into the ProductVariant table with a FormatID value of 1.

When the item in the Webstock table has an InternetOnly value of YES and HAS a value in the InternetAddress it is entered into the ProductVariant table with a FormatID value of 3.

When the item in the Webstock table has an InternetOnly value of No and HAS a value in the InternetAddress it is entered into the ProductVariant table twice one with with a FormatID value of 1 and the other with a FormatID value of 3.




The thing I am stuck on is updating.

The Webstock table may contain updated information on a product meaning that the ProductVariant table will have to be amended.

Here is an example:

The current import table shows that the product was previously entered with an InternetOnly value of NO and the InternetAddress had a value (I am checking this using LEN(InternetAddress)>1). This would mean that the record would be entered into the ProductVariant twice but with different FormatIDs, these being 1 and 3. The new import file now stated that the product now had an InternetOnly value of YES and the InternetAddress had a value. The action that would have to take place is Update the EmailAddress and Delete the record for the product where the FormatID is equal to 1.

I am trying to put this into a procedure but not sure how, I was thinking of using an IF statement but not sure how to.

There are 3 conditions that I am looking at, these being:

1:
InternetOnly is YES
Internet Address contains a value
Previous InternetOnly was NO
Previous InternetAddress contains a value

Action to ProductVariant Table:
Update EmailAddress
Delete the record for the product where the FormatID is 1.

2:
InternetOnly is YES
Internet Address contains a value
Previous InternetOnly was NO
Previous InternetAddress contains NO VALUE

Action to ProductVariant Table:
Update EmailAddress
Delete the record for the product where the FormatID is 1.
Insert a record for the product with a FormatID equal to 3.

3:
InternetOnly is NO
Internet Address contains NO VALUE
Previous InternetOnly was NO
Previous InternetAddress contains a value

Action to ProductVariant Table:
Update EmailAddress
Delete the record for the product where the FormatID is 3.
Insert a record for the product with a FormatID equal to 1.

I hope this make sense!!!

Is there a way of doing all the above in one procedure?

Thanking you in advance!!!




visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-18 : 00:35:52
How are you planning to do this? will you be trying to do this updation periodically or whenever data in webstock table changes?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-18 : 00:42:17
Also do you have an audit/ history table? Does your Webstock table have an audit column like datecreate,lastmodified...?
Go to Top of Page

rcr69er
Constraint Violating Yak Guru

327 Posts

Posted - 2008-08-18 : 06:32:26
Hi

I'm am planning to update it whenever a request comes over, so it can come at different times.

Also there is no audit type column such as datecreated.

I hope this helps!!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-18 : 06:44:14
quote:
Originally posted by rcr69er

Hi

I'm am planning to update it whenever a request comes over, so it can come at different times.

Also there is no audit type column such as datecreated.
I hope this helps!!!


Then how will you identify the latest inserted records of WebStock?
Go to Top of Page

rcr69er
Constraint Violating Yak Guru

327 Posts

Posted - 2008-08-18 : 12:20:22
Hi

I found an easily solution. Just truncate the ProductVariant table and import the data again each time! :D
Go to Top of Page
   

- Advertisement -