Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Tuesday, March 27, 2012

Embedded Code: whats the trick?

OK, I give up -- I can't get this to work. I am trying to put the following function into my report (RS 2005):

Public Shared Function ReportTotal(ByVal IsUnitCost As Boolean,ByVal TotalDirectCosts As Double, ByVal SalaryBenefitsTotal As Double, ByVal IndirectRate As Double, ByVal SS_Screened As Integer, ByVal UnitRate As Double) As Double
If IsUnitCost Then
Return SS_Screened * UnitRate
Else
Return TotalDirectCosts + SalaryBenefitsTotal * IndirectRate
End If
End Function

Supposedly, to use this code you put something like this for the expression in one of your fields: =Code.ReportTotal(...paramaters...)

However, when I type in "Code.", "ReportTotal" is not in the list provided by the Intellisense. If I type it in anyway, there is a red squiggly line under "ReportTotal", and then if I try to preview the report, I get an error: "The definition of report [report name] is invalid. Exception of type 'Microsoft.ReportingServices.ReportProcessing.ReportProcessingException' is thrown".

Real helpful. Does this mean there's an error in the Visual Basic? I tried to access the function improperly? What?

I tried with and without "Public", with and without "Shared". Same result.

Can anyone help??

I'm not familiar yet with 2005, but in 2000, there is a tab in the 'Report Properties' dialogue under the 'Report' menu.

If you put the code in there, IE:

Function FormatDateInterval(ByVal value As DateTime) As String
Return Year(value) & "-" & Month(value)
End Function

You can call the function by using something like:

= Code.FormatDateInterval(First(Fields!SaleDate.Value, "Foreclosures"))

Like I said, it works well in 2000. Hope it helps.

|||

Actually I found the answer. My code was fine -- it just doesn't get compiled until you deploy the report. I was testing it out in "preview" mode, and that's why it wasn't working.

Needless to say, this little feature is not documented anywhere!

sql

Embedded code didnt work

In the code tab of Report Properties window, I pasted the following VB code:

Public Function Divide(Numerator As Double, Denominator As Double)
If Denominator = 0 Then
Return 0
Else
Return Numerator/Denominator
End If
End Function

But when I tried to reference the above function using "Code." in my expression, I saw an "Unrecognized Identifier" when I put my mouse onto this function in the expression edit window. Why? What I did is just copy the above function to the code tab and then start to reference it in the expression, what did I missed? Thanks for your opinion.

Change your function to:

Function Divide(Numerator As Double, Denominator As Double) As Double
If Denominator = 0 Then
Return 0
Else
Return Numerator/Denominator
End If
End Function

When you call it, use:

Code.Divide(10, 5)

Embadded code in VB.Net

Hello everyone!
We have a small problem with one report ,that include VB code.
The code - Vb function ,that connect to Active Directory and return some
value
from DB
When I run my report in local machine I got correctly results,
when I deploy the report - I have #Error.
In the custom code I did all necessary references .
Please help me and sorry for my EnglishOn the server, by default, all code has execute-only privileges. You need to
create a custom assembly to hold this code and then grant addition
permissions to it. See
http://msdn.microsoft.com/SQL/sqlwarehouse/ReportingServices/default.aspx?pull=/library/en-us/dnsql2k/html/dngrfcodeaccesssecurityinsqlserver2000reportingservices.asp.
--
Brian Welcker
Group Program Manager
Microsoft SQL Server
This posting is provided "AS IS" with no warranties, and confers no rights.
"Vikont" <Vikont@.discussions.microsoft.com> wrote in message
news:2328E5C7-7A39-45D4-953E-A79B9B2FF1FD@.microsoft.com...
> Hello everyone!
> We have a small problem with one report ,that include VB code.
> The code - Vb function ,that connect to Active Directory and return some
> value
> from DB
> When I run my report in local machine I got correctly results,
> when I deploy the report - I have #Error.
> In the custom code I did all necessary references .
> Please help me and sorry for my English|||Did you follow the guidance in
http://msdn.microsoft.com/SQL/sqlwarehouse/ReportingServices/default.aspx?pull=/library/en-us/dnsql2k/html/dngrfcodeaccesssecurityinsqlserver2000reportingservices.asp?
--
Brian Welcker
Group Program Manager
Microsoft SQL Server
This posting is provided "AS IS" with no warranties, and confers no rights.
"Vikont" <Vikont@.discussions.microsoft.com> wrote in message
news:2328E5C7-7A39-45D4-953E-A79B9B2FF1FD@.microsoft.com...
> Hello everyone!
> We have a small problem with one report ,that include VB code.
> The code - Vb function ,that connect to Active Directory and return some
> value
> from DB
> When I run my report in local machine I got correctly results,
> when I deploy the report - I have #Error.
> In the custom code I did all necessary references .
> Please help me and sorry for my English

Sunday, February 26, 2012

else if statement


I have this function and is given me the error below the code and want to know which is my mistake

create function pt
(@.idpt INT,
@.option INT)
Returns Real
AS
Begin
if (@.option=1)
Begin
Return ( select sqrt(a.px*a.px + a.py*a.py)
from electron as a
where @.idpt = a.idap)
end
else if(@.option=2)
Begin
Return ( select sqrt(a.px*a.px + a.py*a.py)
from muon as a
where @.idpt = a.idap)
End
else
Begin
Return ( select sqrt(a.px*a.px + a.py*a.py)
from jet as a
where @.idpt = a.idap)
end
end

Msg 455, Level 16, State 2, Procedure pt, Line 22
The last statement included within a function must be a return statement.
Try putting a dummy "RETURN NULL" after the large IF block. I think the compiler isn't realizing that at least one of the 3 return statements you've got will always be called, and it's throwing a fit. The extra RETURN will never get called, but it'll at least keep the compiler happy.
|||

Try the code below.

Chris

Code Snippet

CREATE FUNCTION pt (@.idpt INT, @.option INT)

RETURNS REAL

AS

BEGIN

DECLARE @.Output REAL

IF (@.option = 1)

BEGIN

SELECT @.Output = SQRT(a.px * a.px + a.py * a.py)

FROM electron AS a

WHERE @.idpt = a.idap

END

ELSE

IF (@.option = 2)

BEGIN

SELECT @.Output = SQRT(a.px * a.px + a.py * a.py)

FROM muon AS a

WHERE @.idpt = a.idap

END

ELSE

BEGIN

SELECT @.Output = SQRT(a.px * a.px + a.py * a.py)

FROM jet AS a

WHERE @.idpt = a.idap

END

RETURN @.Output

END

Friday, February 24, 2012

Elegant alternative to table-valued function

I have a table, call it Liquidity, that is the result of joins of other
table-values functions, tables, and subqueries. Now I want to create a
table-valued function (or some parameterized query) called GetLiquidityInfo
to return a view in which each row contains all the columns from Liquidity
plus three additional columns, LockupDate, NoticeDate, and RedemptionDate.
Now, these three columns are calculated based on values from the columns
that come from Liquidity (Liquidity.*) plus an additional parameter that is
supplied from the user. I might use the results from GetLiquidityInfo to
generate a report, or I might need to use it as a source for further
queries.
My question is: what is the most elegant way to achieve GetLiquidityInfo? I
am hoping this can be answered without schema and code, which is
proprietary.
I had initially implemented each of the columns (LockupDate, NoticeDate,
RedemptionDate) as three scalar-valued functions. GetLockupDate produced
LockupDate, which was then supplied to GetRedemptionDate, which produced
RedemptionDate, which was then passed to GetNoticeDate to get NoticeDate.
In each of these function calls, additional parameters came from Liquidity.
Because each function returned a scalar value, I was able to create
GetLiquidityInfo as an inline table-valued function with several nested
queries. The innermost query got the columns from Liquidity; the next query
added the result of GetLockupDate as a column; the next query added the
result of GetRedemptionDate as a column; etc. The nested queries allowed me
to use the columns from the inner select statement as parameters to the
scalar-valued function in the outer select statement.
Now, ly, the specifications have changed, and the three functions cannot
be chained together in this way. They are chained together like this now:
GetRedemptionDate --> GetLockupDate --> GetRedemptionDate --> GetNoticeDate
(That's right, GetRedemptionDate must be called twice.) And the logic of
the processing is more complex, so that nested queries don't work so well.
My first thought was to create a table valued function GetLiquidityDates to
return all three dates at once. I planned to JOIN Liquidity to
GetLiquidityDates(Liquidity.a, Liquidity.b, Liquidity.c, ...) but apparently
I cannot refer to columns from another table in the same join. I had hoped
SQL Server would process GetLiquidityDates as it does for a correlated
subquery, but it doesn't. I have considered using a cursor to evaluate
GetLiquidityDates for each row in Liquidity, and manually constucting the
result table, but I am not sure this is the best way to go.
Any suggestions?dustbort wrote:
> I have a table, call it Liquidity, that is the result of joins of other
> table-values functions, tables, and subqueries. Now I want to create a
> table-valued function (or some parameterized query) called GetLiquidityInf
o
> to return a view in which each row contains all the columns from Liquidity
> plus three additional columns, LockupDate, NoticeDate, and RedemptionDate.
> Now, these three columns are calculated based on values from the columns
> that come from Liquidity (Liquidity.*) plus an additional parameter that i
s
> supplied from the user. I might use the results from GetLiquidityInfo to
> generate a report, or I might need to use it as a source for further
> queries.
> My question is: what is the most elegant way to achieve GetLiquidityInfo?
I
> am hoping this can be answered without schema and code, which is
> proprietary.
> I had initially implemented each of the columns (LockupDate, NoticeDate,
> RedemptionDate) as three scalar-valued functions. GetLockupDate produced
> LockupDate, which was then supplied to GetRedemptionDate, which produced
> RedemptionDate, which was then passed to GetNoticeDate to get NoticeDate.
> In each of these function calls, additional parameters came from Liquidity
.
> Because each function returned a scalar value, I was able to create
> GetLiquidityInfo as an inline table-valued function with several nested
> queries. The innermost query got the columns from Liquidity; the next que
ry
> added the result of GetLockupDate as a column; the next query added the
> result of GetRedemptionDate as a column; etc. The nested queries allowed
me
> to use the columns from the inner select statement as parameters to the
> scalar-valued function in the outer select statement.
> Now, ly, the specifications have changed, and the three functions canno
t
> be chained together in this way. They are chained together like this now:
> GetRedemptionDate --> GetLockupDate --> GetRedemptionDate --> GetNoticeDat
e
> (That's right, GetRedemptionDate must be called twice.) And the logic of
> the processing is more complex, so that nested queries don't work so well.
> My first thought was to create a table valued function GetLiquidityDates t
o
> return all three dates at once. I planned to JOIN Liquidity to
> GetLiquidityDates(Liquidity.a, Liquidity.b, Liquidity.c, ...) but apparent
ly
> I cannot refer to columns from another table in the same join. I had hope
d
> SQL Server would process GetLiquidityDates as it does for a correlated
> subquery, but it doesn't. I have considered using a cursor to evaluate
> GetLiquidityDates for each row in Liquidity, and manually constucting the
> result table, but I am not sure this is the best way to go.
> Any suggestions?
Not without a proper repro. Your code may be proprietary but surely you
could create a hypothetical example.
It seems to me it ought to be possible to calculate these results
in-line in a query unless they are somehow recursive. Whether they are
recursive calculations or not is far from clear to me based on your
narrative.
Please also tell us what version of SQL Server you are using. In SQL
Server 2005 you may be able to make use of the CROSS APPLY operator.
David Portas
SQL Server MVP
--|||Here is the definition for the actual function that I refered to earlier as
GetLiquidityInfo. Below it is the definition for getLiquidityDates. My
logical table Liquidity roughly corresponds to @.activeTranches. (The
function is not inline because it actually runs faster when @.activetranches
is queried out first, because of the sources of the underlying data.
The error I get is "Line 140: Incorrect syntax near 'AT'" which is the
second argument of getLiquidityDates().
---
CREATE FUNCTION [dbo].[getLiquiditySchedule2]
(
@.asOfDate SMALLDATETIME
)
RETURNS @.liquiditySchedule TABLE
(
[valuationDate] SMALLDATETIME,
[valuationVersion] INT,
[value] FLOAT,
[rcgFundId] INT,
[rcgFundName] VARCHAR(100),
[mgrFundId] INT,
[managerName] VARCHAR(100),
[shareClass] VARCHAR(50),
[investmentDate] SMALLDATETIME,
[investmentAmount] FLOAT,
[lockupPeriodValue] INT,
[lockupPeriodUnit] VARCHAR(50),
[noticePeriodValue] INT,
[noticePeriodUnit] VARCHAR(50),
[redemptionFrequencyValue] INT,
[redemptionFrequencyUnit] VARCHAR(50),
[redemptionFrequencyBasisDate] SMALLDATETIME,
[lockupEndDate] SMALLDATETIME,
[latestNoticeDate] SMALLDATETIME,
[nextRedemptionDate] SMALLDATETIME,
[fractionRedeemed] FLOAT,
[fractionRemaining] FLOAT,
[totalInvestmentAmount] FLOAT,
[trancheInvestmentFraction] FLOAT,
[trancheValue] FLOAT
)
AS
BEGIN
DECLARE @.activeTranches TABLE
(
[rcgFundId] INT,
[mgrFundId] INT,
[shareClass] VARCHAR(50),
[investmentDate] SMALLDATETIME,
[investmentAmount] FLOAT,
[noticePeriodValue] INT,
[noticePeriodUnit] VARCHAR(50),
[redemptionFrequencyValue] INT,
[redemptionFrequencyUnit] VARCHAR(50),
[redemptionFrequencyBasisDate] SMALLDATETIME,
[lockupPeriodValue] INT,
[lockupPeriodUnit] VARCHAR(50),
[lockupEndFrequencyValue] INT,
[lockupEndFrequencyUnit] VARCHAR(50),
[lockupEndBasisDate] SMALLDATETIME,
[fractionRedeemed] FLOAT,
[fractionRemaining] FLOAT,
[valuationDate] SMALLDATETIME,
[valuationVersion] INT,
[value] FLOAT
)
INSERT INTO @.activeTranches
SELECT *
FROM getActiveTranches_optimized(@.asOfDate)
INSERT INTO @.liquiditySchedule
SELECT
[AT].[valuationDate],
[AT].[valuationVersion],
[AT].[value],
[AT].[rcgFundId],
[RFN].[rcgFundName],
[AT].[mgrFundId],
[MN].[managerName],
[AT].[shareClass],
[AT].[investmentDate],
[AT].[investmentAmount],
[AT].[lockupPeriodValue],
[AT].[lockupPeriodUnit],
[AT].[noticePeriodValue],
[AT].[noticePeriodUnit],
[AT].[redemptionFrequencyValue],
[AT].[redemptionFrequencyUnit],
[AT].[redemptionFrequencyBasisDate],
[LD].[lastLockupEndDate],
[LD].[nextNoticeDate],
[LD].[nextRedemptionDate],
[AT].[fractionRedeemed],
[AT].[fractionRemaining],
[AT_TOTAL].[totalInvestmentAmount],
[trancheInvestmentFraction] = [AT].[investmentAmount] /
[AT_TOTAL].[totalInvestmentAmount]
FROM
@.activeTranches [AT]
INNER JOIN RcgFundNames [RFN]
ON [AT].[rcgFundId] = [RFN].[rcgFundId]
INNER JOIN ManagerNames [MN]
ON [AT].[mgrFundId] = [MN].[managerId]
INNER JOIN (
SELECT
[rcgFundId],
[mgrFundId],
[shareClass],
[totalInvestmentAmount] = SUM([investmentAmount])
FROM
@.activeTranches
GROUP BY
[rcgFundId],
[mgrFundId],
[shareClass]
) [AT_TOTAL]
ON [AT].[rcgFundId] = [AT_TOTAL].[rcgFundId]
AND [AT].[mgrFundId] = [AT_TOTAL].[mgrFundId]
AND ISNULL([AT].[shareClass], '') = ISNULL([AT_TOTAL].[shareClass], '')
INNER JOIN
getLiquidityDates(
@.asOfDate,
[AT].[investmentDate],
[AT].[noticePeriodValue],
[AT].[noticePeriodUnit],
[AT].[redemptionFrequencyValue],
[AT].[redemptionFrequencyUnit],
[AT].[redemptionFrequencyBasisDate],
[AT].[lockupPeriodValue],
[AT].[lockupPeriodUnit],
[AT].[lockupEndFrequencyValue],
[AT].[lockupEndFrequencyUnit],
[AT].[lockupEndFrequencyBasisDate],
[AT].[rolloverFrequencyValue],
[AT].[rolloverFrequencyUnit],
[AT].[rolloverFrequencyBasisDate]) [LD]
RETURN
END
GO
----
CREATE FUNCTION [dbo].[getLiquidityDates]
(
@.asOfDate SMALLDATETIME,
@.investmentDate SMALLDATETIME,
@.noticePeriodValue INT,
@.noticePeriodUnit VARCHAR(50),
@.redemptionFrequencyValue INT,
@.redemptionFrequencyUnit VARCHAR(50),
@.redemptionFrequencyBasisDate SMALLDATETIME,
@.lockupPeriodValue INT,
@.lockupPeriodUnit VARCHAR(50),
@.lockupEndFrequencyValue INT,
@.lockupEndFrequencyUnit VARCHAR(50),
@.lockupEndFrequencyBasisDate SMALLDATETIME,
@.rolloverFrequencyValue INT,
@.rolloverFrequencyUnit VARCHAR(50),
@.rolloverFrequencyBasisDate SMALLDATETIME
)
RETURNS @.liquidityDates TABLE
(
[lastLockupEndDate] SMALLDATETIME,
[nextNoticeDate] SMALLDATETIME,
[nextRedemptionDate] SMALLDATETIME
)
AS
BEGIN
DECLARE @.nextRedemptionDate SMALLDATETIME
SET @.nextRedemptionDate = dbo.getNextRedemptionDate(
@.asOfDate,
@.noticePeriodValue,
@.noticePeriodUnit,
@.redemptionFrequencyBasisDate,
@.redemptionFrequencyValue,
@.redemptionFrequencyUnit)
DECLARE @.lastLockupEndDate SMALLDATETIME
SET @.lastLockupEndDate = dbo.getLastLockupEndDate(
@.nextRedemptionDate,
@.investmentDate,
@.lockupPeriodValue,
@.lockupPeriodUnit,
@.lockupEndFrequencyBasisDate,
@.lockupEndFrequencyValue,
@.lockupEndFrequencyUnit,
@.rolloverFrequencyBasisDate,
@.rolloverFrequencyValue,
@.rolloverFrequencyUnit)
IF (@.nextRedemptionDate < @.lastLockupEndDate)
BEGIN
-- The projected next redemption date actually occurs during a
-- lockup period.
-- Assume there is at least one real redemption period between
-- rolling lockup periods.
SET @.nextRedemptionDate = dbo.getNextRedemptionDate(
@.lastLockupEndDate,
NULL, -- Don't count notice period; it occurs during lockup.
NULL,
@.redemptionFrequencyBasisDate,
@.redemptionFrequencyValue,
@.redemptionFrequencyUnit)
-- This next redemption date is guaranteed to be after the
-- first one, so sufficient notice period is also guaranteed.
END
DECLARE @.nextNoticeDate SMALLDATETIME
SET @.nextNoticeDate = dbo.DateAddVar(
@.noticePeriodUnit,
-@.noticePeriodValue,
@.nextRedemptionDate)
INSERT INTO
@.liquidityDates
SELECT
@.lastLockupEndDate,
@.nextNoticeDate,
@.nextRedemptionDate
RETURN
END
GO
---
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1136398951.939350.98470@.g47g2000cwa.googlegroups.com...
> dustbort wrote:
> Not without a proper repro. Your code may be proprietary but surely you
> could create a hypothetical example.
> It seems to me it ought to be possible to calculate these results
> in-line in a query unless they are somehow recursive. Whether they are
> recursive calculations or not is far from clear to me based on your
> narrative.
> Please also tell us what version of SQL Server you are using. In SQL
> Server 2005 you may be able to make use of the CROSS APPLY operator.
> --
> David Portas
> SQL Server MVP
> --
>|||I forgot to say that I am using
Microsoft SQL Server 2000 - 8.00.818 (Intel X86)
May 31 2003 16:08:15
Copyright (c) 1988-2003 Microsoft Corporation
Standard Edition on Windows NT 5.0 (Build 2195: Service Pack 4)
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1136398951.939350.98470@.g47g2000cwa.googlegroups.com...
> dustbort wrote:
> Not without a proper repro. Your code may be proprietary but surely you
> could create a hypothetical example.
> It seems to me it ought to be possible to calculate these results
> in-line in a query unless they are somehow recursive. Whether they are
> recursive calculations or not is far from clear to me based on your
> narrative.
> Please also tell us what version of SQL Server you are using. In SQL
> Server 2005 you may be able to make use of the CROSS APPLY operator.
> --
> David Portas
> SQL Server MVP
> --
>|||On Wed, 4 Jan 2006 14:31:50 -0500, "dustbort" <dustbort at yahoo dot
com> wrote:

>Here is the definition for the actual function that I refered to earlier as
>GetLiquidityInfo. Below it is the definition for getLiquidityDates. My
>logical table Liquidity roughly corresponds to @.activeTranches. (The
>function is not inline because it actually runs faster when @.activetranches
>is queried out first, because of the sources of the underlying data.
>The error I get is "Line 140: Incorrect syntax near 'AT'" which is the
>second argument of getLiquidityDates().
(snip)
Hi dustbort,
The error is here:

> INNER JOIN
> getLiquidityDates(
> @.asOfDate,
> [AT].[investmentDate],
> [AT].[noticePeriodValue],
> [AT].[noticePeriodUnit],
> [AT].[redemptionFrequencyValue],
> [AT].[redemptionFrequencyUnit],
> [AT].[redemptionFrequencyBasisDate],
> [AT].[lockupPeriodValue],
> [AT].[lockupPeriodUnit],
> [AT].[lockupEndFrequencyValue],
> [AT].[lockupEndFrequencyUnit],
> [AT].[lockupEndFrequencyBasisDate],
> [AT].[rolloverFrequencyValue],
> [AT].[rolloverFrequencyUnit],
> [AT].[rolloverFrequencyBasisDate]) [LD]
What you try to do is not permitted in SQL Server 2000. If you join to a
table-valued UDF in a query, then the arguments of that UDF can't be
columns from the other tables in the query.
Logically (*), the steps taken to resolve a join to a table-valued UDF
are:
1. Materialize the UDF (i.e. execute it and store the results);
2. Check rows from UDF to rows from other table(s) in the JOIN and
evaluate ON condition to see which combinations will be retained.
The [AT].[ColumnName] arguments can only be replaced by a single value
after performing step 2, yet step 1 already requires single values.
(*) In reality, SQL Server will prefer more efficient paths of
execution, when available; this explanation is purely intended to
explain the logic of this limitation.
Note that SQL Server 2005 introduces the CROSS APPLY operator that will
permit you to do things like this.
http://msdn2.microsoft.com/en-us/library/ms175156.aspx
Hugo Kornelis, SQL Server MVP