Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL Server Delete

Posted on 2011-03-08
18
400 Views
Last Modified: 2014-11-12
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

0
Comment
Question by:Midwest
  • 6
  • 5
  • 4
  • +1
18 Comments
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35069569
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.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 35069578
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.
0
 

Author Comment

by:Midwest
ID: 35069650
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

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:Midwest
ID: 35069656
BTW - The error I am getting is "The multi-part identifier 'DatabaseName.dbo.Users.Id' could not be bound."
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 100 total points
ID: 35069671
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
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 300 total points
ID: 35069672
delete s
from sessions as S
inner join users as U
on U.id = s.UserID
0
 
LVL 3

Assisted Solution

by:CarlsbergFTW
CarlsbergFTW earned 100 total points
ID: 35069708
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.
0
 

Author Comment

by:Midwest
ID: 35069741
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

0
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35069766
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.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 300 total points
ID: 35069819
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
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 35069827
Did you try my syntax?
0
 

Author Comment

by:Midwest
ID: 35069857
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
....
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 300 total points
ID: 35069889
delete s
from sessions as S
inner join users as U
on U.id = s.UserID
and u.id = u.username
0
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35069892


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

0
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35069906
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')
0
 

Author Comment

by:Midwest
ID: 35069923
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

0
 

Author Closing Comment

by:Midwest
ID: 35069959
Thanks guys!
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 35069961
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
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
activating telnet on Microsoft 2016 server 9 70
SQL Error - Query 6 41
EA Azure explanation please 3 35
Server 2016 Terminal Server Error 10 57
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question