Confused with my procedure

  • Hi Professionals

    I have a procedure like so

    ALTER procedure [dbo].[updatethecolumn]

    @columnname1 varchar(1000),

    @columnname2 varchar(1000)

    as

    begin

    --Update the Software Manufacturer and the Product name

    if RTRIM(@columnname1) = 'softwaremanufacturer' and RTRIM(@columnname2) = 'productname'

    begin

    UPDATE dbsource SET @columnname1 = dbref.Amended_SW_Manufacturer,

    @columnname2= dbref.Amended_Product_Name

    FROM dbo.newtable dbsource

    INNER JOIN (

    SELECT Raw_SW_Manufacturer,Amended_SW_Manufacturer,Raw_Product_Version,Amended_Product_Version,

    Raw_Product_Name,Amended_Product_Name

    FROM datalookuptable

    GROUP BY Raw_SW_Manufacturer,Amended_SW_Manufacturer,Raw_Product_Version,Amended_Product_Version,

    Raw_Product_Name,Amended_Product_Name

    ) dbref

    ON dbref.Raw_SW_Manufacturer = dbsource.softwaremanufacturer

    --and dbref.Raw_Product_Version = dbsource.productversion

    and dbref.Raw_Product_Name = dbsource.productname

    end

    I have ran the procedure both ways like so

    exec updatethecolumn softwaremanufacturer,productname

    and

    exec updatethecolumn 'softwaremanufacturer','productname'

    here is my table data

    MicrosoftAccess 200311.0.SP3 (jp)22/04/200530/04/2008

    NULLMicrosoft Access 2003 11.0.SP3 (jp)NULL22/04/200530/04/2008

    which does not update for some strange reason and I dont know why.

    when I run my procedure manually it updates fine.

    UPDATE dbsource SET softwaremanufacturer = dbref.Amended_SW_Manufacturer,

    productname= dbref.Amended_Product_Name

    FROM dbo.newtable dbsource

    INNER JOIN (

    SELECT Raw_SW_Manufacturer,Amended_SW_Manufacturer,Raw_Product_Version,Amended_Product_Version,

    Raw_Product_Name,Amended_Product_Name

    FROM datalookuptable

    GROUP BY Raw_SW_Manufacturer,Amended_SW_Manufacturer,Raw_Product_Version,Amended_Product_Version,

    Raw_Product_Name,Amended_Product_Name

    ) dbref

    ON dbref.Raw_SW_Manufacturer = dbsource.softwaremanufacturer

    --and dbref.Raw_Product_Version = dbsource.productversion

    and dbref.Raw_Product_Name = dbsource.productname

    which produces the update like so

    Microsoft CorporationOffice Access 200311.0.SP3 (jp)22/04/200530/04/2008

    NULLMicrosoft Access 2003 11.0.SP3 (jp)NULL22/04/200530/04/2008

    anyone have any idea where I am going wrong

  • This:

    UPDATE dbsource

    SET @columnname1 = dbref.Amended_SW_Manufacturer,

    @columnname2= dbref.Amended_Product_Name

    Is a funny way of doing variable assignment. There is no indirection, if that is what you thought.

    Another problem with your UPDATE is that one target row matches many source rows. This means that the result of the UPDATE is not deterministic.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • is there a way around this the as the variables

    @columnname1 and @columnname2 could be named anything when they are passed in as this is selected from a dynamically imported spreadsheet

  • "Way around" is may be not the most approriate description, since you seem to be inventing your rules. And the whole idea looks funny. Do you really have umpteen colunms with Manufacturer and Product names? That looks like an incorrect table design to me.

    You could do:

    UPDATE tbl

    SET col1 = CASE WHEN @columnname1 = 'col1'

    THEN dbref.Amended_SW_Manufacturer

    WHEN @columnname2 = 'col2'

    THEN dbref.Amended_Product_Name

    END,

    col2 = CASE WHEN ...

    But I will have to admit that this looks highly suspicious.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • Oracle765 (8/5/2013)


    is there a way around this the as the variables

    @columnname1 and @columnname2 could be named anything when they are passed in as this is selected from a dynamically imported spreadsheet

    You could use dynamic SQL, but I wouldn't recommend it if your strings are derived from a "dynamically imported" spreadsheet. What's wrong with extending the logic that you currently have in your stored procedure? This would work:

    ALTER procedure [dbo].[updatethecolumn]

    @columnname1 varchar(1000),

    @columnname2 varchar(1000)

    as

    begin

    --Update the Software Manufacturer and the Product name

    if RTRIM(@columnname1) = 'softwaremanufacturer' and RTRIM(@columnname2) = 'productname'

    begin

    UPDATE dbsource SET

    softwaremanufacturer = dbref.Amended_SW_Manufacturer,

    productname = dbref.Amended_Product_Name

    FROM dbo.newtable dbsource

    INNER JOIN (

    SELECT Raw_SW_Manufacturer,Amended_SW_Manufacturer,Raw_Product_Version,Amended_Product_Version,

    Raw_Product_Name,Amended_Product_Name

    FROM datalookuptable

    GROUP BY Raw_SW_Manufacturer,Amended_SW_Manufacturer,Raw_Product_Version,Amended_Product_Version,

    Raw_Product_Name,Amended_Product_Name

    ) dbref

    ON dbref.Raw_SW_Manufacturer = dbsource.softwaremanufacturer

    --and dbref.Raw_Product_Version = dbsource.productversion

    and dbref.Raw_Product_Name = dbsource.productname

    end

    Another possibility is to list all of the columns which could be updated in the UPDATE, and only update those which appear as parameters - otherwise, update to same (SET softwaremanufacturer = softwaremanufacturer).

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Hi Chris

    I agree that what you say that it would work

    with the

    if RTRIM(@columnname1) = 'softwaremanufacturer' and RTRIM(@columnname2) = 'productname'

    begin

    UPDATE dbsource SET

    softwaremanufacturer = dbref.Amended_SW_Manufacturer,

    but what if the softwaremanufacturer from the spreadsheet was called, softwareman or productmanufacturer or just manufacturer as it could be named anything but there will only ever be one column that matches the softwaremanufacturer column

    regards

    Alan

  • If they can be named anything, why even bother about them?

    You are talking about a spreadsheet, but maybe you could explain the entire process? Why would the names in a spreadsheet, whether you will copy data from one table to another?

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • because the user has a dropdown box to either select it as the software manufacturer, product name or product version then I pass the name of the column across to update which could be named anything, the dropdown boxes are static

  • Sorry, but you will have to colour me as completely clueless. You are copying data from one database table to another. Where does the spreadsheet come into the picture?

    I am sorry, but it is very difficult to help you, when I don't understand what you are trying to do and why. Can you explain the full process?

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

Viewing 9 posts - 1 through 8 (of 8 total)

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