SQL Server Delete

I am using SQL Azure.  I am trying to test something and I can't get this delete query to work.  I am guessing it has something to do with the fact that my criteria for deleting a record has nothing to do with that table I am deleting from.

DELETE FROM [DatabaseName].[dbo].[Sessions]
    WHERE [DatabaseName].[dbo].[Users].[Id] = [DatabaseName].[dbo].[Users].[Ud]
GO

Open in new window

MidwestAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CarlsbergFTWCommented:
usually the delete statements are straighforward and similar in all sql programming.

delete from THE_TABLE_HERE where COLUMN = 'DELETE CRITERIA HERE'

Open in new window


Would you mind giving out more details about what you are trying to do ?

Thank you.
BrandonGalderisiCommented:
Why don't you explain what you are trying to do?  I can't interpret what your query does and I don't want t speculate and start sending queries your way.
MidwestAuthor Commented:
I have a sessions table.  I have a users table.  Some of the users are anonymous as indicated by their Id = Username (Their username is a Guid).  I want to delete anonymous users and their sessions.  Here, I am just trying to delete their sessions.

A simplified version:
DELETE FROM Sessions AS S
WHERE Users.Id = Users.Username

Open in new window

The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

MidwestAuthor Commented:
BTW - The error I am getting is "The multi-part identifier 'DatabaseName.dbo.Users.Id' could not be bound."
BrandonGalderisiCommented:
delete s
from sessions s
  inner join users u
    on s.User_Id  = u.Id -- Is this the right join between sessions and users?
    and u.Id = u.Username
Éric MoreauSenior .Net ConsultantCommented:
delete s
from sessions as S
inner join users as U
on U.id = s.UserID
CarlsbergFTWCommented:
delete from sessions where sessions.id in (select id from users, sessions where sessions.id = users.id and users.username = 'anonymous')

Open in new window



try tuning that to fit your needs and be careful when working with delete statements. As Brandon tried to explain to you, the more information the better for a potential valid answer.
MidwestAuthor Commented:
Incorrect syntax near the keyword 'as'.  I have tried this before....  I am not a complete newbie at SQL Server, starting to think it has something to do with SQL Azure.

DELETE FROM [DatabaseName].[dbo].[Sessions] as S
	INNER JOIN [DatabaseName].[dbo].[Users] as U on S.UserId = U.Id
        WHERE U.Id = U.Username
GO

Open in new window

CarlsbergFTWCommented:
What are the exact names of the columns / tables you are trying to work with and what is the value for the 'anonymous' identifier in the column ?


-- 'as' just provides an alias for the table you are working with.

Good luck.
Éric MoreauSenior .Net ConsultantCommented:
again (the syntax is that you have to delete from the ALIAS)



delete s
from sessions as S
inner join users as U
on U.id = s.UserID

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BrandonGalderisiCommented:
Did you try my syntax?
MidwestAuthor Commented:
Anonymous users have Users.Id = Users.Username.  When someone comes into the application, right away I create an anonymous user by adding them into the database, they simply have an Id generated (Guid) and I assign that Id as their Username as well.  They don't have any other information (until they sign in or register).  Here are the necessary fields I am working with.

Users Table:
Id
....
Username
....

Sessions Table:
UserId
....
Éric MoreauSenior .Net ConsultantCommented:
delete s
from sessions as S
inner join users as U
on U.id = s.UserID
and u.id = u.username
CarlsbergFTWCommented:


delete from sessions s where S.userid in (select id from users u, sessions ss where ss.userid = u.id and u.username = 'Guid')

Open in new window

CarlsbergFTWCommented:
delete from sessions s where S.userid in (select u.id from users u, sessions ss where ss.userid = u.id and u.username = 'Guid')
MidwestAuthor Commented:
Oh, sorry guys I missed that.  This would work but I am getting an error because my Users.Id (guid) is different from Users.Username (varchar).  See new question here: http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL_Server_2008/Q_26871234.html

DELETE S FROM [DatabaseName].[dbo].[Sessions] as S
	INNER JOIN [DatabaseName].[dbo].[Users] as U on S.UserId = U.Id
    WHERE U.Id = U.Username
GO

Open in new window

MidwestAuthor Commented:
Thanks guys!
BrandonGalderisiCommented:
There is no need to start a new question:

DELETE S FROM [DatabaseName].[dbo].[Sessions] as S
      INNER JOIN [DatabaseName].[dbo].[Users] as U on S.UserId = U.Id
    WHERE cast(U.Id as char(36)) = U.Username
GO
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.