question

Thunderbolts avatar image
Thunderbolts asked

Newbie SQl query

Hi there, This is probable very simple for most users on here: If I have a table in a database called StockProfiles and I wanted to add one to the StockProfile.Inventory_age if the Stock.Datepur is less or equla to 31/01/2011 What would be the correct t-sql statment and Sql Server Agent job (I assume these would both be the same?) T..
sql-server-2005
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Magnus Ahlkvist avatar image
Magnus Ahlkvist answered
You didn't reveal much about your tables. It seems like you have two tables, Stock and StockProfiles. I'm going to guess there's a primary key in **Stock** called StockID and that there's a foreign key in **StockProfiles**, also called StockID. UPDATE StockProfiles SET Inventory_Age = Inventory_Age + 1 FROM StockProfiles p INNER JOIN Stock s ON p.StockID = s.StockID WHERE Stock.Datepur
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fatherjack avatar image
Fatherjack answered
So, to rephrase your question ... you want to `UPDATE` your `StockProfile.Inventory_age` `WHERE` the `Stock.DatePur` is `Less than or equal` to '31-Jan-2011'. That translates into a piece of code that would look like UPDATE StockProfile SET Inventory_age = Inventory_age + 1 FROM StockProfile INNER JOIN Stock on StockProfile.ID_Col = Stock.ID_Col WHERE Stock.DatePur
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.