You are here: Re: Syntax to add records if primary key = list « MsSQL Server « IT news, forums, messages
Re: Syntax to add records if primary key = list

Posted by Alwik on 09/22/05 00:16

> I am new to SQL administration.
>
> >From a list of IDs that are the primary key in one table (i.e. Customer
> Table), I want to make changes in tables that use those IDs as a
> foreign key.
>
> Basically I want to say:
>
> If fk_ID is in list [1,2,3,4,5] then
> do these statements to that record
> End if
>
> Where do I begin?
>
> Thanks for help with this low-level view of SQL programming.
>
Let's try a simple example

Customers table has a field cust_id, orders table has field ord_freight and
ord_custId which is FK.
When you want to update ord_freight for known customer ids you can execute:

UPDATE orders SET ord_freight = 15 WHERE ord_custId IN (1,2,3,4,5)

but let's assume that you know only names of cusomers - then you can use
joins and filter orders using using names and relations between tables.
I think that the best place to start reading are MS SQL Server books
online - look for topic: update (described) - there are some examples etc.
You can use cursors as well and process records on a one by one basis, but I
think that update will solve many of your problems.
I hope this helps

Tomik

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация