MySQL Trigger - Finding column names dynamically - mysql

When a table row is updated , I am getting the changed row as NEW object.
I need to get all column from this NEW row.
Is there a way to get all the column from NEW object, or I should manually access all the columns like NEW.id, NEW.name.
If a new column is introduced into the table for which I am writing the trigger, I need to manually come and add this column in my trigger.
Kindly please suggest me any ways to dynamically access all the columns from the row.

Related

MS-Access chart created from a cross tab query displays a numerical ID instead of text value

I am trying to create a chart from a cross-tab query that I have made. The row values of the chart are actually from a lookup field so instead of the actual value of the cell it shows the unique ID. I was wondering how you could make it show the actual value of the cell.
Thank you so much!
I am fairly new to using Microsoft access and haven't had any formal training except trying things here and there from an existing access database. So please excuse me if I seem clueless and need your help.
Make a query that is the join of your key table to the look up table. Save that with a name.
Then recreate your cross table query and instead of using the key table as its source, use your saved query. You will then be able to use the correct field in the rows.

Google Sheets Pivot Table Not Updating

I have a google app script which submits info to an organized sheet and would like to create a pivot table with the all information in the sheet. I can do this, but whenever I submit a new row of data to the sheet, it is not automatically included in the pivot table. I have to manually change the range of the pivot table every time I submit a new row of data. Is there any way I can make the sheet/pivot table automatically include the new row of data?
I worked around this issue by only specifying the column range.
For example, if you have row data in columns A to F, set the range of the pivot table to SHEET!A:F
If you add rows now, the new data in those columns will be added to the pivot table.
There is a way. I did exactly that 3 years ago on Sheets.
If you are submitting the new rows using Google Forms, then there is no way.
If you try to programmatically update the range, there is no way either because named ranges need to be deleted then re-added, causing #REF on the pivot.
Now for the good part: If you are adding the new row with a script, do not append it to the end. Instead, keep a fake row at the end (with zero values so pivot is not affected) and insert the new row just before it. Your range (named or not) will update.
Even for the Google Forms case, you might get it to work by pre-adding all blank rows to the response sheet and make the named range include the blank ones too.
I encountered the same problem with the Google Sheet linked to a form. How I solved it:
Edit the Pivot table range to include the empty row after the last row in your sheet.
If your pivot table data range is 'Sheet Name'!A1:S100, change it to 'Sheet Name'!A1:S101.
Add a filter to your pivot table for a column (present in the data range) that will never be blank and set the condition for this column - 'Is not empty'
If you have a column 'Timestamp' (and you're sure it will never be empty), then add this column to the filter, select 'filter by condition' and set the condition to 'Is not empty'.
These steps will filter out the last (empty) row. When you will add a new row to your data, the pivot table will update automatically and you won't have to update the data range again.
In my case what was interfering were the fields in the Pivot's Filters section (highlighted in red below).
Make sure you test without any filters... see if it works. Then add your filters one by one and play with the pivot. Probably the filter(s) is\are influencing the pivot to not update.

How can I see the final MySQL column value in a trigger even if it wasn't part of the update data?

How can I get the final value of a column in an AFTER UPDATE trigger even if I didn't update that specific column? Will NEW.columnname always have final value for that column?
If not, how can I get the final value?
On the MySQL Trigger Manual Page
it says the following, which, to me is inconclusive:
In an UPDATE trigger, you can use OLD.col_name to refer to the columns of a row before it is updated and NEW.col_name to refer to the columns of the row after it is updated.
Does NEW refer to ALL the data of the row or just the submitted data?
Does NEW refer to ALL the data of the row or just the submitted data?
Yes it (documentation) refers to all columns of a row that is being updated. Therefore you can safely use NEW keyword to address any columns, not only those that you actually updated.
Here is SQLFiddle demo. Although only col2 was updated all other columns were accessible.

SQL Update with Variable as Parameter

I am working on a stored procedure in MySQL to update a row in a SQL table. What I have is a table where several of the columns are named incrementally. EX: Page_1, Page_2, Page_3....etc.
The data stored in these locations is updated at different times and I have another column to store the number of times the row has been updated. The count variable gets incremented each time the procedure runs and that allows me to utilize it's value in keeping track of where the next update of the data should take place.
From my research I keep finding solutions utilizing "Dynamic SQL." I do not understand how to utilize this to resolve my issue.
I want to pass a variable in to an update statement as the column name.
The code I currently have is as follows.
SET COUNT = COUNT + 1; -- modify count from count column
SET COLUMNLOCATIONVARIABLE = CONCAT('Page_' , COUNT); -- concatenate the count with the column "Prefix"
UPDATE Table
SET COLUMNLOCATIONVARIABLE = INPUTVARIABLE --Use the concatenated statement as the column name and update it with input data
WHERE mainID = INPUTID;
If anyone could explain to me how to pass this variable in as a column name using Dynamic SQL or another solution utilizing non-dynamic SQL I would appreciate it.
Thanks in advance.
This isn't the way to use a database. Create a new table for your Page_* columns, linked to the main table by an identity, with a page number column. Then you can update and include as many pages as you need, addressed by the main identity and the page number.

How do I copy a row in a table while defining the PK of the new row?

I want to copy or duplicate a row in a table (tblSpills) while defining the the PK column values (2) (tblSpills.Year, tblSpills.Complaint).
I already have a stored procedure that inserts a new row with the custom PK values and every other columns (about 30) is empty. So far I have gotten as far as executing the existing stored procedure to create the new row and grabbed the new PK values as variables for the new Duplicate stored procedure, but I cannot figure out how to copy the remaining columns from one row to the newly created row in the same table.
Thank you for any help you can provide.
You're not telling us anything about what system / database you're using - but just guessing if it should be SQL Server, you could do something like this:
INSERT INTO dbo.tblSpills(Year, Complaint, list all other columns here)
SELECT
#YearValue, #ComplaintValue,
(list of all other columns here)
FROM
dbo.tblSpills
WHERE
(some condition here, e.g. ID = 42 or something)
Basically, you insert a list of columns into the tblSpills table, based on your new values for Year and Complaint, and you take all other column values from that same table from an existing row (based on that WHERE condition you have to define)
For the "other columns": you only need to list those columns that require a value or for which you want to set a specific value. Those columns that already have a sensible DEFAULT constraint defined don't need to be listed - they'll automatically get the defined default value when a new row is inserted.