SQL - "deleting" a row by setting all columns to NULL - sql

I'm new to .net.
I have a task at hand for my .net web project, using entity framework.
The task is: To delete a certain row in the db called Products using a webpage interface.
What I have right now: A webpage that displays all the rows in that db, with a button on the side for each row ( that doesn't have any logic behind it yet ), that is supposed to mean the delete button.
How I plan to delete the row: When the button is clicked, it should set all the contents of that row's columns to NULL and save it in the db.
The question is: Is that a viable solution for deleting a row? Could there be any problems in the future if I use that method for deleting the row? Are there maybe better solutions for that task?

i suggest that that you set , for instance the idProduct to -1 (assuming that the id are always positive) since you don't want to delete it directly and Handel the db separately by using a trigger that launches when you update the product table
CREATE TRIGGER tr_onUpdate_delete
ON product
AFTER UPDATE
AS
if (select idProduct from inserted) = -1
begin
DELETE FROM product WHERE id=(select idProduct from deleted)
end
and please note that : i assume that you are updating one row only every time

Related

How do I set up a MS Access form to create a new row in table and set one field as a default value?

So I am not positive I am even asking the correct question, but here it goes. I currently have a MS Access Form built so that someone can enter in a new work order. You are able to set the company, the part number wanted, quantity, and the Work Order number is an auto generated value that I use for my primary key. All of that works great and successfully adds a new row to the table "Work Orders". However, when this form is used to create a new work order I want the last field in the table "Work Orders" which is called "Status" to be set to "Not Started".
I successfully made an update query that asks for the Work Order Number, and will set the "Status" field to "Not Started". Here is the code for that:
UPDATE 03A_WorkOrderList
SET 03A_WorkOrderList.Status = "Not Started"
WHERE ((([03A_WorkOrderList].WO_Num)=[WO_Num:]));
If you give the update query work order number everything works great and the "Status" field is updated.
So back to the form I decided to attach the update query to the build event where the update happens after the new line is created. That seems to have worked too except it asks for the work order number. I totally understand why because it is the code that is in the update query of : WHERE ((([03A_WorkOrderList].WO_Num)=[WO_Num:]));
What I cannot figure out is how to have it pull the work order number that was automatically generated and use that for the update query.
If I am going about this all wrong, please let me know. TIA.
In Ms Access, open your [Work order] table in design mode. Select the Status field and in the property section below, you can set the Default value to be Not started. This way you don't need to perform an update and all new orders will automatically have Not started status.
or in your add order form, On before update event, you can set the status = 'Not started'
I was able to get the functionality that I was looking for with the following UPDATE Query:
UPDATE 03A_WorkOrderList SET 03A_WorkOrderList.Status = "Not Started"
WHERE [03A_WorkOrderList].WO_Num=(SELECT MAX(WO_Num) FROM 03A_WorkOrderList);
Since the WO_Num is auto generated and I want to edit the very same one I was working on, I could just look for the MAX(WO_Num). I then made this UPDATE query apart of the build event of the order form.

What is a bandwidth efficient way to query Microsoft SQL Server to find a unique row where ID exists in Foreign Table

Sorry if I have not worded the question well. I thought about it for a few minutes and this is the best I could come up with.
Long story short, I am adding some Audit Logging into my software (.NET) and am looking for the most efficient way to find out if any modifications were made.
My structure is I have a data object, lets call it a UserLogon, which contains the timestamp, username and access level of somebody logging in to the program.
Now, I have a table of Changesets where one record is created whenever somebody changes the details of the UserLogon. Eg - I logged on as the wrong User, so a data validation person has gone into the event editing system and changed the history for me.
So, there might be 5 different changesets tagged onto the User Logon.
All I want to ask of SQL is for it to tell me True or False that there is at least 1 Changeset linked to the UserLogon.
Currently I have a Join
SELECT Event.Id, Changeset.Id AS ChangesetId
FROM Event
LEFT JOIN Changeset
ON Event.Id = Changeset.EventId
WHERE Changeset.EventId = Event.Id
Which produces the following
Id ChangesetId
F12E54FE-72DF-4A3B-B61B-A4DD00F02597 FA2E0EEB-E5FA-41D1-8C61-A4DD00F025A0
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 FC2496DC-9DF7-4C47-959A-A4DE00BB153C
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 F0CB41F3-D8E3-40F2-B3CE-A4DF00918478
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 4E974BB8-CB41-49E4-A2E7-A4DF00951AE4
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 2887ACBB-4032-4BDD-B8EF-A4E400BD5385
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 8BC5CC13-F557-42FA-9A50-A4E400D370AC
So we can see here there are 2 UserLogons, but there are 5 ChangeSets for the second one.
I would ultimately like to end up with just a True/False on the second column but not too sure where to go from here, or if thats even possible.
My intention is to use this to display an Icon on a ListView in WPF for modified rows to show the user if the row is original or altered.
You can use exists:
select e.*,
(case when exists (select 1 from ChangeSet cs where cs.EventId = e.EventId)
then 1 else 0
end) as HasChangesetFlag
from event e;
For performance, you want an index on ChangeSet(EventId).
Note: This is ANSI SQL and should work in almost any database.

Get ID of inserted record then update another table

Information
I am currently migrating legacy data from an old database to a new database, where the schema has changed.
Old Schema
Table Event
PK_EventID
View OldVideoConferenceParticiants
FK_EventID
FK_ParticipantID
New Schema
Table Event
PK_EventID
FK_VideoConferenceID
Table VideoConference
PK_VideoConferenceID
Table VideoConferenceParticipants
PK_VideoConferenceParticipantID
FK_VideoConferenceID
What I want to do, is:
Group all old participants based on their eventID. (done)
For each eventID, insert a new VideoConference and then set the VideoConference FK for each event to their respective newly inserted VideoConferenceID. (I can't figure this one out)
For each old participant, get the corresponding VideoConferenceID and create a new VideoConferenceParticipant. (done)
Problem
I think I might need to use MERGE but all of my attemps thus far have been disastrous. I've also tried using
INSERT ... OUPUT INSERTED.VideconferenceID
...but I can't get it right. I need to have both the EventID and the VideoConferenceID side by side somehow, which insert does not allow me to do.
Could someone please give me some guidance on how to accomplish #2 above?

Applying a filter of unknown elements using array. Or hiding select records from user

Using a split database, everyone gets a front end with a local table I use as a 'cart' like in online shopping.
I'm copying records to a local table from stock. I don't want the record I copied across to be allowed to be transferred over again making duplicates. I also don't want to delete the original record, just modify it.
So I want them to edit the records copy locally then hit a button that will update the record on the database back end. If they don't hit the button and close the front end, no changes are made. Assume the temp table is wiped on start up.
To stop duplicate records I want to hide select records from the particular user of the front end database only. So if the Access app crashes the record isn't hidden for all users.
Idea: What If I add a Stock_ID (hidden) field to the local table? Then I can poll the column and if any Stock_ID matches the ID of the record I want to copy a message box says Error, record already exists and cancels the record copy?
I think you're saying you want to show the front end user only those stock records whose Stock_ID values are not present in the local table.
If that is correct, you can use an "unmatched query" to display those stock records.
SELECT s.*
FROM
stock AS s
LEFT JOIN [local] AS l
ON s.Stock_ID = l.Stock_ID
WHERE l.Stock_ID Is Null;
The Access query designer has a query wizard for this task. It should be worth a look.
When you say "hide select records", what combinations? Hide all of a certain type from ALL users; hide certain records from SOME users? In your split database, does EACH user have a copy of the front-end, or do all share the same front-end? There must be some criteria that determines who sees what records? Once that is identified, then a solution can follow.

Select record, if doesnt exist (been deleted) - select next valid record

I have inherited a database with records that have been deleted. I’m working on a table of news items. This means that there are some missing id’s where these records have been deleted.
You are able to open any news story’s in the archive(1000’s) then using next and previous buttons to navigate through all of the news stories. At present if you navigate to the next record that has been deleted, a record set end of file is thrown and a default message saying “news item no longer available” is shown.
Is there a way to detect this missing record and move to the next valid news story(with id etc)? I'm using old asp for this site, is there a way to detect this while navigating through a record set or will this type of functionality have to come from the database, maybe a trigger? Thanks for any help.
SELECT TOP 1 *
FROM news
WHERE id >= #next_id
ORDER BY
id
For SQL Server 2005 and above see MSDN.
You can use row_number to create a contiguous number column.
1) Since you know that it affects front-end (your 'record set end of file') of course you can manually increment/decrement identifier and try fetching another record from DB, this seems to be the best that you can do without going into database. But this can be very uneffective if many records in a row are missing. I would advise changing code in database instead.
Assuming you have a query like this:
SELECT * FROM News WHERE Id=#Id
where #Id is identifier which you're trying to fetch. Instead you will have something like this:
SELECT * FROM News WHERE Id=(SELECT MIN(Id) FROM News WHERE Id>=#Id)
This will allow you to select first available record. You should use MAX instead of MIN and <= instead of >= if you will look for 'previous' news item, example above should work for next news item.
Also do not forget that with this approach you will have to increment/decrement identifiers for next/previous records based on the value that you fetched from database instead of that which you were looking for. Example: you have following identifiers in your SQL table - 1,5,12,21. You've opened news item with Id=1. 'Next' button will start looking for Id>=2 and will return record with Id=5. When you will open it then your 'Next' button should look for record with Id>=6 (not 2).
Next point is that you will have to provide not only identifier of record you are trying to fetch but also direction in which to look for. And this parameter should also be passed in http query string.
Also this approach may be not very user-friendly since all pages with following urls will display the same item:
site/news.asp?Id=2&direction=next
site/news.asp?Id=3&direction=next
site/news.asp?Id=4&direction=next
site/news.asp?Id=5&direction=next
2) So maybe it will be more user-friendly to determine which record will be next and previous in advance, when you are displaying current record. In this case you will have to execute query like this:
SELECT (SELECT MAX(Id) FROM News WHERE Id<#Id) as previousId, (SELECT MIN(Id) FROM News WHERE Id>#Id) as nextId
and then correspondingly update Urls for your 'Next'&'Previous' buttons. So if in previous example we will open news item with Id=5 then 'previous' button will navigate directly to Id=1 and 'Next' button will navigate to Id=12.
I believe the second approach is even better in your case since it can be implemented with less changes and it also allows you implementing graying out 'Next'&'Previous' links if corresponding records are not available (you will know this by having NULL returned by the query for previousId or nextId).
Hope this helps :)