INSERT FROM

  • Hello all,

    I am tring to do an inset from querry and i am not able to because of columns not matching up..

     

    Here is what i need to do... I have two tables,, tableA and tableB both tables have only three columns. tableA has a FK_ to tableB,,, so i need to copy the tableA uniqueID AND one columns data and insert the data into tableB,, now i have a matching column in tableB, with tableA's FK_ but the other columns data that is from tableA that i want to insert into tableB does not match the column in tableB

    Is it possiable to copy data from one table field and insert into another table field if the fields do not match?.. 

    Something like this....

     INSERT INTO AttributeExtendedValues

                          (AttributeID,AttributeName)

    SELECT     AttributeID, AttributeName

    FROM         Attribute

    WHERE     (AttributeID BETWEEN 497 AND 506)

    AND

    ??? Attribute.AttributeName = AttributeExtendedValues.ProductImage  ????

     

    Here are the two little table that i am working with.

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_AttributeExtendedValues_Attribute]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)

    ALTER TABLE [dbo].[AttributeExtendedValues] DROP CONSTRAINT FK_AttributeExtendedValues_Attribute

    GO

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_ProductAttribute_Attribute]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)

    ALTER TABLE [dbo].[ProductAttribute] DROP CONSTRAINT FK_ProductAttribute_Attribute

    GO

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Attribute]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

    drop table [dbo].[Attribute]

    GO

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AttributeExtendedValues]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

    drop table [dbo].[AttributeExtendedValues]

    GO

    CREATE TABLE [dbo].[Attribute] (

     [AttributeID] [int] IDENTITY (1, 1) NOT NULL ,

     [AttributeName] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

     [AttributeCategoryID] [int] NOT NULL

    ) ON [PRIMARY]

    GO

    CREATE TABLE [dbo].[AttributeExtendedValues] (

     [AttributeExtendedValuesID] [int] IDENTITY (1, 1) NOT NULL ,

     [AttributeID] [int] NOT NULL ,

     [ProductImages] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

     [ProductCost] [money] NULL

    ) ON [PRIMARY]

    GO

    Thank,

    Erik....

    Dam again!

  • Erik - shouldn't you be doing:

    INSERT INTO AttributeExtendedValues

    (AttributeID,ProductImages)

    SELECT AttributeID, AttributeName

    FROM Attribute

    WHERE (AttributeID BETWEEN 497 AND 506)

    oh...but then the data type of these columns don't match - can you change either one to match the other ?!







    **ASCII stupid question, get a stupid ANSI !!!**

  • Is it ok to just change the column name a insert then change it back?

    Dam again!

  • Erik - now I'm confused - in your table def for AttributeExtendedValues you have:

    CREATE TABLE [dbo].[AttributeExtendedValues] (

    [AttributeExtendedValuesID] [int] IDENTITY (1, 1) NOT NULL ,

    [AttributeID] [int] NOT NULL ,

    [ProductImages] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

    [ProductCost] [money] NULL

    ) ON [PRIMARY]

    GO

    but in your insert into statement you have:

    INSERT INTO AttributeExtendedValues

    (AttributeID,AttributeName)...there is no such column in this table....

    hence my suggestion to change column name to ProductImages since you seemed to indicate that it has similar values as in AttributeName..

    NOW can you explain what your question was ?!







    **ASCII stupid question, get a stupid ANSI !!!**

  • You answerd my question.. That is what i was wondering if i could some how insert data from one column to another if the columns did not match..

     

    HA,AH,, OH WELL ..

     

    Erik...

    Dam again!

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply