Solved

SQL Syntax Populate all sites all items

Posted on 2014-02-08
6
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

687 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