title: Sybase select variable logic
body: <p>Ok, I have a question relating to an issue I've previously had. I know how to fix it, but we are having problems trying to reproduce the error.</p>
  
  <p>We have a series of procedures that create records based on other records. The records are linked to the primary record by way of a <code>link_id</code>. In a procedure that grabs this <code>link_id</code>, the query is</p>
  
  <pre><code>select @p_link_id = id --of the parent
  from  table
  where thingy_id = (blah)
  </code></pre>
  
  <p>Now, there are multiple rows in the table for the activity. Some can be cancelled. The code I have doesn't disinclude cancelled rows in the select statement, so if there are previously cancelled rows, those ids will appear in the select. There is always going to be one 'open' record that is selected if I disinclude cancelled rows. (append <code>where status != 'C'</code>)</p>
  
  <p>This solves this issue. However, I need to be able to reproduce the issue in our development environment.</p>
  
  <p>I've gone through a process where I've entered a whole heap of data, opening, cancelling, etc to try and get this select statement to return an invalid id. However, whenever I run the select, the ids are in order (sequence generated), but in the case where this error occured, the select statement returned what seems to be the first value into the variable.</p>
  
  <p>For example.</p>
  
  <pre><code>ID   Status
  1    Cancelled
  2    Cancelled
  3    Cancelled
  4    Open
  </code></pre>
  
  <p>Given the above, if I do a select for the ID I want, I want to get '4'. In the error, the result is 1. However, even if I enter in 10 cancelled records, I still get the last one in the select.</p>
  
  <p>In oracle, I know that if you select into a variable and more than one record is returned, you get an error (I think). Sybase apparently can assign multiple values into a variable without erroring.</p>
  
  <p>I'm thinking that there's either something to do with how the data is selected from the table, where the id's without a sort order don't return in ascending order, or there's a dboption where a select into a variable will save the first or last value queried.</p>
  
  <p>Edit: it looks like we can reproduce this error by rolling back stored procedure changes. However, the procs don't go anywhere near this link_id column. Is it possible that changes to the database architecture could break an index or something?</p>