Showing posts with label delete. Show all posts
Showing posts with label delete. Show all posts

Tuesday, March 20, 2012

Query Parsing Error

delete from checklists where checklistid not in (SELECT CHECKLISTS.CHECKLISTID FROM CHECKLISTS LEFT OUTER JOIN ItemsToChecklist ON CHECKLISTS.CHECKLISTID = ItemsToChecklist.CHECKLISTID having COUNT(ItemsToChecklist.CHECKLISTID) > 0 GROUP BY CHECKLISTS.CHECKLISTID)

Not sure why it's not liking it. Says token in error is Group. We are pulling a subset of data down from Oracle and need to clean it up a bit more when it comes down to the handheld.

Here is the script to create the two tables in question.

create table Checklists (ChecklistId int primary key not null, Groups int not null, defaultlocationid int, Title nvarchar(50));

create table ChecklistItems (ChecklistItemId int primary key not null, Description nvarchar(255) not null, Recommendation nvarchar(255) not null, HotList bit not null);

create table ItemsToChecklist(ChecklistId int not null, ChecklistItemId int not null, SortOrder int not null);

alter table ItemsToChecklist add constraint fk__checklist foreign key (ChecklistId) references Checklists (ChecklistId);

alter table ItemsToChecklist add constraint fk__checklistitem foreign key (ChecklistItemId) references ChecklistItems (ChecklistItemId);

alter table ItemsToChecklist add constraint pk__primary__ItemsToChecklist primary key (ChecklistId, ChecklistItemId);

Thanx for any help.

Sub selects (nested queries) are no supported by SQL CE. Why not do the clean up server side, to save device processing, device memory and bandwidth?

Friday, March 9, 2012

Query optimize

can this be rewritten in more optimized way..
DELETE FROM A WHERE ID Not In (SELECT ID FROM @.ABC)DELETE FROM A T WHERE ID Not exists (SELECT ID FROM @.ABC where ID=T.ID)
Madhivanan|||You cannot use a variable table name. Indexes will help, but the
optimizer is msart enough to pick a good plan.|||You could possibly try:
DELETE def
WHERE NOT EXISTS
(
SELECT 1
FROM abc
WHERE abc.ID = def.ID
)
Frank
"Sunny" <Sunny@.discussions.microsoft.com> wrote in message
news:1BF75571-9841-41EC-95EF-16D6837095C9@.microsoft.com...
> can this be rewritten in more optimized way..
> DELETE FROM A WHERE ID Not In (SELECT ID FROM @.ABC)|||delete A
from A
left join B
on B.CommonKey = A.CommonKey
where (B.CommonKey is null)
ML|||On Thu, 11 Aug 2005 06:54:01 -0700, Sunny wrote:

> can this be rewritten in more optimized way..
> DELETE FROM A WHERE ID Not In (SELECT ID FROM @.ABC)
As Celko said, you can't use a variable table name directly. The best you
can do is dynamic SQL:
declare @.abc nvarchar(32)
declare @.sql nvarchar(500)
set @.abc='MyTable'
set @.sql = N'DELETE FROM A WHERE NOT EXISTS (SELECT * FROM ' + @.ABC + ' B
WHERE A.ID=B.ID)'
execute sp_executesql @.sql|||Ah, I must have just breezed through the DML statement. I didn't catch the
@.. Sunny, if you were using a declared table variable by design, you should
follow the advice in the posts that followed mine.
Frank Castora.
"Sunny" <Sunny@.discussions.microsoft.com> wrote in message
news:1BF75571-9841-41EC-95EF-16D6837095C9@.microsoft.com...
> can this be rewritten in more optimized way..
> DELETE FROM A WHERE ID Not In (SELECT ID FROM @.ABC)|||Before you issue blanket statements like the one below, you should check
your facts. You can indeed use a variable table name, provided the variable
table name refers to an existing table variable. Table variables are a
feature of SQL Server 2000. You refer to a table variable in the same way
as a scalar variable.
"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1123769246.878870.169080@.g44g2000cwa.googlegroups.com...
> You cannot use a variable table name. Indexes will help, but the
> optimizer is msart enough to pick a good plan.
>

Query optimisor out of stack space

Hi,

I have recently upgraded to SQL Server 2005 (SP1) Developer Edition from SQL Server 2000 (SP4). Since doing so, i have been unable to delete any records from a specific table.

I have found this is due to having approximately 340 foreign key constraints referencing the UserID column of the table.

This functionality worked in SQL Server 2000. Do Microsoft plan to release any fixes for this issue?

Kind Regards,
Martyn...

Wow, 340 FKs?!

I don't think there is going to be a fix for this. The cost for maintaining this constraint is quite large. You should consider simplifying the schema.

http://msdn2.microsoft.com/en-us/library/aa337357.aspx|||

Hello,

are you the Martyn Lawson from Taunus-Sparkasse?

Gru?

moh

|||

Hi,

Just to update you all with this issue. I have raised a formal support call with Microsoft who are currently investigating the issue. They recommend a maximum of 253 FK's on a single table but the absolute maximum is 2966.

The support engineer assigned to my case has told me that although i have exceeded the recommended maximum it should not cause an error and this is indeed a bug.

I am still waiting to here the outcome, but will update this thread as soon as i know anything.

Cheers,

Martyn...

|||No. I am not.

Query optimisor out of stack space

Hi,

I have recently upgraded to SQL Server 2005 (SP1) Developer Edition from SQL Server 2000 (SP4). Since doing so, i have been unable to delete any records from a specific table.

I have found this is due to having approximately 340 foreign key constraints referencing the UserID column of the table.

This functionality worked in SQL Server 2000. Do Microsoft plan to release any fixes for this issue?

Kind Regards,
Martyn...

Wow, 340 FKs?!

I don't think there is going to be a fix for this. The cost for maintaining this constraint is quite large. You should consider simplifying the schema.

http://msdn2.microsoft.com/en-us/library/aa337357.aspx|||

Hello,

are you the Martyn Lawson from Taunus-Sparkasse?

Gru?

moh

|||

Hi,

Just to update you all with this issue. I have raised a formal support call with Microsoft who are currently investigating the issue. They recommend a maximum of 253 FK's on a single table but the absolute maximum is 2966.

The support engineer assigned to my case has told me that although i have exceeded the recommended maximum it should not cause an error and this is indeed a bug.

I am still waiting to here the outcome, but will update this thread as soon as i know anything.

Cheers,

Martyn...

|||No. I am not.

Query optimisor out of stack space

Hi,

I have recently upgraded to SQL Server 2005 (SP1) Developer Edition from SQL Server 2000 (SP4). Since doing so, i have been unable to delete any records from a specific table.

I have found this is due to having approximately 340 foreign key constraints referencing the UserID column of the table.

This functionality worked in SQL Server 2000. Do Microsoft plan to release any fixes for this issue?

Kind Regards,
Martyn...

Wow, 340 FKs?!

I don't think there is going to be a fix for this. The cost for maintaining this constraint is quite large. You should consider simplifying the schema.

http://msdn2.microsoft.com/en-us/library/aa337357.aspx|||

Hello,

are you the Martyn Lawson from Taunus-Sparkasse?

Gru?

moh

|||

Hi,

Just to update you all with this issue. I have raised a formal support call with Microsoft who are currently investigating the issue. They recommend a maximum of 253 FK's on a single table but the absolute maximum is 2966.

The support engineer assigned to my case has told me that although i have exceeded the recommended maximum it should not cause an error and this is indeed a bug.

I am still waiting to here the outcome, but will update this thread as soon as i know anything.

Cheers,

Martyn...

|||No. I am not.

Monday, February 20, 2012

query metadata within trigger scope

I have a few tables which I'd like to protect from accidental deletes.
I'm thinking adding an INSTEAD of trigger on DELETE to the table that
raise an error and return could be an approach (not sure if its the
best). Something like:
CREATE TRIGGER trig_DeleteNoAllowed
ON sandbox.dbo.Table1
INSTEAD OF DELETE
AS
BEGIN
if @.@.ROWCOUNT > 0
BEGIN
RAISERROR ('Deletion not allowed.', 16, 1)
RETURN
END
END
However, I'd like to tell the user which table he tries to delete rows
from. Something like:
BEGIN
if @.@.ROWCOUNT > 0
BEGIN
DECLARE @.tablename varchar(128)
SET @.tablename = [?]
RAISERROR
('Deletion not allowed on table %d.',
16, 1, @.tablename)
RETURN
END
END
How do I get the name of the table on which the trigger is working!
MortenHi
Take a look at this example
CREATE TABLE TT
(
COL INT
)
CREATE TRIGGER MY_TR ON TT
FOR INSERT
AS
DECLARE @.ObjID int
SET @.ObjID = (SELECT parent_obj FROM sysobjects WHERE id = @.@.PROCID)
SELECT OBJECT_NAME(@.ObjID) AS 'Parent Table'
INSERT INTO TT VALUES (1)
SELECT * FROM TT
DROP TABLE TT
"wapsiii" <wapsiii@.otmail.com> wrote in message
news:6po8o1t2s5fsd06uo862o86vcc5nhm95q4@.
4ax.com...
>I have a few tables which I'd like to protect from accidental deletes.
> I'm thinking adding an INSTEAD of trigger on DELETE to the table that
> raise an error and return could be an approach (not sure if its the
> best). Something like:
> CREATE TRIGGER trig_DeleteNoAllowed
> ON sandbox.dbo.Table1
> INSTEAD OF DELETE
> AS
> BEGIN
> if @.@.ROWCOUNT > 0
> BEGIN
> RAISERROR ('Deletion not allowed.', 16, 1)
> RETURN
> END
> END
> However, I'd like to tell the user which table he tries to delete rows
> from. Something like:
> BEGIN
> if @.@.ROWCOUNT > 0
> BEGIN
> DECLARE @.tablename varchar(128)
> SET @.tablename = [?]
> RAISERROR
> ('Deletion not allowed on table %d.',
> 16, 1, @.tablename)
> RETURN
> END
> END
> How do I get the name of the table on which the trigger is working!
> Morten|||Try something like this:
BEGIN
if @.@.ROWCOUNT > 0
BEGIN
DECLARE @.tablename varchar(128)
SELECT @.tablename = so2.name
FROM sysobjects so1
INNER JOIN sysobjects so2
ON so1.id = so2.parent_obj
AND so2.xtype = 'tr'
AND so2.id= @.@.PROCID
RAISERROR
('Deletion not allowed on table %d.',
16, 1, @.tablename)
RETURN
END
END
Please remember that certain tables will change to the next release of
SQL server (assuming that you are using SQL2k here)
HTH, jens Suessmeyer.|||Since the trigger code needs to specify the table in the CREATE TRIGGER
... part, why not just include the table name instead of trying to
parameterize it?
Any reason why you want to use a trigger rather than security to
prevent accidental deletes:
DENY DELETE ON dbo.Table1 TO ...
This has the advantage that you can apply it selectively and you won't
block deletes by admins. A trigegr on the other hand is hard to disable
selectively.
David Portas
SQL Server MVP
--|||thanks for your replies...
I was thinking... even if I use the table as a primary key table and
hence have delete constraints on used values, I'd like not even admins
to be able to delete un-used values by accident. I couldn't come with
any other way than using a trigger or instead of trigger. Of course
the trigger could easily be disabled, but then it would hardly be an
accident.
I reached the same conclusion in regards to "just include the table
name and not trying to parameterize it". I guess this too would be
faster. However, I learned something, picking around in metadata :)
On 23 Nov 2005 05:25:35 -0800, "David Portas"
<REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote:

>Since the trigger code needs to specify the table in the CREATE TRIGGER
>... part, why not just include the table name instead of trying to
>parameterize it?
>Any reason why you want to use a trigger rather than security to
>prevent accidental deletes:
>DENY DELETE ON dbo.Table1 TO ...
>This has the advantage that you can apply it selectively and you won't
>block deletes by admins. A trigegr on the other hand is hard to disable
>selectively.
>--
>David Portas
>SQL Server MVP