Solved

SQL Syntax Populate all sites all items

Posted on 2014-02-08
6
178 Views
Last Modified: 2014-02-09
I have a situation where I am trying to protect users from themselves. In this case there are three tables involved:
1) Site table - PK - LOCNCODE
2) Item table - PK - ITEMNMBR
3) Item/Site table - PK - ITEMNMBR,LOCNCODE

I am looking to convert my old script that uses cursors to one that does not. I need to schedule a script to run every hour that insures that the Item/Site table always has a record for every item at every site. So let's assume my site table is called SITEMSTR, the item table is called ITEMMSTR and the item/Site table is called ITEMSITE.

The outer loop is a select for every item
The inner loop is a select for every site
Inside the loop it has to look for the item/site combination in the item/site table and if a record does not exist, create it using the current values for item and site.
0
Comment
Question by:rwheeler23
  • 4
6 Comments
 

Author Comment

by:rwheeler23
ID: 39844683
I forgot to mention we are currently using SQL Server 2008 R2.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39844842
In a word:  Why?

Why are you relying on an hourly job?  Why not fix your code so that it does not happen?  If you cannot find a way then add a TRIGGER on the last table to be added to check for this situation and INSERT if necessary.  If you are not sure which is the last table add a TRIGGER on both.
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39844849
you can do that with the below code

;WITH C AS
(
SELECT distinct I.ITEMNMBR,S.LOCNCODE
FROM  ITEMMSTR  I
CROSS JOIN SITEMSTR S
)
INSERT INTO ITEMSITE
SELECT * FROM C
WHERE NOT EXISTS ( SELECT 1 FROM ITEMSITE IS WHERE IS.ITEMNMBR = C.ITEMNMBR and IS.LOCNCODE = C.LOCNCODE )

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:rwheeler23
ID: 39845258
The reason why is that I have no control over when new sites or items are being added and I have grown tired of getting the frantic call that a part could not be ordered for a site because the item/site combination does not exist. The application is an ERP solution and we are not allowed to make any alternations to any table. Not to mention I would need two triggers. One when a new item is added and another when a new site is added. These would be two different tables. The snippet of code provided by Surendra gives me the idea of the cross join which will provide the solution.
0
 

Author Comment

by:rwheeler23
ID: 39845264
I should also add this company is using this ERP solution in a non-standard way. This ERP solution is from Microsoft and we have no access to the code. If I did then I would fix the code or at the very least add the triggers. Both of these were good suggestions.
0
 

Author Closing Comment

by:rwheeler23
ID: 39845265
Thanks
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now