Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Confused with my procedure Expand / Collapse
Author
Message
Posted Sunday, August 04, 2013 11:08 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 03, 2014 4:58 PM
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
Post #1480814
Posted Monday, August 05, 2013 1:11 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 4:43 AM
Points: 756, Visits: 631
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
Post #1480823
Posted Monday, August 05, 2013 3:56 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 03, 2014 4:58 PM
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
Post #1481114
Posted Tuesday, August 06, 2013 1:04 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 4:43 AM
Points: 756, Visits: 631
"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
Post #1481175
Posted Tuesday, August 06, 2013 1:40 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:33 AM
Points: 6,754, Visits: 12,854
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
Post #1481193
Posted Tuesday, August 06, 2013 3:45 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 03, 2014 4:58 PM
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



Post #1481592
Posted Tuesday, August 06, 2013 4:00 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 4:43 AM
Points: 756, Visits: 631
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
Post #1481597
Posted Tuesday, August 06, 2013 4:17 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 03, 2014 4:58 PM
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
Post #1481601
Posted Wednesday, August 07, 2013 2:51 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 4:43 AM
Points: 756, Visits: 631
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
Post #1481737
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse