October 23, 2013 at 8:07 am
Write a Stored Procedure utilizing a function that receives a LocationID and determines if the LocationID is an even or
an odd number producing
October 23, 2013 at 8:19 am
josephmoka (10/23/2013)
Write a Stored Procedure utilizing a function that receives a LocationID and determines if the LocationID is an even oran odd number producing
This sounds like homework. We like to help people around here, but we don't like to do homework. We will be happy to assist you but if we do your homework you don't learn anything.
What have you tried so far?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 24, 2013 at 10:54 am
CREATE FUNCTION ODD_or_Even
(@Digit nvarchar(3))
RETURNS varchar(4)
AS BEGIN declare @Return varchar(30)
select @return = CASE WHEN
CAST(@Digit as decimal)/CAST(2 AS decimal)NOT LIKE '%.0%' THEN 'Odd' ELSE 'Even' END
return
@return
end
SELECT dbo.ODD_or_Even(3)
October 24, 2013 at 11:05 am
If you were asked to write a stored procedure, why would you create a function? :hehe:
October 24, 2013 at 11:49 am
Lol, I was half way there, Write a Stored Procedure utilizing a "function".
CREATE FUNCTION ODD_or_Even
(@Digit nvarchar(3))
RETURNS varchar(4)
AS BEGIN declare @Return varchar(30)
select @return = CASE WHEN
CAST(@Digit as decimal)/CAST(2 AS decimal)NOT LIKE '%.0%' THEN 'Odd' ELSE 'Even' END
return
@return
end
CREATE PROCEDURE AmIOddorEven
@Digit nvarchar (3) = '3'
AS
SELECT dbo.ODD_or_Even(@Digit)
October 24, 2013 at 12:00 pm
Now, there's a small problem. What happens if you ever get a value like 'a23'?
You should use a numeric datatype, and for even/odd numbers problem, the best type could be int.
To find out if a number is even or odd, the classic way to go is with a modulo operator.
WITH Tally(n) AS(
SELECT TOP 11 ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) - 1
FROM sys.all_columns)
SELECT n, CASE WHEN n % 2 = 1 THEN 'Odd' ELSE 'Even' END
FROM Tally
October 24, 2013 at 12:02 pm
Where is the Stored Procedure? Lol
October 24, 2013 at 12:03 pm
If this is homework and want to impress your instructor, you should take a look at this article.
October 24, 2013 at 12:48 pm
Honestly, the function posted is fraught with issues. There is all kinds of casting/converting both explicitly and implicitly.
If we are going to demonstrate how to do somebody's homework we should actually go to the next level and so them the right way to do this. Scalar functions are notoriously poor regarding performance. Instead of a scalar function converting this to an Inline Table Valued Function (iTVF) would be orders of magnitude better than the scalar function proposed.
Something like this.
CREATE FUNCTION ODD_or_Even
(
@Digit int
)
RETURNS table with schemabinding
AS
return
select case when @Digit % 2 = 0 then 1 else 0 end as IsEven
OK. So now we have a function but so what? We could easily create a select statement and stick that after a create procedure. That would fulfill the requirements but misses the point entirely. How about if we create a stored proc that will return the numbers from 1 to x and state if each of them is Odd or Even. This is easy now that we made our function an iTVF instead of a scalar.
create procedure GetOddOrEvenValues
(
@NumResults int
) as
WITH
E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
)
select *
from cteTally t
cross apply dbo.ODD_or_Even(t.N)
where t.N <= @NumResults
Alright. Now we have a stored proc that is utilizing a function.
All that is left is to call our newly created stored proc.
exec GetOddOrEvenValues 123 --change the 123 to any positive number and it will return that many rows.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 24, 2013 at 12:59 pm
ccavaco (10/24/2013)
Where is the Stored Procedure? Lol
There's none, it was just an example on how to do things correctly 😀
October 24, 2013 at 1:17 pm
Tell your instructor that writing a function for this is a bit crazy and is likely to result in the ponderous use of RBAR. Folks should also get away from character based results ('Even', 'Odd') for basic binary logic.
Here's an example of what I mean... simple math takes care of the problem without a function or other complexities.
SELECT IsOdd = SomeIntColumn%2
FROM dbo.SomeTable
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply