Click here to Skip to main content
15,918,706 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem with this procedure, the update is naturally updating all the records where I only want it to update one


SQL
--checks to see if the user is signed out, if so sign them in then return 2

ELSE IF EXISTS (select * from sitestaff where SIOType=@SIOType AND SIODate=convert(date,getdate()) and Name=@name and SignIn is NULL)

BEGIN

    update sitestaff
    set
    SignIn=getdate()
    where Name=@name and SIOdate = convert(date,getdate())
    set @result=2

END


What I have tried:

I need to pass a unique column to update only one record but I'm not passing the UniqueID to the query (The database has a unique ID called ID)

Ideally needs to be :-
SQL
update sitestaff
set
SignIn=getdate()
where Name=@name and ID=ID
set @result=2


But not sure how to do this, it basically just needs to update the latest record
Posted
Updated 21-Sep-17 2:52am
v2
Comments
ZurdoDev 21-Sep-17 8:05am    
What are you needing help with? What is your question?

Unless you tell the query which ID value you want to update, you run the risk of updating multiple records - this is particularly a problem when you release to production and there are multiple uses using the DB at the same time.

If the ID is an IDENTITY field, and it was created earlier in the same connection, you can get the value by using the SCOPE_IDENTITY (Transact-SQL) | Microsoft Docs[^] function.
If it's a "real" UniqueID value (i.e. a GUID) then you will need to pass that through from wherever it was created, or you will get more problems as you go on.
 
Share this answer
 
Think I might have solved it, simple way too

SQL
where Name=@name and SIOdate = convert(date,getdate())


should be :-

SQL
where Name=@name and SIOdate = convert(date,getdate()) and SignIn is NULL



Simple fix, the lastest record should always have a NULL SignIn
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900