dbTalk Databases Forums  

using table valued function in a update query

microsoft.public.sqlserver.tools microsoft.public.sqlserver.tools


Discuss using table valued function in a update query in the microsoft.public.sqlserver.tools forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
David Hills
 
Posts: n/a

Default using table valued function in a update query - 03-19-2009 , 10:48 AM






Good after noon

I've succesfully used a scaler valued function in a update query
ie

UPDATE ProductItemDH
SET "ItemId"=get_itemid(ProductItemDH.field1)

But now I need to set serveral fields ie


UPDATE ProductItemDH
SET "ItemId"=get_itemid(ProductItemDH.field1)
SET "desciption"=get_description(ProductItemDH.field1)
SET "value"=get_value(ProductItemDH.field1)

BUT what I really want is a table valued function that returns all three
values
so the update would look some thing like this

UPDATE ProductItemDH
SET ("ItemId",desciption", "value")
=select(get_table_values(ProductItemDH.field1))

where get_table_values is a table value function.

Anyone got an example how to do this?


thanks


David Hills








Reply With Quote
  #2  
Old   
Tom Cooper
 
Posts: n/a

Default Re: using table valued function in a update query - 03-19-2009 , 11:48 AM






You want to use CROSS APPLY (which requires that you are on SQL 2005 or
later), for example,

Create Table dbo.FooTable (field1 int, ItemID int, ItemDescription
varchar(20), ItemValue decimal(9,2));
go

Create Function dbo.FooFunction (@field int)
Returns Table As
Return (Select @field * 10 As ItemIDResult,
'Item Number - ' + Cast(@field As varchar(10)) As ItemDescriptionResult,
@field / 100. As ItemValueResult);
go

Insert dbo.FooTable (field1)
Select 1
Union All Select 256
Union All Select 1475;

Select 'Before', field1, ItemID, ItemDescription, ItemValue
From dbo.FooTable;

Update ft
Set ItemID = f.ItemIDResult,
ItemDescription = f.ItemDescriptionResult,
ItemValue = f.ItemValueResult
From dbo.FooTable ft
Cross Apply dbo.FooFunction(ft.field1) f;

Select 'After', field1, ItemID, ItemDescription, ItemValue
From dbo.FooTable;

Drop Function dbo.FooFunction;
go

Drop Table dbo.FooTable;
go

Tom
"David Hills" <David (AT) discussions (DOT) microsoft.com> wrote

Quote:
Good after noon

I've succesfully used a scaler valued function in a update query
ie

UPDATE ProductItemDH
SET "ItemId"=get_itemid(ProductItemDH.field1)

But now I need to set serveral fields ie


UPDATE ProductItemDH
SET "ItemId"=get_itemid(ProductItemDH.field1)
SET "desciption"=get_description(ProductItemDH.field1)
SET "value"=get_value(ProductItemDH.field1)

BUT what I really want is a table valued function that returns all three
values
so the update would look some thing like this

UPDATE ProductItemDH
SET ("ItemId",desciption", "value")
=select(get_table_values(ProductItemDH.field1))

where get_table_values is a table value function.

Anyone got an example how to do this?


thanks


David Hills









Reply With Quote
  #3  
Old   
David
 
Posts: n/a

Default Re: using table valued function in a update query - 03-20-2009 , 07:19 AM



works great
--
Thanks



David Hills


"Tom Cooper" wrote:

Quote:
You want to use CROSS APPLY (which requires that you are on SQL 2005 or
later), for example,

Create Table dbo.FooTable (field1 int, ItemID int, ItemDescription
varchar(20), ItemValue decimal(9,2));
go

Create Function dbo.FooFunction (@field int)
Returns Table As
Return (Select @field * 10 As ItemIDResult,
'Item Number - ' + Cast(@field As varchar(10)) As ItemDescriptionResult,
@field / 100. As ItemValueResult);
go

Insert dbo.FooTable (field1)
Select 1
Union All Select 256
Union All Select 1475;

Select 'Before', field1, ItemID, ItemDescription, ItemValue
From dbo.FooTable;

Update ft
Set ItemID = f.ItemIDResult,
ItemDescription = f.ItemDescriptionResult,
ItemValue = f.ItemValueResult
From dbo.FooTable ft
Cross Apply dbo.FooFunction(ft.field1) f;

Select 'After', field1, ItemID, ItemDescription, ItemValue
From dbo.FooTable;

Drop Function dbo.FooFunction;
go

Drop Table dbo.FooTable;
go

Tom
"David Hills" <David (AT) discussions (DOT) microsoft.com> wrote in message
news:227095D9-22B8-41B7-98DF-F7D49EC3276C (AT) microsoft (DOT) com...
Good after noon

I've succesfully used a scaler valued function in a update query
ie

UPDATE ProductItemDH
SET "ItemId"=get_itemid(ProductItemDH.field1)

But now I need to set serveral fields ie


UPDATE ProductItemDH
SET "ItemId"=get_itemid(ProductItemDH.field1)
SET "desciption"=get_description(ProductItemDH.field1)
SET "value"=get_value(ProductItemDH.field1)

BUT what I really want is a table valued function that returns all three
values
so the update would look some thing like this

UPDATE ProductItemDH
SET ("ItemId",desciption", "value")
=select(get_table_values(ProductItemDH.field1))

where get_table_values is a table value function.

Anyone got an example how to do this?


thanks


David Hills










Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.