Updating a table with variable column name, retrieved from another table ?

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 1
Reputation: fatjoez is an unknown quantity at this point 
Solved Threads: 0
fatjoez fatjoez is offline Offline
Newbie Poster

Updating a table with variable column name, retrieved from another table ?

 
0
  #1
Jul 3rd, 2009
I have 3 tables, below are their structures
( a bit of pseudo code )

I'm trying to update one table, with a variable column name, retrieved from another table

order
[id,field1,field2]
example row ( '1' , '', '' )

produkt
[order_id,produktnr]
example row ( '1' , '23' )

fields
[produktnr,fieldname,value]
example row ( '23', 'field1', 'abc' )

Example update

set order.field1 = abc

(retrieve column name "field1" from the "fieldname" field in fields table, and value from value in same table, where there is a match from order.id to produkt.id, produkt.produktnr to fields.produktnr

There are about 3000 rows in "order" table which need to be updated like this so I need to write a query that'll do the job...

I've searched and read a lot and am finding it quite difficult but I've gathered the following

It requires a mssql loop
and something like the following

DECLARE @colname varchar(20),
@colvalue varchar(20),@colid int

SELECT @colname = fieldname,@colvalue = value 
from fields f,produkt p,order o 
where o.id=p.order_id 
and p.produktnr = f.produktnr 
and o.id = @ID (not sure how but need the order id here)

exec("update order set "+@colname+" = '"+@colvalue+"'
where id = '"+@id+"' ");

I've gotten pretty far but I just don't know how to to...

get the @id in the select/update statement

make the code automatically get all the order id's and increment through them, setting @id for every increment

I would sincerely appreciate any help....
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Updating a table with variable column name, retrieved from another table ?

 
0
  #2
Jul 3rd, 2009
You can use a cursor to iterate for every @id. Here is sample data and code to demonstrate the concept:

  1. IF OBJECT_ID('OrdersTest', 'U') IS NOT NULL DROP TABLE OrdersTest
  2. CREATE TABLE OrdersTest
  3. (
  4. OrderId INT identity(1000, 1) PRIMARY KEY NONCLUSTERED,
  5. CustName VARCHAR(30)
  6. )
  7. GO
  8. INSERT INTO OrdersTest (CustName) VALUES ('Scott')
  9. INSERT INTO OrdersTest (CustName) VALUES ('Bob')
  10. INSERT INTO OrdersTest (CustName) VALUES ('Dave')
  11. INSERT INTO OrdersTest (CustName) VALUES ('Frank')
  12. GO
  13.  
  14. --OK done setting up test data, start the code.
  15. DECLARE @OrderId INT
  16.  
  17. DECLARE det_cursor CURSOR FOR
  18. SELECT OrderId FROM OrdersTest (NOLOCK) ORDER BY OrderId
  19.  
  20. OPEN det_cursor
  21.  
  22. FETCH NEXT FROM det_cursor INTO @OrderId
  23.  
  24. WHILE (@@FETCH_STATUS = 0)
  25. BEGIN
  26. exec('print ' + @OrderId)
  27. FETCH NEXT FROM det_cursor INTO @OrderId
  28. END
  29.  
  30. CLOSE det_cursor
  31. DEALLOCATE det_cursor
Last edited by sknake; Jul 3rd, 2009 at 1:19 pm.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 5
Reputation: rathnakar is an unknown quantity at this point 
Solved Threads: 0
rathnakar rathnakar is offline Offline
Newbie Poster

Re: Updating a table with variable column name, retrieved from another table ?

 
0
  #3
Jul 14th, 2009
hi friend,
followind queries will help u,

update table1 set column1=b.column1 from table1 a,table2 b
where a.column2=b.column2
or
update table1 set column1='ap' where dist in(select dist from tabl2)
regards,
rathnakar


Originally Posted by fatjoez View Post
I have 3 tables, below are their structures
( a bit of pseudo code )

I'm trying to update one table, with a variable column name, retrieved from another table

order
[id,field1,field2]
example row ( '1' , '', '' )

produkt
[order_id,produktnr]
example row ( '1' , '23' )

fields
[produktnr,fieldname,value]
example row ( '23', 'field1', 'abc' )

Example update

set order.field1 = abc

(retrieve column name "field1" from the "fieldname" field in fields table, and value from value in same table, where there is a match from order.id to produkt.id, produkt.produktnr to fields.produktnr

There are about 3000 rows in "order" table which need to be updated like this so I need to write a query that'll do the job...

I've searched and read a lot and am finding it quite difficult but I've gathered the following

It requires a mssql loop
and something like the following

DECLARE @colname varchar(20),
@colvalue varchar(20),@colid int

SELECT @colname = fieldname,@colvalue = value 
from fields f,produkt p,order o 
where o.id=p.order_id 
and p.produktnr = f.produktnr 
and o.id = @ID (not sure how but need the order id here)

exec("update order set "+@colname+" = '"+@colvalue+"'
where id = '"+@id+"' ");

I've gotten pretty far but I just don't know how to to...

get the @id in the select/update statement

make the code automatically get all the order id's and increment through them, setting @id for every increment

I would sincerely appreciate any help....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC