sql edit save for log - sql-server

I want to know how to save edited values in sql server to return to it's value after that.
For example:
I have the user which it's status is working, after an year it's status is "Closed" the status is saved as an id in table user when it is working the id of status is 1 when it is closed i update the id to 2. how can I see that this user from date to date was working and now he is closed.
Thanks for your replies.
You got my question.
But the problem here is that I give here only one example which was user status, but the problem is that I have so many tables which I change they id's. For example I have Id of city, or Id of products etc.
If I save it at the same table of user for example I lost user id because every time I update user it will take a new id, or if I do a user status table I will do 20 or more tables like user status history. Also I want to save the text not only id's. For example the user got e new address I want to say from this date to date he was at previous address and now-days hi live in another place.
Any suggestion please :).

Your question is very unclear, but I think you mean "I have a User table with a Status column, and I need to see a history of status changes because when I change the Status value for a specific user, I lose the previous value".
If that's correct, one simple solution is to add a second table called UserStatus or whatever, and INSERT a new row into it every time the user status changes. You can then easily see the complete history of status changes for each user, and you can also find the current status for any user by querying the 'newest' row for that user.
In principle, the Status column on the User table would be unnecessary if you add the UserStatus table, so you could remove it. If that isn't possible (e.g. you don't control the applications using the table), then you can add a trigger to the User table that populates the UserStatus table automatically when you UPDATE the Status.

My suggestion is to have two columns
One is working/date and closed/date so that you can maintain the history of data.

Related

How to generate an activation link in an email via a stored procedure

I want a way to validate emails in order to not choke up our queue. The goal is that when a user is finished with their account setup, the email needs to be validated. My idea is to create a stored procedure that will send an email to the one the user put with a link or something to verify that the email has been sent and opened.
What I am confused with is how to generate this link.
In one of my tables that stores users, I added a column named 'isActive' with type BIT and set to 0. The goal is for the email to have a link where once clicked, will flip the 'isActive' column from 0 to 1, this will verify if the email was reached properly.
I expect that when the user is done entering his info, the stored procedure will send an email to verify that the email is correct by clicking on an activation link.
Based on the comment clarification above, I'm thinking you would want to set one column to a GUID (or add a new column). These links will provide some details about how and why to do this, but the short version is that you can guarantee that the join key is not guessable and is always unique. Set a column to have a default GUID value via UNIQUEIDENTIFIER DEFAULT NEWID() and it will create a new ID with every new row that you can use in other sections of your code.
http://www.sqlservertutorial.net/sql-server-basics/sql-server-guid/
https://www.sqlshack.com/understanding-the-guid-data-type-in-sql-server/

Bound combo box in form does not always update table

I'm struggling with a problem which I can't work out is due to a logical error, a design error or both. Or maybe it's something else entirely.
I have created a simple customer services database and form based user-interface for my company. When a user answers the phone they are supposed to log the call in the system if it is an issue relating to a product.
Sometimes a chain of stores which we have a special relationship with calls up on behalf of customers and we take their reference number and give them ours. This system actually works pretty well if I may say so myself (!), but I noticed after a while that when this store calls up and gives us their number, when they are logged into the system their branch number/name is not updated in the table record.
Here is a summarised outline of the relevant database tables. I renamed the store to SpecialStore:
Issues
IssueReference
ProductID
CustomerID
SpecialStoreBranch
Customers
...
Products
...
SpecialStore
SS_ID
City
Address
The idea is that even if it is either a customer or the store with their own IDs, the products are still stored in the same Products table, just with either the CustomerID or SS_ID filled in. Then when issues are logged, the store or customer is set and logged against a product.
In my main form I have a variety of boxes for a new issue to be filled in. There are combo boxes to select the CustomerID, ProductID etc, and of course the SpecialStoreBranch. These are all bound to the table, and the combo boxes all have row sources relating to their particular table. E.g. CustomerID combo box is a list of the IDs in the Customers table. (This is filtered to the required one elsewhere in the form.)
The Issues table SpecialStoreBranch field has the following query as its Row Source:
SELECT SpecialStore.[SS_ID], [SpecialStore].[City] FROM SpecialStore;
This is the same row source for the combo box on my main form.
The Column Count is 2, with the Bound Column as 0 and the column widths, if necessary, are 1cm;2.544cm.
My main form combo box SpecialStoreBranch has the other attributes:
Limit to List = yes
Allow Value List Edits = no
Inherit Value List = yes
Show Only Row Source Values = no
On the form the CustomerID and ProductID store correctly when the record is saved, and they are also from combo boxes. The form refreshes to update those fields when another part of the form (adding new customers and products) is changed. Could this be why the SpecialStoreBranch does not update? What am I missing? Is there a way to test this? I have been trying to run user tests to see if it ever works, but all I know is that it doesn't always set. If I have forgotten a piece of information let me know.
Edit:
Looking here: BoundColumn-Propery, I'm not sure if setting the bound column to 0 is doing what I think it's doing. But even then it should still use that id number to input into the table I would have thought and if they are all the same, what does it matter. When I tried setting it to 1, it took the text value in the table. Confusing!

GeneralDetailReportQuery key field

We use GeneralDetailReportQuery to get customer "transactions" from QuickBooks desktop and save them to a mssql table. Query is called passing the From/To Date filter params to get changes only. We try to get report changes each day and keep our table in sync with QB data. TranId, customer ListId and TranType seem to be those fields which can uniquely identify each record returned by Report query. Problem starts to happen when users change one of those key fields, customer ListId for example.
It must have been a change of a customer in a transaction... ?
Anyway, how to keep data in sync, does report have some kind of primary key field, which is not changed and can be use to track changes ?
Thanks,
Slava

how to maintain friend requests data in sqlserver

I have a requirement in my app where one user can send the friend request to another user. We use SQL Server database as the backend. The structure of the table is like this.
CREATE TABLE FriendStatus
(FriendStatusId BIGINT PRIMARY KEY IDENTITY(1,1),
FromUserId BIGINT,
ToUserId BIGINT,
StatusId TINYINT,
SentTime DATETIME2,
ResponseTime DATETIME2);
I have few questions related to this:
If user A sends a friend request to user B, then the friend request from user B to User A should still valid ? I feel that should be the case, let me know if there is a better way of handling this ?
Is it a good idea to store the users data in a separate table called friends table once User b approves user A friend request ? And Once User B approves User A request then two records needs to be inserted into the friends table with col1 containing user A and col2 containing user B .At the same time should we also insert a record with User B in col1 and USer A in col2 ? Or two records are unnecessary ?
Is it a good idea to store the users data in a separate table called friends table once User b approves user A friend request ?
No, it's almost never a good idea to duplicate data in your database. You can get anomalies where the same data in two places has two different values. Which value is the correct value?
Here's one way to maintain the relationship
User
----
User ID
User Name
...
Friend
------
User ID 1
User ID 2
...
In the Friend table, the primary key is (User ID 1, User ID 2). You would also have a unique index on (User ID 2, User ID 1). It's up to you if you want to have one row or two rows for each relationship.
One row means you have to do two SELECTS with a UNION. One SELECT using the primary key and one SELECT using the unique index.
Two rows means you do a SELECT using the primary key.
You have the same one row / two row choice with the FriendRequest table.
FriendRequest
-------------
User ID 1
User ID 2
Status
Sent Time Stamp
Accepted Time Stamp
...
You can have one row or two rows for each request. In this case, I'd prefer the one row because I could determine which user initiated the friend request.
I'll take a few examples from Facebook to answer.
If user A sends a friend request to user B, then the friend request
from user B to User A should still valid?
No, a dialog box appears You have already recieved a Friend Request from {name}. Also from B's view, The link to Send Friend Request to A should be changed to Respond to friend request with a respective code.
Is it a good idea to store the users data in a separate table called friends...?
No, one record is enough. Additionally you can have a new column to maintain status status={blocked|friends|pending}
Well thats my idea. You are free since the application is yours. Think as a user too.

Automatically Update Table From Another PHPMYADMIN

I have a existing table Sign-Up which list the users and state an option if they would like to subscribe to the news letter. So in the column (in Table Sign_Up) for newsletter it states "YES" if this has been checked.
I Also have a second table called Subscribers.. suppose i want to add all or specific(column) values of an existing Sign_Up record if the data states Yes they want to subscribe to newsletter.
The purpose of this is to have any records which have said yes to newsletter to update and save to my subscribers table.
Can this be done automatically within phpMyadmin?
Could Triggers help with this?

Resources