none
Update to Insert 3 Feilds RRS feed

  • Question

  • Morning

    I'm not sure if this right forum to ask. But my question is

    I have a table dbo.Product that has a feild called Name. Also contained in this table are 3 other feilds I would like to populate these feilds with with the data in the feild Name. So for example ID 1 Name populates SeName with the same peice of data working through all row's of data.

    Is this at all possible.

    All help kindly regarded

    Richard 

    Monday, September 10, 2012 7:46 AM

Answers

  • If you wish to change the value of only a single column, then you can only update the same column.

    However, if you want to do it conditionally,

    UPDATE dbo.TableName
    SET Col1 = @Col1,
    	Col2 = ISNULL(@Col2, Col2), --Check for Null
    	Col3 = CASE @Col3 WHEN NULL THEN 123 ELSE Col3 END -- Custom Condition
    WHERE Col4 = @Col4
     
    You evaluate a condition, if failed, then update with the same column again. SQL Server is smart enough to not update any indexes, if the values are not changed in an update.


    Welcome to MSDN Forums.
    Feel free to ask your questions and Please Note to Vote all helpful topics and Mark all answering posts.

    You Value Community's efforts and Community values your faith.
    - Arun Kumar Allu

    • Marked as answer by RichIEvans Monday, September 10, 2012 8:18 AM
    Monday, September 10, 2012 8:09 AM
  • UPDATE dbo.Product
    Set SeName = Name
    GO
    

    This did the job.

    Thank you and Good Luck.

    Richard

    • Marked as answer by RichIEvans Monday, September 10, 2012 8:23 AM
    Monday, September 10, 2012 8:18 AM

All replies

  • Pls post table structure, sample data and the expected o/p

    vt


    Please mark answered if I've answered your question and vote for it as helpful to help other user's find a solution quicker

    Monday, September 10, 2012 7:48 AM
  • Table

    SELECT TOP 20000 [Id]
          ,[Name]
          ,[ShortDescription]
          ,[FullDescription]
          ,[AdminComment]
          ,[ProductTemplateId]
          ,[ShowOnHomePage]
          ,[MetaKeywords]
          ,[MetaDescription]
          ,[MetaTitle]
          ,[SeName]
          ,[AllowCustomerReviews]
          ,[ApprovedRatingSum]
          ,[NotApprovedRatingSum]
          ,[ApprovedTotalReviews]
          ,[NotApprovedTotalReviews]
          ,[Published]
          ,[Deleted]
    VALUES
           (<Name, nvarchar(400),>
           ,<ShortDescription, nvarchar(max),>
           ,<FullDescription, nvarchar(max),>
           ,<AdminComment, nvarchar(max),>
           ,<ProductTemplateId, int,>
           ,<ShowOnHomePage, bit,>
           ,<MetaKeywords, nvarchar(400),>
           ,<MetaDescription, nvarchar(max),>
           ,<MetaTitle, nvarchar(400),>
           ,<SeName, nvarchar(200),>
           ,<AllowCustomerReviews, bit,>
           ,<ApprovedRatingSum, int,>
           ,<NotApprovedRatingSum, int,>
           ,<ApprovedTotalReviews, int,>
           ,<NotApprovedTotalReviews, int,>
           ,<Published, bit,>
           ,<Deleted, bit,>
           ,<CreatedOnUtc, datetime,>
           ,<UpdatedOnUtc, datetime,>

    Data

    66 BABY-ACTIVE 350G BABY TALCUM POWDER	NULL NULL NULL 3 0 NULL NULL	NULL NULL	1 0 0 0 0	1 0 2012-07-09 09:05:13.6202012-07-09 09:05:13.620	

    I would like to populate

    <MetaTitle, nvarchar(400),>
    <SeName, nvarchar(200),>
    

    With

    [Name]

    So that each SeName and MetaTitle uses the product Name to correct Integar.

    Thank you for the quick response.

    Richard

    Monday, September 10, 2012 8:03 AM
  • If you wish to change the value of only a single column, then you can only update the same column.

    However, if you want to do it conditionally,

    UPDATE dbo.TableName
    SET Col1 = @Col1,
    	Col2 = ISNULL(@Col2, Col2), --Check for Null
    	Col3 = CASE @Col3 WHEN NULL THEN 123 ELSE Col3 END -- Custom Condition
    WHERE Col4 = @Col4
     
    You evaluate a condition, if failed, then update with the same column again. SQL Server is smart enough to not update any indexes, if the values are not changed in an update.


    Welcome to MSDN Forums.
    Feel free to ask your questions and Please Note to Vote all helpful topics and Mark all answering posts.

    You Value Community's efforts and Community values your faith.
    - Arun Kumar Allu

    • Marked as answer by RichIEvans Monday, September 10, 2012 8:18 AM
    Monday, September 10, 2012 8:09 AM
  • Data
    66 BABY-ACTIVE 350G BABY TALCUM POWDER	NULL NULL NULL 3 0 NULL NULL	NULL NULL	1 0 0 0 0	1 0 2012-07-09 09:05:13.6202012-07-09 09:05:13.62

    is this is how the data is stored in Name filed

    Sorry its very hard to understand..

    vt


    Please mark answered if I've answered your question and vote for it as helpful to help other user's find a solution quicker

    Monday, September 10, 2012 8:16 AM
  • UPDATE dbo.Product
    Set SeName = Name
    GO
    

    This did the job.

    Thank you and Good Luck.

    Richard

    • Marked as answer by RichIEvans Monday, September 10, 2012 8:23 AM
    Monday, September 10, 2012 8:18 AM