Solved

MS SQL Function issue

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL STANDARD CORE 8 38
First Max value 3 30
SQL Group By Question 4 20
Help with my first SQL CLR table value function 2 5
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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

828 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