?
Solved

MS SQL Function issue

Posted on 2013-01-17
2
Medium Priority
?
348 Views
Last Modified: 2013-01-17
Hi guys,

I'm new to SQL functions, and have been using them create a few "computed column" in a table for an online "competition" generating points score.

I have an issue in that the function is constantly returning a count of 0, when it should be responding back with a number...

I have used the following script to create the function..

CREATE FUNCTION [dbo].[FBCount] (@TeamID NVARCHAR)
RETURNS NVARCHAR
AS BEGIN
    DECLARE @FBCount INT

    SELECT @FBCount = COUNT(DISTINCT fbuser) FROM dbo.FBtable WHERE fbcode = TeamID

    RETURN @FBCount
END

And added ([dbo].[FBCount]([RandomCode])) to the computed column formula - where "RandomCode" is a 20 character random string - an example of this is OVU9ANYL0CBQ0NBVMFIL.  Randomcode is a column of NVARCHAR(255)

If I just try a sql "select" using SELECT COUNT(DISTINCT fbuser) FROM dbo.FBtable WHERE fbcode = 'OVU9ANYL0CBQ0NBVMFIL' it works fine.

So where am I going wrong??? is it something to do with quotes?

Thanks
0
Comment
Question by:BenjyAdams
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1000 total points
ID: 38788090
your input type is nvarchar, which is equivalent to nvarchar(1)  it should be nvarchar(255)

CREATE FUNCTION [dbo].[FBCount] (@TeamID NVARCHAR(255) )
RETURNS int
AS BEGIN
    DECLARE @FBCount INT

    SELECT @FBCount = COUNT(DISTINCT fbuser) FROM dbo.FBtable WHERE fbcode = TeamID

    RETURN @FBCount
END
0
 

Author Closing Comment

by:BenjyAdams
ID: 38788259
Bingo - thanks....so simple...but had stumped me :)
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question