Sorry for the length of this, I'm trying to give a lot of info to avoid non-relevant solutions.
My goal is to embed at least 1 UPDATE statement into a SELECT statement so that I have a chance to update some computed values at the instant before the select statement runs (think of it like a TRIGGER on SELECT). VIEW is not in immediate solution, since I'm constrained by the system I'm using (see below).
I'm customizing a 3rd party commerical ERP that is weak on features (system will remain nameless! -- upshot is you haven't heard of it, but its not home grown either). It has a canned query facility where I can use a text/GUI to build a SELECT query. Once I save a query, the users can click on the query to execute it and see the results. ERP runs atop MS SQL Server 2000; version upgrade is NOT in the cards right now. I can write whatever feature set I need outside of the ERP in whatever language I want, I have done this in the past, if the features warrant it. But my user community finds it easier when my customizations can be done in the ERP system.
The query can be arbitrarily complex, but the ERP package builds the SQL Select statement itself. Internal to the compiled ERP is something like this (this is just a guess!):
"SELECT " + fieldList + " FROM " + tableListAndJoins + " WHERE " + whereCond
The GUI builder helps novice users build the fieldList and so on but you can bypass it and write the clauses in text as long as the SQL is valid when combined as above.
I can't seem to find an incantation to run a stored procedure as a side effect of a SELECT statement, whether it's in the select clause, where clause, etc. I really don't care how I jailbreak the system -- a stable SQL injection attack would be fine, as long as it didn't mean I had to modify the security of underlying sql server itself. I've looked at UDFs, but you can't put an UPDATE statement into a scalar UDF, and it doesn't make sense to try to modify the return of a table UDF (or does it?). If you can UPDATE from within a VIEW then I would like to see an example, but I realize I can use a VIEW to compute columns and that is not the solution I am looking for. I read a suggestive statement online about being able to use some sort of XP_ to accomplish this, but as to which XP_ or how to do it, I don't know.
this question is NOT a solution, in and of itself: Updating a table within a select statement
The UPDATE from SELECT query structure is the main technique for performing these updates. An UPDATE query is used to change an existing row or rows in the database. UPDATE queries can change all tables' rows, or we can limit the update statement affects for certain rows with the help of the WHERE clause.
Here is another useful syntax: UPDATE suppliers SET supplier_name = (SELECT customers.name FROM customers WHERE customers. customer_id = suppliers. supplier_id) WHERE EXISTS (SELECT customers.name FROM customers WHERE customers.
You can't. There's no convention in a SQL UPDATE statement for returning data. And vice versa — a SELECT statement doesn't write information to a table.
The FOR UPDATE clause is an optional part of a SELECT statement. Cursors are read-only by default. The FOR UPDATE clause specifies that the cursor should be updatable, and enforces a check during compilation that the SELECT statement meets the requirements for an updatable cursor.
I can't think of any way to combine a SELECT with an UPDATE in SQL 2000 (though in 2005 and up, the OUTPUT clause is available). However, it looks like you get three string values (fieldList, tableListAndJoins, whereCond) that get concatenated together with "SELECT", "FROM" and "WHERE", and assuming they don't do some serious SQL injenction-like code detection, you might be able to kludge together something like this:
fileList = "NULL where 1 = 0; UPDATE MyTable set MyColumn = 'Whatever' where SomeColumn = 'Criteria'; SELECT MyColumn"
tableListAndJoins = "MyTable"
whereCond = "SomeColumn = 'Criteria'"
[The semicolons are actually optional, and might not even work in SQL 2000 -- they just make it clear where one command ends and the next begins.]
The downside of this is that you'll get back two data sets. The first will be an empty one-column set (alias that NULL if you want a name to the column), and the data you want will be in the second set. Other work-arounds might be possible, depending on how these three values are used and how errors are caught. (Let that first query generate and error, and hope the update and the second query go through?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With