Solved

MS SQL Function issue

Posted on 2013-01-17
2
341 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 250 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

719 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