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

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 :)

Related

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

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

MS Access manual Auto incrementing field

Im building a system for my company to keep track of internal orders, inbetween our warehouses, we have material that goes out warehouse 1 to warehouse 2 and we kind of lose track of how much of "x" is in warehouse 1 and how much in warehouse 2, so i want to implement this access db where a user fills a form and says: order 1: 500 of "x" order 2: 300 of "y". then another user fills an exit form where he says 1 of "x" going out, so i would need the program to keep track of total order and how much as gone out to fill order 1 and so on...
My idea here is to have both an order number and an id number for each of "x" everytime someoneone assembles 1 "x" they fill the form and print a label directly from the access (i have this part working already) while keeping a record of when it was assembled, who verified and what was verified (it will work as a quality control also).
What i dont know is how to program the db so when it reaches 500 of "x", the id number for "x" starts again from 1
This is the one major issue with my program right now, i'm not experienced in access db's or vba, but im getting there with a tip and a trick from here and there, so, no need to be careful with the technical language, i will google it if i have to :p
EDIT:
The table structure goes as follows:
1 table as the main table where I record the check that is made for every product, where I include the model of the product, the said ID that I want to reset after a number of products checked, and a concatenated field that includes most of this information to generate a qr code.
Then there is a table for the Order Number, which is connected to a form to record each new order with a date/time field, the order number itself and the number of products. This number of products must then be called from the code that will count how many products have been checked to date and keep the order number field updated so we can keep track of the order.
Then there is another minor table just to get values for the form, the product models
Thank you for your answers ;)
See this MSDN Documentation
Unfortunately in Access, you cannot 'reset' an ID field, unless you move the records to a newly created table and use that table for every 500 records.
As for the user control and login form, I'm afraid those are separate questions that must be asked in a different thread.
To get you started:
You can set the RecordSource of a form to a table, and when users make entries, the data will be saved to the table. You can also use a form with controls (text boxes, comboboxes, etc.) and create a button that runs a query to insert these records into a table.
The login piece - you can encrypt the database with a password. That may/may not be sufficient.
I would suggest you change your schema, if possible. Something like the following:
Orders
OrderID (Autonumber)
ProductID (link to your Products table)
QuantityRequested
Deliverables
DeliverableID (Autonumber)
OrderID (link to your Orders table)
SequenceNumber: in the BeforeInsert event set this value equal to:
DCount("*", "Deliverables", "OrderID=" & Me.OrderID) + 1
I'm assuming that your form has a control named OrderID that is bound to the OrderID field of the Deliverables table.
The code uses the DCount() function to get the count of all the other deliverables that have already been created for this order. If this is the first deliverable, DCount() will return 0. It then adds 1 to this count to get the sequence number of the next deliverable.
If the new SequenceNumber is greater than the quantity requested, you could display a message saying that the order has been filled and cancel the creation of the Deliverable record.
This is just one approach and it is not a complete solution. I'm assuming that once assigned a sequence number a deliverable cannot be deleted. You might need to make allowances for deliverables that get lost or damaged. You could incorporate a status field to the Deliverable table to deal with this, but you would still need to make a decision about what to do with the SequenceNumber.

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.

How to remove row that exists in another table?

I have two tables. Main table is "CompleteEmailListJuly11" and the second table is "CurrentCustomersEmailJuly11". I want to delete rows in CompleteEmailListJuly11 table that CurrentCustomersEmailJuly11 has based off email.
I've tried this following Delete example, but it doesn't do anything close to what I'm trying to do. This only shows me the ones that EXIST in the database, it doesn't show me the the list of emails that AREN'T matching.
DELETE * FROM CompleteEmailListJuly11 AS i
WHERE EXISTS (
SELECT 1 FROM CurrentCustomersEmailJuly11
WHERE CurrentCustomersEmailJuly11.email = i.EmailAddress
)
Help is greatly appreciated.
This is the query I think you need:
DELETE FROM CompleteEmailListJuly11
WHERE EmailAddress IN (SELECT email FROM CurrentCustomersEmailJuly11)
Ps: The DELETE query does not delete individual fields, only entire rows, so the * is not necessary, you will also need to "Execute" this query rather than "Previewing" or "Exporting"
If you're building your DELETE query in Access' query designer, notice there are two different modes of operation which seem similar to "go ahead and do this".
Datasheet View (represented by the grid icon labeled "View" on the "Design" section of the ribbon). That view enables you to preview the affected records, but does not actually delete them.
The "Run" icon (represented by a red exclamation point). "Run" will actually execute the query and delete the affected records.
If you already know this, my description may seem insulting. Sorry. However, it seems that folks new to Access can easily overlook the distinction between them.
You can use something like this adapted to delete
SELECT ... // complete
EXCEPT
SELECT ... // current
I am not sure exactly how it maps to delete but take a look at that.
I fond it in a similar question: How do I 'subtract' sql tables?
We can use Correlated Query to resolve the issue like
DELETE FROM COMPLETE C
WHERE EMAIL = (SELECT EMAIL FROM CURR CU WHERE CU.EMAIL=C.EMAIL);

Getting last inserted id from a table not part of the current controller I am in

I need to get the last inserted ID from a table which is not part of the current model nor the current database connection: right now I am in table (pdtlisting) but I want to get the last inserted id from table (deallisting). How can i get it?
try this..
Its for mySql..
LAST_INSERT_ID()
OR
Its for Php..
mysql_insert_id()
All the last_insert_id functions (be they PHP wrappers or the native mySQL one) typically refer to the last ID created using the current database connection. The last login was probably not created during the same request you are showing the table in, so this method won't work for you.
Use a normal SELECT to find out the newest login instead - e.g. by using ORDER by creationtime DESC LIMIT 1.
OR
In Yii you can find the last inserted id like this ..
Yii::app()->db->getLastInsertID();
also you can refer this link..
http://www.yiiframework.com/doc/api/1.0/CActiveRecord#primaryKey-detail