Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Why INSERT works depending on order of select_list

Author  Topic 

urobertson
Starting Member

9 Posts

Posted - 2003-12-17 : 15:41:18
I have an INSERT SELECT statement that only works when the select_list is in a specific order.

This works:
INSERT INTO product_colour_size
SELECT DISTINCT p.ina_prod_id, pc.vendor_colour_ID, ps.vendor_size_ID, 1 AS avail_ind
FROM product_style p INNER JOIN product_size ps
ON p.ina_prod_id = ps.ina_prod_id INNER JOIN product_colour pc
ON p.ina_prod_id = pc.ina_prod_id
This produces a foreign key error:
INSERT INTO product_colour_size
SELECT DISTINCT p.ina_prod_id, ps.vendor_size_ID, pc.vendor_colour_ID, 1 AS avail_ind
FROM product_style p INNER JOIN product_size ps
ON p.ina_prod_id = ps.ina_prod_id INNER JOIN product_colour pc
ON p.ina_prod_id = pc.ina_prod_id
INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_product_colour_size_vendor_size'. The conflict occurred in database 'INA', table 'vendor_size', column 'vendor_size_ID'.
The statement has been terminated.

Here is the SQL Script for the tables including foreign keys:
CREATE TABLE [dbo].[vendor_colour] (
[vendor_colour_ID] [int] IDENTITY (1, 1) NOT NULL ,
[nrf_colour_ID] [int] NULL ,
[vendor_no] [int] NOT NULL ,
[description] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[vendor_colour] WITH NOCHECK ADD
CONSTRAINT [PK_vendor_colour] PRIMARY KEY CLUSTERED
(
[vendor_colour_ID]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[vendor_colour] ADD
CONSTRAINT [FK_vendor_colour_colour] FOREIGN KEY
(
[nrf_colour_ID]
) REFERENCES [dbo].[nrf_colour] (
[nrf_colour_ID]
) NOT FOR REPLICATION ,
CONSTRAINT [FK_vendor_colour_vendor] FOREIGN KEY
(
[vendor_no]
) REFERENCES [dbo].[vendor] (
[vendor_no]
) NOT FOR REPLICATION
GO
CREATE TABLE [dbo].[vendor_size] (
[vendor_size_ID] [int] IDENTITY (1, 1) NOT NULL ,
[size_ID] [int] NULL ,
[vendor_no] [int] NOT NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[vendor_size] WITH NOCHECK ADD
CONSTRAINT [PK_vendor_size] PRIMARY KEY CLUSTERED
(
[vendor_size_ID]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[vendor_size] ADD
CONSTRAINT [FK_vendor_size_size] FOREIGN KEY
(
[size_ID]
) REFERENCES [dbo].[size] (
[size_ID]
) NOT FOR REPLICATION ,
CONSTRAINT [FK_vendor_size_vendor] FOREIGN KEY
(
[vendor_no]
) REFERENCES [dbo].[vendor] (
[vendor_no]
) NOT FOR REPLICATION
GO
CREATE TABLE [dbo].[product_colour_size] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[ina_prod_id] [int] NOT NULL ,
[vendor_colour_ID] [int] NOT NULL ,
[vendor_size_ID] [int] NOT NULL ,
[avail_ind] [bit] NOT NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[product_colour_size] WITH NOCHECK ADD
CONSTRAINT [PK_product_colour_size] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[product_colour_size] WITH NOCHECK ADD
CONSTRAINT [DF_product_colour_size_avail_ind] DEFAULT (1) FOR [avail_ind]
GO

ALTER TABLE [dbo].[product_colour_size] ADD
CONSTRAINT [FK_product_colour_size_product_style] FOREIGN KEY
(
[ina_prod_id]
) REFERENCES [dbo].[product_style] (
[ina_prod_id]
) NOT FOR REPLICATION ,
CONSTRAINT [FK_product_colour_size_vendor_size] FOREIGN KEY
(
[vendor_size_ID]
) REFERENCES [dbo].[vendor_size] (
[vendor_size_ID]
) NOT FOR REPLICATION
GO

The table product_style is the main product table. The primary key is ina_prod_id. The script is too long to show just for that bit of info.

Would someone be able to tell me why the INSERT crashes if it's not in the specific order?

Thanks,
Ursula

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-12-17 : 16:30:47
Are you using the latest SQL Server service pack, which is service pack 3a? Could just be a bug.

Tara
Go to Top of Page

urobertson
Starting Member

9 Posts

Posted - 2003-12-17 : 16:41:23
I'm running the code in Query Analyzer of SQL Server 2000. Now here's an embarrasing question: How do I check if the service pack is installed?

I'm currently working for a med-large size company and all the installs were done by the IS dept. Is the service pack for SQL Server client? I don't have access to the server settings or the box. The version is the Add/Remove programs is 8.00.194. Rather not bother the IS dept if I can do this myself (don't need to wait weeks:).

Thanks,
Ursula
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-12-17 : 16:45:30
194 is service pack 1 or no service pack at all, I can't remember which.

For your info though, run SELECT @@VERSION in QA to check.

If you had service pack 3a, it would say 8.00.760 or higher. I'm at 8.00.818 with sp3a, can't remember what I added to get it to 818 though.


Tara
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-12-17 : 16:46:50
Service packs are mostly for the server, but they can be applied to the client machine as well. Applying them to the client was important in previous versions when you were building DTS packages. I heard that it isn't as important now, but it is still recommended that you apply them to both. I always do.

Tara
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-12-17 : 16:48:44
You should always list out the columns that you want to select into...

You're getting lucky because the select list matches the structure of your table...

code like this

INSERT INTO (Col1, col2, col3, ect)
SELECT Col1, col2, col3, ect
FROM myTable99
WHERE yada, yada, yada....

It's not a service pack issue...



Brett

8-)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-12-17 : 16:50:46
Ah, I didn't notice that she wasn't listing out the column in the INSERT part. She's only doing it in the SELECT part. Gotta do it in both. Yes I agree, it's not a service pack issue.

Tara
Go to Top of Page

urobertson
Starting Member

9 Posts

Posted - 2003-12-17 : 16:53:16
Doh! That was obvious! Normally I do write out the INSERT list - no wonder I haven't had this problem before. Thanks guys :)

Thanks,
Ursula
Go to Top of Page
   

- Advertisement -