So I enabled change tracking on the database and table and ran the below query;
SELECT *
FROM CHANGETABLE(CHANGES Cust, 0) AS CT
This returns valuable information that I can use but I am wondering if there is a way that I can show the value before and after the change?
For example - If the customer table has a name column and I change that column from 'Facebook' to 'Twitter'. That I can then see the before value (Facebook) and after value (Twitter).
Related
I am using a stored procedure to select a "Random" row from Available_Tickets table, after selection if the user like the number, and buy the ticket, the row will be deleted from the table.
my procedure looks like this:
CREATE OR REPLACE GET_RANDOM_TICKET (RESULT OUT INTEGER) IS
co,mn,mx integer;
BEGIN
SELECT COUNT(ID) , MIN(ID) ,MAX(ID) INTO CO,MN,MX FROM TICKETS;
SELECT TICKET_NUMBER INTO RESULT FROM (
SELECT TICKET_NUMBER
FROM TICKETS WHERE ID >= DBMS_RANDOM(MN,MX)
) WHERE ROWNUM = 1;
END GET_RANDOM_TICKET;
if the user agrees on the returned number the selected row is deleted.
can I get in a worst case scenario where the row with max(id) is delete after executing the first select statement ?
edit 1----
Would the two SELECT statements see the same data in-spite off the changes in the table? and why?
i would redesign it as below
1) add a column (ticket status [free,reserved,sold]) to mark the returned ticket as reserved until the user confirmed his selection
2) use a cursor with for update clause to update that ticket status column after returning it to the user - also add where condition for free tickets
3) check ticket status again after user confirmation if it still reserved then update to sold. (in the very slim chance that 2 users ran the program in the same time and got the same number one of them will get the number and the other one should receive error message since the ticket is not reserved anymore.
hope that help
I have a table of users that has the usual suspects, name, email, etc. As the users complete an activity (queried from another table), I need to award them a gift card code.
update users
set giftcardcode = 'code from other table'
where email in (select email from useractivity where necessary conditions are met)
I have a table of unique gift card codes that are unique, one-time use codes. So I need to update my user table, setting the award code field equal to a distinct, unused gift card code from the gift card code table. Then I need to mark the 'used' field in the gift card table to 'Y'.
The goal is to do this with SQL and not any programming. I'm stumped.
I think there is a Many To Many relationship between User table and Activity table.
So, you can use a trigger to execute a query when update.
Each time a row will be updated in the Activity table, the trigger will do something.
It will UPDATE the User table by adding a new gift code.
I think you can add an attribute in your GiftCode table to easily check if the code as already been used. An you can get an unused code like that :
// Retrieve an unused code based on a BIT attribute.
SELECT TOP 1 [Code] FROM [GiftCode] WHERE IS_UNUSED = 1;
Don't forget to update this Gift code after using it.
You can use a SELECT statement including a sub SELECT statement to get a code too :
// Retrieve an unused code based on User table used codes.
SELECT TOP 1 [Code] FROM [GiftCode] WHERE [Code] NOT IN (SELECT [Code] FROM [User]);
It works well if you don't have too much users.
Otherwise , the first statement will be more efficient.
Don't forget to update the User table.
Now you can easily use one of these previous statement in a UPDATE statement.
It will be something like that :
UPDATE [User] SET [Code] = (
SELECT TOP 1 [Code] FROM [GiftCode] WHERE [Code] NOT IN (
SELECT [Code] FROM [User]))
WHERE USER_ID = // ...;
You can perform this in a trigger.
You can use a stored procedure, it's more efficient and will wrap all the SQL code in a compiled function. Then you can call it in your trigger.
You can execute a stored procedure in a job (see SQL Server Agent jobs) too.
create a Trigger on your table for update and do what you want inside it using inserted and deleted
I want to make filtration on a column after selecting a specific value of another column in the same table, I tried to use #... special character followed by the column's name to get the address of this value.
My SQL statement is like the following :
SELECT ATTRIBUTE FROM TABLE WHERE FIELD = '#FIELDNAME';
If I used a specific value instead of #FIELDNAME, it will work properly but it will be static but I need it to be dynamic based on the selected value.
Create another table which will have the list of values that are in the FIELDNAME and give each record a unique id ,then retrieve the value depending on what you have selected by the name of the new table's field preceded by '#...'
I don't know if that what are you looking for, please let me know.
If no triggers are allowed, do you have any date/time column in the table? Is it possible to have that extra column anyway to see the time of a newly inserted row?
You may have to check the lastest row entered, save its field value into a variable. Then do the select based on the variable value.
Based on the vague last row id you could try the following (it's not pretty). But again, if you have date/time that's more accurate.
select attribute from table
where field = (select field from table
where rowid =(select max(rowid) from table))
;
upate
Do you have the priviledge to set up your insert command as below:
insert into table (id, col1, col2,...) values (1,'something', 'something',...)
returning id into variable; -- you may either save field or id depending on your table
Then you may use this variable to select the records you want.
i have a third party program that uses a data base table to display user created (within the app) fields on the screen so i have a table named User_Created_Fields that looks like
TABLE FIELD_NAME GROUP LABEL
products charge1 1 First Charge
products begin_date1 1 Begin Date
products end_date1 1 End Date
products charge2 2 First Charge
products begin_date2 2 Begin Date
products end_date2 2 End Date
when the app sees this in the table it displays the following in the app
Group 1
First Charge *text area for input*
Begin Date *text area for input*
End Date *text area for input*
----------
Group 2
First Charge *text area for input*
Begin Date *text area for input*
End Date *text area for input*
The app saves the data for these fields to the table and field name specified in the User_Created_Fields table so the PRODUCTS.charge1 and PRODUCTS.charge2 fields (and same for the corresponding date fields).
now i need to create a report that selects the values that are stored in the Products table but... since the fields will be added by users i need the columns selected to come from the Table and field_name columns in the User_Created_Fields table.
so the output would look like
PRODUCTS.Begin_date, PRODUCTS.End_date, PRODUCTS.Charge1, User_Created_Fields.Group
the query would look (very roughly) like
select (select Table ||'.'||field_name from User_Created_Fields where Label='First Charge' and Group= (select Group from User_created_fields where label ='First Charge') ) from Products
this is going into a crystal report so i can't just use sql to generate sql like i normally would. There may be a more crystal esq way to do this as well but i am unaware of what it is. this info is going to be combined with sales detail obviously but i left that part out for simplicitiy since this part is un-godly complicated enough. i am using crystal 11 and oracle 10
if you have read all of this you deserve a reward.... thank you.
As far as I know Crystal Reports can access stored procedures instead of sql statments. So put your code for creating the sql statement in a stored procedure and use that as the source of data for the report.
You might have to determine a maximum number of columns to support and always return that number of columns in order to make crystal happy.
I have a table house with fields price and area, now I want to add one more column named 'average_price'.
How could I set the average_price according to the price and area in MYSQL?
Just one idea:
Add your column in your favorite editor/method
Loop through your table and define average_price value
Update actual row with your value
UPDATE your_table SET average_price = price/area;
You can do it with a suquery. But I don't know about performance.
UPDATE `houses` AS h
SET `avg_price` = (
SELECT AVG(`price`)
FROM `houses` AS h2
WHERE h2.area == h.area
)
Or you could create a temporary table in which to put the average prices and then update.
This doesn't sound like the kind of data you should actually store, by how often you'd have to update it - once every single time you modify the house table in any way! That's a lot of unnecessary load on the database, and a whole lot of room for human error.
Instead, I recommend acquiring it on the fly through a SQL query when you need it, and never actually storing it.
Retrieving from all areas:
SELECT `area`, AVG(`price`) AS `average_price`
FROM `house`
GROUP BY `area`
ORDER BY `average_price` ASC
Retrieving from a specific area:
SELECT `area`, AVG(`price`) AS `average_price`
FROM `house`
WHERE `area` = "New York"
GROUP BY `area`