Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>this</p> <pre><code>UPDATE obp.Contacts SET URL = ( SELECT url from obp.URL WHERE id = @NextURLId) </code></pre> <p>updates <em>every</em> row with the same. Add a proper WHERE clause like</p> <pre><code>WHERE contact_id=@varContact_id </code></pre> <p>About the requirement for this: I understand that you want to associate a Contact with a URL and that there is no logical rule for which with what. At first sight I would consider a match table the right way to do this. It feels better to me to put such associations into a seperate table, even if there is a strong belief in a 1:1-relationship between the two objects associated. obp.URL and obp.Contacts are dimensional tables (I assume/hope). Keeping the association in one different table requires one action if changes occur. In your model a change must be reflected in both those tables.</p> <p>Here is an idea for such a table:</p> <pre><code>create table Contact_URL_match (ID int identity (1,1) ,URL_id int not null unique ,contact_id int not null unique ,created datetime) </code></pre> <p>the unique constraints disallow insertion of the same URL or the same Contact_id twice. On each insert/update prior content is being checked for duplicates and if found the action is denied, thus uniqueness protected.</p> <p>For manifesting new matches in a first large initial action try this (haven't tested, just an idea)</p> <pre><code>INSERT INTO Contact_URL_match (URL_id ,contact_id ,created) SELECT urls.id ,contacts.contact_id ,getdate() FROM (SELECT DISTINCT(contact_id) ,ROW_NUMBER() over (ORDER BY contact_id asc) rn FROM obp.Contacts) contacts INNER JOIN (SELECT id ,ROW_NUMBER() over (ORDER BY id) rn FROM obp.URL) urls ON contacts.rn=urls.rn </code></pre> <p>Within the subqueries this creates a row number in both the source tables based on the ORDER BY clauses. It then joins the resultsets of the subqueries by that rownumber which is an act of deliberate randomness. I hope you want that. The result of that join is inserted into the match table.</p> <p>If later you want to manifest a single new association you could add WHERE clauses to the subqueries that specify what URL you want matched with what Contact. Before picking a URL or Contact check the match table with NOT EXISTS to make sure it is not used in there.</p> <p><strong>EDIT :</strong> syntax errors cleaned</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload