Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to: Add stored procedure to column of query?

Author  Topic 

jp2code
Posting Yak Master

175 Posts

Posted - 2010-03-01 : 14:05:31
I'd like to use a stored procedure's single value output as a parameter in a select statement.

My stored procedure is called "sp_GetBadge(@FullName nVarChar(255))" and it returns the first matching employee badge number that it encounters.

I can not, however, seem to find a way to make it work in this type of statement:
SELECT FirstName, LastName, dbo.sp_GetBadge(FirstName+' '+LastName) As 'Badge'
FROM EmployeeTable
I have tried using the stored procedure 'sp_GetBadge' with with and without the parenthesis, but my SQL error is either "Invalid object name 'dbo.sp_GetBadge'." (with parenthesis) or "Incorrect syntax near '+'." (without parenthesis).

Thanks in advance,
~Joe


Avoid Sears Home Improvement

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-03-01 : 14:14:40
You could do it that way if sp_GetBadge were a function instead of a sproc. Also, it's not a good idea to name your sprocs sp_, SQL Server will think it's a system sproc.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

jp2code
Posting Yak Master

175 Posts

Posted - 2010-03-01 : 14:33:16
Hi Jim,

I tried creating a function for this initially and just retried at your suggestion. However, I can't get it to store because of an error with "Incorrect syntax near the keyword 'select'." ...which is where I tell it to "SELECT TOP 1" - it just won't do that!

Any ideas how to solve this?
CREATE FUNCTION fn_GetBadge(@FullName nVarChar(255)) RETURNS nVarChar(255) AS
BEGIN
-- Declare the return variable here
declare @BadgeNum nVarChar(255)
-- Declare other variables needed
declare @FirstName nVarChar(255), @LastName nVarChar(255)
declare @index int, @space nVarChar(1), @lastChar nVarChar(1)

-- Add the T-SQL statements to compute the return value here
set @space=' ' -- constant - do not change
set @FirstName='' -- initialize just in case there are no spaces!
set @LastName=LTrim(RTrim(@FullName))
set @index=CharIndex(@space, @LastName)
if (0 < @index) Begin
set @FirstName=SubString(@LastName, 1, @index - 1)
set @index=Len(@LastName)-1
set @lastChar=SubString(@LastName, @index, 1) -- starts at the end...
while (@lastChar!=@space) begin
set @index=@index-1 -- ...and walks back one char at a time
set @lastChar=SubString(@LastName, @index, 1)
end
set @LastName=SubString(@LastName, @index+1, Len(@LastName))
end
if (@LastName!='') begin
set @FirstName=@FirstName + '%'
set @LastName='%' + @LastName
set @BadgeNum=select top 1 NUM
from EmployeeInfo
where (FIRSTNAME Like @FirstName) AND (LASTNAME Like @LastName)
order by [COUNT] desc
end else begin
set @BadgeNum=@LastName
end
return @BadgeNum -- Return the result of the function
END
GO

Regards,
~Joe


Avoid Sears Home Improvement
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-03-01 : 14:45:04
If your function compiled, it could be in your select statement
SELECT dbo.fn_GetBadge(FirstName+' '+LastName)

Everyday I learn something that somebody else already knew
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-03-01 : 17:57:37
Try wrapping the SELECT in parenthesis:
set @BadgeNum = (select top 1 NUM
from EmployeeInfo
where (FIRSTNAME Like @FirstName) AND (LASTNAME Like @LastName)
order by [COUNT] desc)
Go to Top of Page

jp2code
Posting Yak Master

175 Posts

Posted - 2010-03-02 : 09:04:10
Thanks Lamprey! That got it.

Jimf, it seems like the stored function works every bit as good as the stored procedure I wanted, so I am happy!

Thanks to both!


Avoid Sears Home Improvement
Go to Top of Page
   

- Advertisement -