Magnus Karlsson
Magnus Karlsson

Reputation: 3579

What is wrong with this SQL query?

I'm getting a SQLException was unhandled by user code "Incorrect syntax near 'TBL'." I also tried to replace [PrioContextDb].[dbo].[RatingListTriangleModels] with just the table name and without brackets.

using (SqlCommand command = new SqlCommand
                    ("delete from [PrioContextDb].[dbo].[RatingListTriangleModels] TBL where ( TBL.To1Id = @MY_ID and TBL.To2Id = @OTHER_ID ) or ( TBL.To2Id = @My_ID and TBL.To3Id = @OTHER_ID ) or ( TBL.To3Id = @My_ID and TBL.To1Id = @OTHER_ID )", cn))
                {
                    //
                    // Add new SqlParameter to the command.
                    //
                    command.Parameters.Add(new SqlParameter("MY_ID", myToid));
                    command.Parameters.Add(new SqlParameter("OTHER_ID", theirTradeObjectid));
                    var no = command.ExecuteNonQuery();

Upvotes: 1

Views: 86

Answers (3)

Neil Knight
Neil Knight

Reputation: 48587

As others have suggested, you need to remove TBL to your SQL statement. You'll need to remove it from all of your clauses too.

But it also looks like you are missing the @ from your parameters which may be the next error that you get.

command.Parameters.Add(new SqlParameter("@MY_ID", myToid));
command.Parameters.Add(new SqlParameter("@OTHER_ID", theirTradeObjectid));

Upvotes: 2

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79979

Add the alias TBL after the DELETE keywrod like so:

delete TBL 
from [PrioContextDb].[dbo].[RatingListTriangleModels] TBL 
where ...
...

Upvotes: -1

TechDo
TechDo

Reputation: 18659

Please try by removing the alias name TBL.

delete from [PrioContextDb].[dbo].[RatingListTriangleModels] 
where ( To1Id = @MY_ID and To2Id = @OTHER_ID ) or 
  ( To2Id = @My_ID and To3Id = @OTHER_ID ) or 
  ( To3Id = @My_ID and To1Id = @OTHER_ID )

Upvotes: 1

Related Questions