Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jwmayo
Starting Member
28 Posts |
Posted - 2010-08-18 : 11:42:53
|
Wondering if this is possible. I have sales table with product and customer information in it.. Sales File:Customer#, PROD#I want to create a function that based on thecustomer# and prod# in the sales file it looks up the Discount in a discount table that contains:Discount File:Customer#, Prod#, DiscountResults:Sales file:Customer#, prod#, DiscountAnd again I need to be able to do this in a function. Thanks |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-08-18 : 11:57:26
|
This can be achieved with select statement directly.Select S.Customer, S.Prod, D.Discount fromSales S inner join --or Use left join for getting all records from Sales tableDiscount D on S.Customer = D.Customer and S.Prod = D.ProdIf any other columns also needs to be considered for discount then post the complete DDL statement.Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
jwmayo
Starting Member
28 Posts |
Posted - 2010-08-18 : 12:01:12
|
agreed it can be in just a select statement.. But need it in a function.. long story |
 |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-08-18 : 12:11:50
|
Try this:Create function GetDiscount(@Customer int, @PROD int) Returns intasBeginDeclare @DiscAmt int--Assuming only 1 discount exist for customer and product combination Select @DiscAmt = D.Discount from Discount D where d.Customer = @Customer and D.Prod = @PRODReturn @DiscAmtEndRegards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
jwmayo
Starting Member
28 Posts |
Posted - 2010-08-18 : 12:13:14
|
Thank you |
 |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-08-18 : 12:19:06
|
quote: Originally posted by jwmayo Thank you
You are welcome. I am here to learn from Masters and help new bees in learning. |
 |
|
jwmayo
Starting Member
28 Posts |
Posted - 2010-08-18 : 12:32:19
|
Column is populated with 0's.. this is what I have.. Any thoughtsUSE [Dailysales]GO/****** Object: UserDefinedFunction [dbo].[discountSO] Script Date: 08/18/2010 12:22:45 ******/SET ANSI_NULLS OFFGOSET QUOTED_IDENTIFIER ONGOALTER FUNCTION [dbo].[discountSO] ( @newmarket varchar(25), @company varchar(25)) RETURNS intASBEGIN declare @discAmt int select @discAmt = Discounttable.Sfrom Discounttable where Discounttable.[new market] = @newmarket and discounttable.company = @companyReturn @discAmt END |
 |
|
jwmayo
Starting Member
28 Posts |
Posted - 2010-08-18 : 12:40:57
|
I got it.. Wrong data type |
 |
|
|
|
|