Ask a Question related to Microsoft SQL / MS SQL Server, Design and Development.
-
Dale Fye #1
Delete from one table with matching records in another table
I'm an Access programmer making the migration to SQL Server, and I
want to delete all the records from one table with matching records in
another table, but am getting a syntax error: Incorrect syntax near
the keyword 'INNER'. The second table starts out as a string of comma
separated text variables but gets converted to a table by the
charlist_to_table UDF.
In Access I'd create a temp table for the Users and do something like
the following, but that doesn't work either
DELETE ProfileUsers.*
FROM ProfileUsers
INNER JOIN tempUsers
on ProfileUsers.UserID = tempUsers.UserID
WHERE ProfileID = 1
Thanks to Aaron B and Erland S, I now have a UDF that allows me to
pass a comma delimted string to a function and get a table of values
back. My T-SQL now looks like:
CREATE PROCEDURE proc_Exercise_User_Profile_UnAssign
@ProfileID INT,
@Users Varchar(8000)
AS
DELETE FROM ProfileUser
INNER JOIN charlist_to_table(@Users, ',') S
ON ProfileUser.UserID = Users.str
WHERE ProfileUser.ProfileID = @ProfileID
GO
Thanks!!!
--
Dale Fye
Dale Fye Guest
-
How to get All Records in Table A that don't havematching ID in Table B
I thought this would be a breeze, but I cannot seem to get it to work. I have two tables (Table A) and (Table B). (Table A) = all productid's ... -
Delete form - Post data to a table and delete uponsubmit.
I have a delete form that I'd like to post the data to a table (delete_pcn) and delete upon submit, so that all deletions may be kept track of in... -
Delete key doesn't delete when datagrid is bound to a disconnected table
(Re: WinForm Datagrid) I manually built a table instead of filling it from a datasource. I bound my datagrid to the table. The datagrid works great.... -
Could not load type VTFixup Table from assembly Invalid token in v-table fix-up table.
We are getting this error after clearing the web.config of database infomation - even after using the wizard to re-enter the information. I could... -
Delete the table link but keep the table
In Excel, there is a way where you could remove a link and still keep the current data that you have. Is there such a thing in Access where all... -
Anith Sen #2
Re: Delete from one table with matching records in another table
In T-SQL, you'll have to use an ackward syntax like:
DELETE ProfileUser
FROM ProfileUser
INNER JOIN charlist_to_table(@Users, ',') S
ON ProfileUser.UserID = Users.str
WHERE ProfileUser.ProfileID = @ProfileID
A clearer construct would be to use EXISTS/IN clause like:
DELETE FROM ProfileUser
WHERE EXISTS ( SELECT *
FROM charlist_to_table(@Users, ',') f1
WHERE f1.str = ProfileUser.UserID )
AND ProfileID = @ProfileID ;
--
- Anith
( Please reply to newsgroups only )
Anith Sen Guest
-
Dean Savovic #3
Re: Delete from one table with matching records in another table
Try this:
CREATE PROCEDURE proc_Exercise_User_Profile_UnAssign
@ProfileID INT,
@Users Varchar(8000)
AS
DELETE ProfileUser
FROM ProfileUser
INNER JOIN charlist_to_table(@Users, ',') S
ON ProfileUser.UserID = Users.str
WHERE ProfileUser.ProfileID = @ProfileID
--
Dean Savovic
[url]www.teched.hr[/url]
"Dale Fye" <spam.saic.com@nomore.dale.fye> wrote in message
news:O$F$VH9PDHA.2480@tk2msftngp13.phx.gbl...> I'm an Access programmer making the migration to SQL Server, and I
> want to delete all the records from one table with matching records in
> another table, but am getting a syntax error: Incorrect syntax near
> the keyword 'INNER'. The second table starts out as a string of comma
> separated text variables but gets converted to a table by the
> charlist_to_table UDF.
>
> In Access I'd create a temp table for the Users and do something like
> the following, but that doesn't work either
>
> DELETE ProfileUsers.*
> FROM ProfileUsers
> INNER JOIN tempUsers
> on ProfileUsers.UserID = tempUsers.UserID
> WHERE ProfileID = 1
>
> Thanks to Aaron B and Erland S, I now have a UDF that allows me to
> pass a comma delimted string to a function and get a table of values
> back. My T-SQL now looks like:
>
> CREATE PROCEDURE proc_Exercise_User_Profile_UnAssign
>
> @ProfileID INT,
> @Users Varchar(8000)
>
> AS
>
> DELETE FROM ProfileUser
> INNER JOIN charlist_to_table(@Users, ',') S
> ON ProfileUser.UserID = Users.str
> WHERE ProfileUser.ProfileID = @ProfileID
>
> GO
>
>
> Thanks!!!
> --
>
> Dale Fye
>
>
>
Dean Savovic Guest
-
Russell Fields #4
Re: Delete from one table with matching records in another table
Dale,
DELETE ProfileUser -- This is the missing piece. Note that there is no * or
column list.
FROM ProfileUser
INNER JOIN charlist_to_table(@Users, ',') S
ON ProfileUser.UserID = Users.str
WHERE ProfileUser.ProfileID = @ProfileID
Russell Fields
"Dale Fye" <spam.saic.com@nomore.dale.fye> wrote in message
news:O$F$VH9PDHA.2480@tk2msftngp13.phx.gbl...> I'm an Access programmer making the migration to SQL Server, and I
> want to delete all the records from one table with matching records in
> another table, but am getting a syntax error: Incorrect syntax near
> the keyword 'INNER'. The second table starts out as a string of comma
> separated text variables but gets converted to a table by the
> charlist_to_table UDF.
>
> In Access I'd create a temp table for the Users and do something like
> the following, but that doesn't work either
>
> DELETE ProfileUsers.*
> FROM ProfileUsers
> INNER JOIN tempUsers
> on ProfileUsers.UserID = tempUsers.UserID
> WHERE ProfileID = 1
>
> Thanks to Aaron B and Erland S, I now have a UDF that allows me to
> pass a comma delimted string to a function and get a table of values
> back. My T-SQL now looks like:
>
> CREATE PROCEDURE proc_Exercise_User_Profile_UnAssign
>
> @ProfileID INT,
> @Users Varchar(8000)
>
> AS
>
> DELETE FROM ProfileUser
> INNER JOIN charlist_to_table(@Users, ',') S
> ON ProfileUser.UserID = Users.str
> WHERE ProfileUser.ProfileID = @ProfileID
>
> GO
>
>
> Thanks!!!
> --
>
> Dale Fye
>
>
>
Russell Fields Guest
-
Dale Fye #5
Re: Delete from one table with matching records in another table
Works like a charm. Thanks Anith
--
HTH
Dale Fye
"Anith Sen" <anith@bizdatasolutions.com> wrote in message
news:#w4hWM9PDHA.704@tk2msftngp13.phx.gbl...
In T-SQL, you'll have to use an ackward syntax like:
DELETE ProfileUser
FROM ProfileUser
INNER JOIN charlist_to_table(@Users, ',') S
ON ProfileUser.UserID = Users.str
WHERE ProfileUser.ProfileID = @ProfileID
A clearer construct would be to use EXISTS/IN clause like:
DELETE FROM ProfileUser
WHERE EXISTS ( SELECT *
FROM charlist_to_table(@Users, ',') f1
WHERE f1.str = ProfileUser.UserID )
AND ProfileID = @ProfileID ;
--
- Anith
( Please reply to newsgroups only )
Dale Fye Guest



Reply With Quote

