Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Syntax Populate all sites all items

Posted on 2014-02-08
6
Medium Priority
?
190 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

609 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