Question : help with Scalar-valued function

I have a simple DB with currently just 2 tables.  the first Table is a list of my products.  I am trying to write a function that will be used in a PERSISTED Computed column on this table that will perform an evaluation against data (if present) from the second table, named Comp.

The code for both tables is attached.

I am trying to create a function that will be used for the formula on dbo.myProducts.RecPrice, but feel I've gotten a little over my head.  This function will grab the lowwest price from dbo.Comp.Price where dbo.Comp.MFG_No = dbo.myProducts.MFG_No

Once it has the value, it will evaluate it somehow.  IF @sqlResult > dbo.myProducts.Cost then dbo.myProducts.RecPrice = @sqlResults
else
dbo.myProducts.RecPrice = dbo.myProducts.Cost + (dbo.myProducts.Cost * 0.05)
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
USE [myCompany]
GO

/****** Object:  Table [dbo].[myProducts]    Script Date: 02/01/2010 08:59:28 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[myProducts](
	[Item_ID] [int] IDENTITY(1,1) NOT NULL,
	[Vendor_NO] [nvarchar](50) NOT NULL,
	[MFG_No] [nvarchar](50) NOT NULL,
	[GSA_Description] [nvarchar](50) NOT NULL,
	[my_Description] [nvarchar](50) NULL,
	[Item_Name] [nvarchar](50) NULL,
	[ExtendedDesc1] [nvarchar](50) NULL,
	[ExtendedDesc2] [nvarchar](50) NULL,
	[ExtendedDesc3] [nvarchar](50) NULL,
	[OMB_MAS] [nchar](10) NULL,
	[ListPrice] [money] NOT NULL,
	[Cost] [money] NOT NULL,
	[UOI] [nchar](10) NOT NULL,
	[COO] [nchar](10) NULL,
	[PPC] [float] NULL,
	[Lowwest] [money] NOT NULL,
	[Average] [money] NOT NULL,
	[RecPrice] [money] NOT NULL,
	[Profit] [money] NOT NULL,
 CONSTRAINT [PK_myProducts] PRIMARY KEY CLUSTERED 
(
	[Item_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

USE [myCompany]
GO

/****** Object:  Table [dbo].[Comp]    Script Date: 02/01/2010 08:57:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Comp](
	[Item_ID] [int] IDENTITY(1,1) NOT NULL,
	[MFG_No] [nvarchar](50) NOT NULL,
	[Comp_Price] [money] NOT NULL,
	[Contract] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Comp] PRIMARY KEY CLUSTERED 
(
	[Item_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Answer : help with Scalar-valued function

to compare something to null you have to use 'is' and 'not is' not an '=' so
when dbo.Comp.Comp_Price is null then dbo.MyProducts.Price *1.12

at the moment you are not using your variable MFG_No so you will get the first row from Comp table each time.

if you are interested in the lowest value simply use MIN()

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
CREATE FUNCTION [dbo].[fnRecPrice]
(
        @MFG_No as nvarchar(50)
)
RETURNS money
AS
BEGIN
        DECLARE @RecPrice money
        SELECT @RecPrice = min( 
                CASE
                        WHEN [dbo].[Comp].[Comp_Price] = NULL THEN [dbo].[myProducts].[Price] * 1.12
                        WHEN [dbo].[Comp].[Comp_Price] > [dbo].[myProducts].[Cost] THEN [dbo].[Comp].[Comp_Price]
                        WHEN [dbo].[Comp].[Comp_Price] < [dbo].[myProducts].[Cost] THEN [dbo].[myProducts].[Cost] * 1.06
                        END)
                FROM [dbo].[Comp]
                INNER JOIN dbo.Comp.Comp_Price
                WHERE [dbo].[Comp].[MFG_No] = [dbo].[myProducts].[MFG_No] and dbo.Comp.MFG_No = @MFG_No
                ORDER BY [dbo].Comp.Comp_Price Desc
        RETURN @RecPrice
END
GO
Random Solutions  
 
programming4us programming4us