Solved

SQL Syntax Populate all sites all items

Posted on 2014-02-08
6
173 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

744 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

12 Experts available now in Live!

Get 1:1 Help Now