Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14353

[SOLVED] SQL update using OUTPUT clause

$
0
0
I'm trying to use the OUTPUT clause here https://msdn.microsoft.com/en-us/library/ms177564.aspx

What I'm doing is updating a column that contains a key for a record.  So say the key is 10.  I need to update it to 11 and return that 10.  This can be accomplished by using a simple update query but codeigniter isn't liking it and won't return any results even though it does complete.

This query does not return any results and I get an error trying to use result_array(), result(), or row()
Code:
UPDATE
  KEY_COUNTERS
SET
  KEYS_COUNTERS = KEYS_COUNTERS + 1
OUTPUT
  deleted.KEYS_COUNTERS
WHERE
  KEY_COUNTERS_ID = 'KEY'

This query runs as well and I can run result_array() and get the deleted key but I don't want to have to run a select and then update.
Code:
DECLARE @KEY INT;

SELECT
  @KEY = KEYS_COUNTERS+1
FROM
  KEY_COUNTERS
WHERE
  KEY_COUNTERS_ID = 'KEY'

UPDATE
  KEY_COUNTERS
SET
  KEYS_COUNTERS = @KEY
OUTPUT
  deleted.KEYS_COUNTERS
WHERE
  KEY_COUNTERS_ID = 'KEY'

Can anyone tell me why the first query doesn't return results but the bottom one does.  If I run this query in SQL management studio I get the same result so only codeigniter isn't working.  The only thing I can think is that since the first query only has the Update query that it doesn't return results because of codeigniter.

Viewing all articles
Browse latest Browse all 14353

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>