|
Well, it is a great idea to use "SELECT MAX(ID) from TableXXX" . I suppose that if some record is removed from the table then the given gap is not filled by the new records - the ID is not reused. Always new ID is generated which is greater than the rest. Right?
Thanks,
Abyss
|
|
|
|
|
Hi
Yes but remember always the new ID generated is greater than the last one you insert.
I'll send you(maybe today) a complete application(C#) with SQLServer solution how to do it
Huseyin
|
|
|
|
|
Thanks in advance.
Have you any idea why the original solution fail with "SELECT SCOPE_IDENTITY()" and "SELECT @@IDENTITY AS 'Ident'" ? It is on the MSDN - how to obtain the ID of the inserted record. Is it an Access related problem?
Anyway your solution is great I think that it will work in all cases.
Abyss
|
|
|
|
|
Those features are part of Microsoft SQL Server, not Access/Jet.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
No, it is not a great idea. This only works if it is a single user database AND if the ID has not been reset AND SQL Server decides not to fill in gaps. Do not use select max(ID)...
Jeff Martin
My Blog
|
|
|
|
|
I'm using an Access (MDB) database - via Access/Jet ODBC driver. I suppose that it is a single user database - maybe it is possible to access the database table from different applications at the same time... Will such database fill in gaps (reuse IDs) and when resets the server the ID? Mark told (see the thread) that "SELECT SCOPE_IDENTITY()" are part of SQL server not Access/Jet. Any other idea?
I really appreciate your help
Thanks,
Abyss
|
|
|
|
|
Sorry, no. I don't use Access.
Jeff Martin
My Blog
|
|
|
|
|
If you are calling this in a separate query from the insert, I know SCOPE_IDENTITY() won't work. I'm not sure about @@IDENTITY. I write my stored procs that do the insert where they get the scope_identity() and return it. On the code side, I just get the return from teh stored proc.
Jeff Martin
My Blog
|
|
|
|
|
Uhh, it is out of my knowledge (I'm rather a novice). How does such stored procedure looks like? Something like this?
INSERT INTO TableXXX (A, B) VALUES (3, 5);<br />
SELECT SCOPE_IDENTITY();
in one call. Is it possible at all?
Thanks,
Abyss
|
|
|
|
|
Basically, yes. It is basically a predefined collection of SQL statements stored on the database. But with Access, you can't use them.
Jeff Martin
My Blog
|
|
|
|
|
I already
have had this problem but it exists yet :
I have this View :
VIEW
__________________________
ID Name TypeID
__________________________
1 | John | 1
1 | John | 2
1 | John | 3
2 | Mary | 2
2 | Mary | 3
3 | Betty | 1
3 | Betty | 3
4 | Joe | 1
__________________________
when I say :
SELECT DISTINCT ID FROM [VIEW]WHERE TypeID IN (1,3)AND ID NOT IN (SELECT ID FROM [VIEW]WHERE TypeID NOT IN (1,3))
I get :
3 , 4
But I Want to have only :
3
I dont know what should i do !
|
|
|
|
|
First off, my name isn't Colin, but I'll try to help you. I'm not sure why you are only wanting 3 out of that query, but I'll try to explain why you are getting the 3 and 4.
The first where clause TypeID in (1, 3) will return all records with a TypeID of 1 or 3, so the following records are there...
1 | John | 1
1 | John | 3
2 | Mary | 3
3 | Betty | 1
3 | Betty | 3
4 | Joe | 1
The next where clause AND ID NOT IN (SELECT ID FROM [VIEW] WHERE TypeID NOT IN (1, 3))
That will return all records with a TypeID of anything other than 1 or 3, which based on your recordset, will be all TypeIDs of 2. So the AND ID NOT IN [subquery] will result in data that looks like...
3 | Betty | 1
3 | Betty | 3
4 | Joe | 1
Combining those two results in both IDs of 3 and 4. I get the idea you are trying to find all IDs that have a TypeID of 1 AND 3, but not 2. So you don't want 4 in the result set since it does not have a record with TypeID of 3. Is that correct?
If so, here is a query that does that (replace test1 with your View name)...
<br />
SELECT DISTINCT ID<br />
FROM test1 <br />
WHERE TypeID IN (1,3)<br />
and ID IN (SELECT ID FROM test1 WHERE TypeID = 1) <br />
and ID IN (SELECT ID FROM test1 WHERE TypeID = 3)<br />
and ID NOT IN (SELECT ID FROM test1 WHERE TypeID = 2)<br />
If that's not what you are after, you'll have to be a little clearer in your question.
Jeff Martin
My Blog
|
|
|
|
|
This is exact problem :
I have a RDBMS and i want to search and get query . This is a database about document archiving. Each document may have several subject
(as subjectID in DocumentView,a View for filtering , quering and searching ).
In the application the end user can select many subject and gets the documents in two ways:
#1 . Documents that their subjects are X1 OR X2 OR ... OR Xn.
In this case, documents that may have one or more subject in the selected subjects.For example if user selects A , B , G , U as selected subjects
he gets document result set that their subjects are A OR B OR G OR U . I pass the IDs of selcted subjects to stored procedure as nvarchar
( select * from DocumentView where subjectID in ( 1,12,14,18 ) ) that 1,12,14,18 are ID of A,B,G,U respectivly.
#2 . Documents that their subjects are X1 AND X2 AND ... AND Xn.
For example if user selects A , B , G , U as selected subjects ,in this case user must recieve documents that their subjects are A AND B AND G AND U only.
|
|
|
|
|
I have this view :
VIEW
__________________________
ID Name TypeID
__________________________
1 | John | 1
1 | John | 2
1 | John | 3
2 | Mary | 2
2 | Mary | 3
3 | Betty | 1
3 | Betty | 3
4 | Joe | 1
__________________________
when I say :
SELECT DISTINCT ID FROM [VIEW]WHERE TypeID IN (1,3)AND ID NOT IN (SELECT ID FROM [VIEW]WHERE TypeID NOT IN (1,3))
I get :
3 , 4
But I Want to have only :
3
I dont know what should i do !
|
|
|
|
|
Put aliases around the view names to make sure the query plan gets constructed properly.
SELECT DISTINCT ID
FROM [VIEW] x
WHERE x.TypeID IN (1,3)
AND x.ID NOT IN ( SELECT ID FROM [VIEW] y WHERE y.TypeID NOT IN (1,3) )
|
|
|
|
|
Any one pls give the definition fo sql stored procedure
or what r stored procedure in sql
what r triggers in sql
and finally
difference betn sql triggers and stored procedures
difference betn function in sql and stored procedure
Patel Neelesh A
|
|
|
|
|
A stored procedure (SP) is just a procedure written in SQL. It takes input parameters and can have a return value, but can also return result sets.
A user defined function (UDF) is also like a procedure but written in SQL, however a UDF is more strict. For instance it can only use deterministic functions. UDFs can be used in other queries in the place of values or tables.
A trigger is a special sort of stored procedure that is run automatically when a specific action occurs, for instance a SELECT, INSERT, UPDATE or DELETE on a table. The trigger can perform additional actions depending on what triggered it.
Does this help?
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
Sir is it possile for u to give more details on this topic with some example and also advantages of each of
If possible pls give a simple example of triggers pls
Patel Neelesh A
|
|
|
|
|
Colin, I'm glad that you're very helpful but this request sounds suspiciously like someone's homework.
IMHO giving him the short explanation is ok but any more and you might as well be doing his homework for him and that won't really help him further down the track.
Man, I was never this lazy back in my school days.
Edbert
|
|
|
|
|
Hi everyone,
I've wondered across a peculiar issue with ADO (although something else could be the culprit, but through my investigation, I believe it to be ADO).
I have written a service that uses ADO. When running the service as a console app, everything works great. However, when the service is run 'as a service' in the SCM, I find a problem with the connection.
Basically, this is what I've found to happen:
1. I issue a connect() call, and it returns S_OK.
2. Immediately after that, I execute a simple query, say "select count(*) from table1". At this point, when running as a normal app, the query executes and all is well. However, as a service, the query fails. Upon further inspection, doing a check on whether the connection is actually "connected", I find that it is infact disconnected.
Any ideas? I've been thrashing my brains with this problem for a few hours now. Maybe someone can shed some light on this?
I have tried changing the account under which the service runs. That didn't work. I tried testing different data providers. Same problem. Anything else I can try?
Thanks,
Peter
controlSHIFT
[Glossary Manager] [AfterThought Backup Lite]
All good things were meant to be improved
|
|
|
|
|
Peter Mares wrote:
I have tried changing the account under which the service runs. That didn't work.
hmmm...... That was my first thought. I have written a few services that update different data sources (dB2, MS SQL, Access) with ADO and ADO.net and not had this issue.
Just as an idea, is it possible that you are catching an exception somewhere in your code that is not getting propigated back to the caller?
Hey don't worry, I can handle it. I took something. I can see things no one else can see. Why are you dressed like that?
- Jack Burton
|
|
|
|
|
Hey, thanks for the reply.
I found the problem. When running under the SCM as a service, the SCM obviously takes the "entry point" nominated in the dispatch table that is passed into StartServiceCtrlDispatcher( ) and puts it into another thread. Since I was calling CoInitialize BEFORE this, COM never got initialized, or so it seemed. Simply putting CoInitialize into the entry point function sorted me out.
As an aside, I am slightly curious why, before the fix, calls the ADO Connection::Open( ) function SUCCEEDED and the CreateInstance( ) function returned what seems to be a valid interface pointer to the Connection object - weird.
Anyhow, atleast its sorted out
Thanks,
Peter
controlSHIFT
[Glossary Manager] [AfterThought Backup Lite]
All good things were meant to be improved
|
|
|
|
|
How do I query Access Date field, My query returns no records
This is my query
"Select * from Score where testDate = 3/10/2005"
|
|
|
|
|
|
AFAIK in Access you use # in between the date, e.g. #3/10/2005#
Use the ISO format that Colin mentioned though (#2005-10-03#).
Edbert
|
|
|
|
|