Showing posts with label gt-. Show all posts
Showing posts with label gt-. Show all posts

Thursday, March 8, 2012

cascade update / foreign key

Hi!
For the sake of simplicity, I have three tables, Employee, Department and
Work

Employee >-- Department
\ /
\ /
^ ^
Work

The Work table have two columns, empno and depno and consists that the
employee has worked on another department.

Here is my scripts:
create table employee (empno int not null primary key, depno int not null)
create table department (depno int not null primary key)
create table work (empno int not null, depno int not null)

alter table employee add constraint fk_employee_department foreign key
(depno)
references department (depno)
on update cascade

alter table work add constraint fk_work_employee foreign key (empno)
references employee (empno)
on update cascade

alter table work add constraint fk_work_department foreign key (depno)
references department (depno)
on update cascade

My problem is the last command. SQL Server responds:
Server: Msg 1785, Level 16, State 1, Line 1
Introducing FOREIGN KEY constraint 'fk_work_department' on table 'work' may
cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON
UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

But I want the depno in the work table to be updated when a department.depno
changes a value.

Does anyone have a suggestion on how to overcome this problem?

Thanks in advance

Best regards,
Gunnar Vyenli
EDB-konsulent as
NORWAYHi,

SQL statement that you had provided is generating circular refrence in
updates
Workaround is to use triger instead of foreign key integrity.

Comment the one of foriegn key sql and use triger.

Note this trigger are not tested. but is only for your reference and
other work around for problem.

/*alter table work add constraint fk_work_employee foreign key (empno)
references employee (empno)
on update cascade
go
*/

create trigger trgUpdateEmpNoInDept
on employee
for update
as
set nocount on

declare @.NewEmpno int , @.OldEmpno int

select @.NewEmpno = inserted.empno from inserted
select @.OldEmpno = deleted.empno from deleted

update work set work.empno = @.NewEmpno
where work.empno = @.OldEmpno
if @.@.rowcount=0 or @.@.error<>0
begin
Rollback tran
end

set nocount off
go

create trigger trgCheckEmpExists
on Work
For Update
as

set nocount on

declare @.NewEmpno int
select @.NewEmpno = inserted.empno from inserted

If Not Exists (Select * from employee where empno = @.NewEmpno )
Begin
RAISERROR ('Empno do not exists', 16, 1)
Rollback Tran
End
set nocount off

go

Thanks & Regards, Amit

"Gunnar Vyenli" <gv@.edbkonsulent.no> wrote in message news:<3f6a24cb$1@.news.broadpark.no>...
> Hi!
> For the sake of simplicity, I have three tables, Employee, Department and
> Work
> Employee >-- Department
> \ /
> \ /
> ^ ^
> Work
> The Work table have two columns, empno and depno and consists that the
> employee has worked on another department.
> Here is my scripts:
> create table employee (empno int not null primary key, depno int not null)
> create table department (depno int not null primary key)
> create table work (empno int not null, depno int not null)
> alter table employee add constraint fk_employee_department foreign key
> (depno)
> references department (depno)
> on update cascade
> alter table work add constraint fk_work_employee foreign key (empno)
> references employee (empno)
> on update cascade
> alter table work add constraint fk_work_department foreign key (depno)
> references department (depno)
> on update cascade
> My problem is the last command. SQL Server responds:
> Server: Msg 1785, Level 16, State 1, Line 1
> Introducing FOREIGN KEY constraint 'fk_work_department' on table 'work' may
> cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON
> UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
> But I want the depno in the work table to be updated when a department.depno
> changes a value.
> Does anyone have a suggestion on how to overcome this problem?
> Thanks in advance
> Best regards,
> Gunnar Vyenli
> EDB-konsulent as
> NORWAY|||Thank you very much!
-Gunnar

"AMIT" <amitb@.zenithinfotech.com> wrote in message
news:f3093a66.0309182236.2313bc10@.posting.google.c om...
> Hi,
>
> SQL statement that you had provided is generating circular refrence in
> updates
> Workaround is to use triger instead of foreign key integrity.
> Comment the one of foriegn key sql and use triger.
> Note this trigger are not tested. but is only for your reference and
> other work around for problem.
> /*alter table work add constraint fk_work_employee foreign key (empno)
> references employee (empno)
> on update cascade
> go
> */
>
> create trigger trgUpdateEmpNoInDept
> on employee
> for update
> as
> set nocount on
> declare @.NewEmpno int , @.OldEmpno int
> select @.NewEmpno = inserted.empno from inserted
> select @.OldEmpno = deleted.empno from deleted
> update work set work.empno = @.NewEmpno
> where work.empno = @.OldEmpno
> if @.@.rowcount=0 or @.@.error<>0
> begin
> Rollback tran
> end
> set nocount off
> go
> create trigger trgCheckEmpExists
> on Work
> For Update
> as
> set nocount on
> declare @.NewEmpno int
> select @.NewEmpno = inserted.empno from inserted
> If Not Exists (Select * from employee where empno = @.NewEmpno )
> Begin
> RAISERROR ('Empno do not exists', 16, 1)
> Rollback Tran
> End
> set nocount off
> go
>
> Thanks & Regards, Amit
> "Gunnar Vyenli" <gv@.edbkonsulent.no> wrote in message
news:<3f6a24cb$1@.news.broadpark.no>...
> > Hi!
> > For the sake of simplicity, I have three tables, Employee, Department
and
> > Work
> > Employee >-- Department
> > \ /
> > \ /
> > ^ ^
> > Work
> > The Work table have two columns, empno and depno and consists that the
> > employee has worked on another department.
> > Here is my scripts:
> > create table employee (empno int not null primary key, depno int not
null)
> > create table department (depno int not null primary key)
> > create table work (empno int not null, depno int not null)
> > alter table employee add constraint fk_employee_department foreign key
> > (depno)
> > references department (depno)
> > on update cascade
> > alter table work add constraint fk_work_employee foreign key (empno)
> > references employee (empno)
> > on update cascade
> > alter table work add constraint fk_work_department foreign key (depno)
> > references department (depno)
> > on update cascade
> > My problem is the last command. SQL Server responds:
> > Server: Msg 1785, Level 16, State 1, Line 1
> > Introducing FOREIGN KEY constraint 'fk_work_department' on table 'work'
may
> > cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or
ON
> > UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
> > But I want the depno in the work table to be updated when a
department.depno
> > changes a value.
> > Does anyone have a suggestion on how to overcome this problem?
> > Thanks in advance
> > Best regards,
> > Gunnar Vyenli
> > EDB-konsulent as
> > NORWAY

Friday, February 24, 2012

Capturing ALL elements and attributes

Hi
I need to capture and store the data from the following xml file:
- <server name="xxx-abcd">
- <match matchno="1">
<fullkey>99</fullkey>
<key>99</key>
<type>99</type>
<value>29</value>
</match>
- <match matchno="2">
<fullkey>87</fullkey>
<key>87</key>
<type>87</type>
<value>46</value>
</match>
- <match matchno="3">
<fullkey>52</fullkey>
<key>52</key>
<type>52</type>
<value>-12</value>
</match>
Using the following schema I can insert everything except the first
line <server name="xxx-abcd">
which I would like to do for each row so that it inserts the following
into my table
e.g
name matchno fullkey key type value
xxx-abcd 1 99 99 99 29
xxx-abcd 2 87 87 87 46
xxx-abcd 3 52 52 52 12
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
- <xsd:element name="match" sql:relation="cpl">
- <xsd:complexType>
- <xsd:sequence>
<xsd:element name="fullkey" type="xsd:integer" />
<xsd:element name="key" type="xsd:string" />
<xsd:element name="type" type="xsd:string" />
<xsd:element name="value" type="xsd:string" />
</xsd:sequence>
<xsd:attribute name="matchno" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
What changes do I need to make to my schema (above) to be able to do
this please?
Thanks in advance to anyone who replies!
Regards
ChrisAssume this is SQL 2000 and SQLXML you're using? If on SQL 2005 there's a
much easier way to achieve the same result with the XML data type .value()
and .nodes() methods.
<templeoakchris@.hotmail.com> wrote in message
news:1171569947.901469.306050@.j27g2000cwj.googlegroups.com...
> Hi
> I need to capture and store the data from the following xml file:
> - <server name="xxx-abcd">
> - <match matchno="1">
> <fullkey>99</fullkey>
> <key>99</key>
> <type>99</type>
> <value>29</value>
> </match>
> - <match matchno="2">
> <fullkey>87</fullkey>
> <key>87</key>
> <type>87</type>
> <value>46</value>
> </match>
> - <match matchno="3">
> <fullkey>52</fullkey>
> <key>52</key>
> <type>52</type>
> <value>-12</value>
> </match>
>
> Using the following schema I can insert everything except the first
> line <server name="xxx-abcd">
> which I would like to do for each row so that it inserts the following
> into my table
> e.g
> name matchno fullkey key type value
> xxx-abcd 1 99 99 99 29
> xxx-abcd 2 87 87 87 46
> xxx-abcd 3 52 52 52 12
>
> - <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
> - <xsd:element name="match" sql:relation="cpl">
> - <xsd:complexType>
> - <xsd:sequence>
> <xsd:element name="fullkey" type="xsd:integer" />
> <xsd:element name="key" type="xsd:string" />
> <xsd:element name="type" type="xsd:string" />
> <xsd:element name="value" type="xsd:string" />
> </xsd:sequence>
> <xsd:attribute name="matchno" type="xsd:string" />
> </xsd:complexType>
> </xsd:element>
> </xsd:schema>
> What changes do I need to make to my schema (above) to be able to do
> this please?
> Thanks in advance to anyone who replies!
> Regards
> Chris
>|||On 15 Feb, 21:48, "Mike C#" <x...@.xyz.com> wrote:
> Assume this is SQL 2000 and SQLXML you're using? If on SQL 2005 there's a
> much easier way to achieve the same result with the XML data type .value()
> and .nodes() methods.
> <templeoakch...@.hotmail.com> wrote in message
> news:1171569947.901469.306050@.j27g2000cwj.googlegroups.com...
>
>
>
>
>
>
>
>
>
>
>
>
> - Show quoted text -
Oops... yes, I am using SQL 2000 and SQLXML ... any ideas?
Regards
Chris|||>
> Oops... yes, I am using SQL 2000 and SQLXML ... any ideas?
> Regards
> Chris
>
Sorry, it's been far too long since I did anything with SQLXML on 2000. I
did notice your XML is not well-formed (no closing </server> ). You might
want to look up the "sql:field" attribute at microsoft.com. You will
probably need to expand your XSD schema to include an outer "server" element
something like this (not tested):
...xsd:schema...
<xsd:element name="server">
<xsd:complexType>
<xsd:sequence>
...existing schema "match" element...
<xsd:attribute name="name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
You will probably need to play with the sql:relation and sql:field
attributes a bit as well.
Sorry couldn't be of more help.

Capturing ALL elements and attributes

Hi
I need to capture and store the data from the following xml file:
- <server name="xxx-abcd">
- <match matchno="1">
<fullkey>99</fullkey>
<key>99</key>
<type>99</type>
<value>29</value>
</match>
- <match matchno="2">
<fullkey>87</fullkey>
<key>87</key>
<type>87</type>
<value>46</value>
</match>
- <match matchno="3">
<fullkey>52</fullkey>
<key>52</key>
<type>52</type>
<value>-12</value>
</match>
Using the following schema I can insert everything except the first
line <server name="xxx-abcd">
which I would like to do for each row so that it inserts the following
into my table
e.g
name matchno fullkey key type value
xxx-abcd 1 99 99 99 29
xxx-abcd 2 87 87 87 46
xxx-abcd 3 52 52 52 12
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
- <xsd:element name="match" sql:relation="cpl">
- <xsd:complexType>
- <xsd:sequence>
<xsd:element name="fullkey" type="xsd:integer" />
<xsd:element name="key" type="xsd:string" />
<xsd:element name="type" type="xsd:string" />
<xsd:element name="value" type="xsd:string" />
</xsd:sequence>
<xsd:attribute name="matchno" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
What changes do I need to make to my schema (above) to be able to do
this please?
Thanks in advance to anyone who replies!
Regards
Chris
Assume this is SQL 2000 and SQLXML you're using? If on SQL 2005 there's a
much easier way to achieve the same result with the XML data type .value()
and .nodes() methods.
<templeoakchris@.hotmail.com> wrote in message
news:1171569947.901469.306050@.j27g2000cwj.googlegr oups.com...
> Hi
> I need to capture and store the data from the following xml file:
> - <server name="xxx-abcd">
> - <match matchno="1">
> <fullkey>99</fullkey>
> <key>99</key>
> <type>99</type>
> <value>29</value>
> </match>
> - <match matchno="2">
> <fullkey>87</fullkey>
> <key>87</key>
> <type>87</type>
> <value>46</value>
> </match>
> - <match matchno="3">
> <fullkey>52</fullkey>
> <key>52</key>
> <type>52</type>
> <value>-12</value>
> </match>
>
> Using the following schema I can insert everything except the first
> line <server name="xxx-abcd">
> which I would like to do for each row so that it inserts the following
> into my table
> e.g
> name matchno fullkey key type value
> xxx-abcd 1 99 99 99 29
> xxx-abcd 2 87 87 87 46
> xxx-abcd 3 52 52 52 12
>
> - <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
> - <xsd:element name="match" sql:relation="cpl">
> - <xsd:complexType>
> - <xsd:sequence>
> <xsd:element name="fullkey" type="xsd:integer" />
> <xsd:element name="key" type="xsd:string" />
> <xsd:element name="type" type="xsd:string" />
> <xsd:element name="value" type="xsd:string" />
> </xsd:sequence>
> <xsd:attribute name="matchno" type="xsd:string" />
> </xsd:complexType>
> </xsd:element>
> </xsd:schema>
> What changes do I need to make to my schema (above) to be able to do
> this please?
> Thanks in advance to anyone who replies!
> Regards
> Chris
>
|||On 15 Feb, 21:48, "Mike C#" <x...@.xyz.com> wrote:
> Assume this is SQL 2000 and SQLXML you're using? If on SQL 2005 there's a
> much easier way to achieve the same result with the XML data type .value()
> and .nodes() methods.
> <templeoakch...@.hotmail.com> wrote in message
> news:1171569947.901469.306050@.j27g2000cwj.googlegr oups.com...
>
>
>
>
>
>
>
> - Show quoted text -
Oops... yes, I am using SQL 2000 and SQLXML ... any ideas?
Regards
Chris
|||>
> Oops... yes, I am using SQL 2000 and SQLXML ... any ideas?
> Regards
> Chris
>
Sorry, it's been far too long since I did anything with SQLXML on 2000. I
did notice your XML is not well-formed (no closing </server>). You might
want to look up the "sql:field" attribute at microsoft.com. You will
probably need to expand your XSD schema to include an outer "server" element
something like this (not tested):
...xsd:schema...
<xsd:element name="server">
<xsd:complexType>
<xsd:sequence>
...existing schema "match" element...
<xsd:attribute name="name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
You will probably need to play with the sql:relation and sql:field
attributes a bit as well.
Sorry couldn't be of more help.