update [database].[table]
set
[fieldalpha] = REPLACE(fieldAlpha,
'/c/', '/c/[variable]')
where [fieldbravo] like "[variable]%";
So this code works and does what I need it to do. However I have a substantial number of variables that need to be applied to about 50k records. Is there a way to loop through this code with a list of replacements for [variable]?
Related
Say I have multiple tables in {game} like {bullets}, where {bullets} has multiple tables as found below. How would I iterate through and call all the update functions contained in {game}?
--Below is a simplified example, Assume each table in {bullets} has multiple entries not just update. And that the final code must work in cases like game={bullets,coins,whatever}, each entry being of similar nature to bullets.
game={}
game.bullets={{update=function(self) end,...}, {update=function(self) end,...},...}
for obj in all(game) do
for things in all(obj) do
things:update() end end
--I'm not sure what I"m doing wrong and whether I even need a double for-loop.
--if bullets wasn't embedded in {game} it would just be:
for obj in all(bullets) do
obj:update()
end
I've also tried:
for obj in all(game.bullets) do
obj:update()
end
*correction: this works, the problem I want solved though is to make this work if I have multiple tables like {bullets} in {game}. Thus the first attempt at double iterations which failed. So rather than repeat the above as many times as I have items in {game}, I want to type a single statement.
all() isn't a standard function in Lua. Is that a helper function you found somewhere?
Hard to tell without seeing more examples, or documentation showing how it's used, with expected return values. Seems to be an iterator, similar in nature to pairs(). Possibly something like this:
for key, value in pairs( game ) do
for obj in all( value ) do
obj :update()
end
end
I have a procedure on MySQL that returns two results and I need to show this on Delphi but I didn't find how to pass for each result.
Here is how appear on DBForge when I execute, I want this on delphi too, show Query1 and Query2 in a TTabControl.
How to go through this queries and get the name of the query like: Query1,Query2?
You don't say what DB interface lib you're using, like FireDAC, Zeos, or something else.
You're going to issue something like a dbxyz.ExecSQL() call and check for some results.
It sounds like you're expecting to get back multiple records in the result set. Using a TTabControl, you'd simply create a list of tabs like "Result-1", "Result-2", and so on, depending on how many records you get back. (They're results, not queries.) You could select the first one by default.
When another tab is clicked, use the control's TabIndex property to select the corresponding result from the result set, then format up the data in that result and display it in whatever format you're using below the tabs.
There's so little detail you've given that it's impossible to show any code for anything other than the tab control's OnChange handler that will use the TabIndex property to find the desired result set. But this is the overall approach I'd take for this using the TTabControl.
I solve the problem with the command
var
tab: TTabItem;
stringGrid: TStringGrid;
repeat
tab:= TTabItem.Create(nil);
tab.Parent:= tabcontrol1;
tab.Text:= query.Fields.Fields[0].FieldName; //the table name
stringGrid:= TStringGrid.Create(Self);
stringGrid.Parent:= tab;
stringGrid.Align:= TAlignLayout.Client;
for I := 1 to query.FieldCount-1 do
begin
stringGrid.AddObject(TStringColumn.Create(stringGrid));
stringGrid.Columns[i-1].Header:= query.Fields.Fields[i].FieldName;
query.First;
for j := 0 to query.RecordCount-1 do
begin
stringGrid.cells[i-1, j]:= query.Fields.Fields[i].value;
query.Next;
end;
end;
stringGrid.RowCount:= j;
until not query.openNext;
I am using below code to get records with specified condition, and then to update only the same records.
$this->db->where('Parameter1', 'TRUE');
$query = $this->db->get('Messages');
$this->db->where('Parameter1', 'TRUE');
$this->db->set('Parameter1', 'FALSE');
$this->db->update('Messages');
This works, but calling two times the same query using where() command seems like wasting of server power. Is it possible to make get() command not reset query or to use the previous record set in the update command?
I doubt this is something you really need to worry about taking up too many resources, and you can't really reuse the where clause in the actual sql query. But if you'd like you can refactor to get slightly cleaner code.
$unread = array('Parameter1'=>TRUE);
$read = array('Parameter1'=> FALSE);
$query = $this->db->get_where('Messages', $unread);
$this->db->update('Messages', $read, $unread);
Note:
In your code your getting every element where Parameter1 is set to true, and then changing every one of those elements to false. This almost certainly is not desirable, but perhaps it is a problem you take care of somewhere else in your real application.
I am trying to find a way to ensure my database query is ran as part of the for loop shown below. I don't know if async.series is the best way, or if fibers might work? Rather than paste loads of code, in pseudo code it looks like this:
for length of array
newArray = array.split
databaseQuery(select **** from *** where x = newArray[0] and y = newArray[1]
my problem is as node is asynchronous, it simply repeats the for loop split while the query is running and then gets the result of the query, meaning I only get the last result returned.
Is there a way to ensure the database query is executed each time in the for loop? I've used nested queries, and callbacks in the past but I can't seem to figure out the best way to call the query each time for the loop
If I have the following query, is it possible to be able to run a function inside? Let's say I want to add WHERE zip_code = user_distance(zip_code)?
I want to take data from each row and run it through a function before actually selecting it.
#posts = Listing.find_by_sql(["SELECT * FROM listings WHERE industry = ? && ", current_user.industry])
If you are mainly looking to get this working and not worrying so much about performance (because going straight to the SQL is faster than going through ActiveRecord) then you could do:
listings = []
Listing.all.each do |listing|
listings << listing if user_distance(listing.zip_code)
end
So, it will go through each listing and add it to that array if the user_distance method returns true (or however it is set up).
Another thing you could do is set up a stored procedure ("stored proc") on your database that takes in a zip code and returns what it is you want (i.e, does the same thing as user_distance), and that user defined variable max_distance could be in a database table so it's accessible to your stored procedure. Then you could call that stored proc from the SQL and still be able to pass in the zip_code of each row.