Sunday, February 19, 2012

How Can i Update many fields by using SQL

I got myInvTable, myCompanyTable,
I need to update
CompanyAdd1,CompanyAdd2,CompanyAdd3,Comp
anyAdd4,CompanyTel,CompanyFax,Compan
yContact.
How can I write it by one SQL statment '
Thanks a lotYou can update only one table at a time in a single Transact-SQL statement.
It is unclear from your post what columns belong to which tables. Single
table UPDATE example:
UPDATE myCompanyTable
SET
CompanyAdd1 = 'address 1',
CompanyAdd2 = 'address 2',
CompanyAdd3 = 'address 3',
CompanyAdd4 = 'address 14',
CompanyTel = 'tel 1',
CompanyFax = 'fax 1',
CompanyContact' = 'contact 1'
WHERE myCompanyTableID = 1
Hope this helps.
Dan Guzman
SQL Server MVP
"Agnes" <agnes@.dynamictech.com.hk> wrote in message
news:%23U2uIF%23EFHA.1408@.TK2MSFTNGP10.phx.gbl...
>I got myInvTable, myCompanyTable,
> I need to update
> CompanyAdd1,CompanyAdd2,CompanyAdd3,Comp
anyAdd4,CompanyTel,CompanyFax,Comp
anyContact.
> How can I write it by one SQL statment '
> Thanks a lot
>|||OH, I need to update myInvtable indeed. update the information from the
companytable
Can I
update myinvTable
set companyadd1 = myCompanyTable.companyadd1,
companyadd2 = myCompanyTable.companyadd2,
....etc
where .. myInvTable.companycode = myCompanyTable.companycode ?
Thanks
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> glsD:OZroCL%23EFHA.1936@.TK2MS
FTNGP14.phx.gbl...
> You can update only one table at a time in a single Transact-SQL
> statement. It is unclear from your post what columns belong to which
> tables. Single table UPDATE example:
> UPDATE myCompanyTable
> SET
> CompanyAdd1 = 'address 1',
> CompanyAdd2 = 'address 2',
> CompanyAdd3 = 'address 3',
> CompanyAdd4 = 'address 14',
> CompanyTel = 'tel 1',
> CompanyFax = 'fax 1',
> CompanyContact' = 'contact 1'
> WHERE myCompanyTableID = 1
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Agnes" <agnes@.dynamictech.com.hk> wrote in message
> news:%23U2uIF%23EFHA.1408@.TK2MSFTNGP10.phx.gbl...
>|||You can use subqueries to solve this problem
update myinvTable
set companyadd1 = (select companyadd1 from myCompanyTable where
companycode=.... ),
companyadd2 = (select companyadd2 from myCompanyTable where companycode=
.... ),,....etc
where companycode = ....
"Agnes" <agnes@.dynamictech.com.hk> wrote in message
news:OaPdiP%23EFHA.1936@.TK2MSFTNGP14.phx.gbl...
> OH, I need to update myInvtable indeed. update the information from the
> companytable
> Can I
> update myinvTable
> set companyadd1 = myCompanyTable.companyadd1,
> companyadd2 = myCompanyTable.companyadd2,
> ....etc
> where .. myInvTable.companycode = myCompanyTable.companycode ?
> Thanks
> "Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net>
glsD:OZroCL%23EFHA.1936@.TK2MSFTNGP14.phx.gbl...
CompanyAdd1,CompanyAdd2,CompanyAdd3,Comp
anyAdd4,CompanyTel,CompanyFax,Compan
yContact.
>|||On Wed, 16 Feb 2005 12:54:27 +0800, Agnes wrote:

>OH, I need to update myInvtable indeed. update the information from the
>companytable
>Can I
(snip)
Hi Agnes,
You can, but you shouldn't. You'd be storing redundant data in your
database.
It's far better to join the Invoices table to the Companies table in the
code that prints invoices.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)

No comments:

Post a Comment