Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Confused with my procedure


Confused with my procedure

Author
Message
Oracle765
Oracle765
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 214
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

Microsoft Access 2003 11.0.SP3 (jp) 22/04/2005 30/04/2008
NULL Microsoft Access 2003 11.0.SP3 (jp) NULL 22/04/2005 30/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
[code]
Microsoft Corporation Office Access 2003 11.0.SP3 (jp) 22/04/2005 30/04/2008
NULL Microsoft Access 2003 11.0.SP3 (jp) NULL 22/04/2005 30/04/2008
[code]

anyone have any idea where I am going wrong
Erland Sommarskog
Erland Sommarskog
SSC Eights!
SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)

Group: General Forum Members
Points: 937 Visits: 866
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.

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Oracle765
Oracle765
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 214
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
Erland Sommarskog
Erland Sommarskog
SSC Eights!
SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)

Group: General Forum Members
Points: 937 Visits: 866
"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.

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9005 Visits: 19028
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
Exploring Recursive CTEs by Example Dwain Camps
Oracle765
Oracle765
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 214
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
Erland Sommarskog
Erland Sommarskog
SSC Eights!
SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)

Group: General Forum Members
Points: 937 Visits: 866
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?

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Oracle765
Oracle765
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 214
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
Erland Sommarskog
Erland Sommarskog
SSC Eights!
SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)SSC Eights! (937 reputation)

Group: General Forum Members
Points: 937 Visits: 866
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?

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search