| | |
Updating a table with variable column name, retrieved from another table ?
Please support our MS SQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2009
Posts: 1
Reputation:
Solved Threads: 0
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
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....
( 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....
You can use a cursor to iterate for every @id. Here is sample data and code to demonstrate the concept:
sql Syntax (Toggle Plain Text)
IF OBJECT_ID('OrdersTest', 'U') IS NOT NULL DROP TABLE OrdersTest CREATE TABLE OrdersTest ( OrderId INT identity(1000, 1) PRIMARY KEY NONCLUSTERED, CustName VARCHAR(30) ) GO INSERT INTO OrdersTest (CustName) VALUES ('Scott') INSERT INTO OrdersTest (CustName) VALUES ('Bob') INSERT INTO OrdersTest (CustName) VALUES ('Dave') INSERT INTO OrdersTest (CustName) VALUES ('Frank') GO --OK done setting up test data, start the code. DECLARE @OrderId INT DECLARE det_cursor CURSOR FOR SELECT OrderId FROM OrdersTest (NOLOCK) ORDER BY OrderId OPEN det_cursor FETCH NEXT FROM det_cursor INTO @OrderId WHILE (@@FETCH_STATUS = 0) BEGIN exec('print ' + @OrderId) FETCH NEXT FROM det_cursor INTO @OrderId END CLOSE det_cursor DEALLOCATE det_cursor
Last edited by sknake; Jul 3rd, 2009 at 1:19 pm.
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Solved Threads: 0
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
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
•
•
•
•
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....
![]() |
Similar Threads
- How to Get Column names of a table of MS Access (.mdb)- ODBC Connection (C#)
- Collect sum of a column in a html table? (JavaScript / DHTML / AJAX)
- sqlite3-- how to see column names for table (Python)
- How to read the data from Table Variable (MS SQL)
- Add Column to Access Table with Macro (MS Access and FileMaker Pro)
- alter table add column through Dreamweaver ... need to get this to work ASAP! :( (ASP)
- add a new table to the database or add a new column to an existing table. (MS SQL)
Other Threads in the MS SQL Forum
- Previous Thread: Update table row without firing trigger?
- Next Thread: Using IF ELSE in script
Views: 1327 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last age autogrowth business connectingtodatabaseinuse count cursor data database dateadd datediff datepart day" dbsize deadlock delete_trigger exploit getdate hack highperformancecomputing hpc hpcserver2008 ibm iis limit live loop maximum microsoft ms mssql multiple multithreading news number password permission position query reporting result security server services sets single source sql sql-injection sqlserver sqlserver2005 subtype supercomputing supertype tables uniqueid update view weekday






