Showing posts with label criteria. Show all posts
Showing posts with label criteria. Show all posts

Wednesday, March 21, 2012

deleting data using table prefix

I can run a select to retrieve data using a prefix 'a' for the specific table involved. However when I try to run a delete using the same criteria it fails telling me

Msg 102, Level 15, State 1,.......Line 1

Incorrect syntax near 'a'

The Select statement looks like:

select count(*) from schema.table a where a.customer_id=1234

The Delete looks like:

delete from schema.table a where a.customer_id=1234

What am I doing wrong here? and how can I prefix the table, because the command I want to run is much more complicated than the example above and it needs the prefix

According to BOL there is no alias for delete statement

Thanks

|||

You can use the TSQL extension for the DELETE statement like:

delete from schema.table

from schema.table a

where a.customer_id=1234

Friday, February 17, 2012

Delete Query Using Join

I need to delete records from one table base on criteria from another table. The example below from the Northwind database shows exactly what I want to do. I want to delete the records from the employee table who a terrorityID of 30346 in the EmployeeTerritories table.

Can someone tell me how to write a delete statement that will delete the rows returned from the following SQL Statement? The sql statement will return one employee name. I would lke to delete that one employee from the employee table and I havent been able to figure out how to do it.

////////// Sql Statment
SELECT dbo.EmployeeTerritories.EmployeeID, dbo.EmployeeTerritories.TerritoryID
FROM dbo.EmployeeTerritories INNER JOIN
dbo.Employees ON dbo.EmployeeTerritories.EmployeeID = dbo.Employees.EmployeeID
WHERE (dbo.EmployeeTerritories.TerritoryID = N'30346')
////////// end of sql statement

Thanks
GEMRemove the select clause and replace with a delete clause. Your delete clause needs to include the name of the table you want to delete from.

Assuming of course you want to use the T-SQL extension rather than SQL standard syntax:
http://msdn2.microsoft.com/en-US/library/aa258847(SQL.80).aspx
As ever, BoL is the best starting point|||DELETE dbo.EmployeeTerritories
FROM dbo.EmployeeTerritories
JOIN dbo.Employees
ON dbo.EmployeeTerritories.EmployeeID = dbo.Employees.EmployeeID
WHERE (dbo.EmployeeTerritories.TerritoryID = N'30346')

--ddave