Log in  ::  Register  ::  Not logged in

 Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 user functions Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, October 23, 2013 8:07 AM
 Forum Newbie Group: General Forum Members Last Login: Monday, February 03, 2014 3:44 AM Points: 5, Visits: 18
 Write a Stored Procedure utilizing a function that receives a LocationID and determines if the LocationID is an even or an odd number producing
Post #1507634
 Posted Wednesday, October 23, 2013 8:19 AM
 SSChampion Group: General Forum Members Last Login: Today @ 2:01 PM Points: 11,608, Visits: 10,717
 josephmoka (10/23/2013)Write a Stored Procedure utilizing a function that receives a LocationID and determines if the LocationID is an even or an odd number producingThis 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 Moden's splitter.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2)
Post #1507657
 Posted Thursday, October 24, 2013 10:54 AM
 SSC Rookie Group: General Forum Members Last Login: Yesterday @ 1:13 PM Points: 32, Visits: 364
 CREATE FUNCTION ODD_or_Even (@Digit nvarchar(3))RETURNS varchar(4) AS BEGIN declare @Return varchar(30) select @return = CASE WHENCAST(@Digit as decimal)/CAST(2 AS decimal)NOT LIKE '%.0%' THEN 'Odd' ELSE 'Even' END return @return end SELECT dbo.ODD_or_Even(3)
Post #1508172
 Posted Thursday, October 24, 2013 11:05 AM
 SSCrazy Group: General Forum Members Last Login: Today @ 2:17 PM Points: 2,544, Visits: 5,407
 If you were asked to write a stored procedure, why would you create a function? Luis C.I am a great believer in luck, and I find the harder I work the more I have of it. Stephen LeacockForum Etiquette: How to post data/code on a forum to get the best help
Post #1508180
 Posted Thursday, October 24, 2013 11:49 AM
 SSC Rookie Group: General Forum Members Last Login: Yesterday @ 1:13 PM Points: 32, Visits: 364
 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 WHENCAST(@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'ASSELECT dbo.ODD_or_Even(@Digit)
Post #1508199
 Posted Thursday, October 24, 2013 12:00 PM
 SSCrazy Group: General Forum Members Last Login: Today @ 2:17 PM Points: 2,544, Visits: 5,407
 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` Luis C.I am a great believer in luck, and I find the harder I work the more I have of it. Stephen LeacockForum Etiquette: How to post data/code on a forum to get the best help
Post #1508203
 Posted Thursday, October 24, 2013 12:02 PM
 SSC Rookie Group: General Forum Members Last Login: Yesterday @ 1:13 PM Points: 32, Visits: 364
 Where is the Stored Procedure? Lol
Post #1508205
 Posted Thursday, October 24, 2013 12:03 PM
 SSCrazy Group: General Forum Members Last Login: Today @ 2:17 PM Points: 2,544, Visits: 5,407
 If this is homework and want to impress your instructor, you should take a look at this article.http://www.sqlservercentral.com/articles/T-SQL/91724/ Luis C.I am a great believer in luck, and I find the harder I work the more I have of it. Stephen LeacockForum Etiquette: How to post data/code on a forum to get the best help
Post #1508207
 Posted Thursday, October 24, 2013 12:48 PM
 SSChampion Group: General Forum Members Last Login: Today @ 2:01 PM Points: 11,608, Visits: 10,717
 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 schemabindingAS 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) asWITH 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 tcross 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 Moden's splitter.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2)
Post #1508225
 Posted Thursday, October 24, 2013 12:59 PM
 SSCrazy Group: General Forum Members Last Login: Today @ 2:17 PM Points: 2,544, Visits: 5,407
 ccavaco (10/24/2013)Where is the Stored Procedure? LolThere's none, it was just an example on how to do things correctly Luis C.I am a great believer in luck, and I find the harder I work the more I have of it. Stephen LeacockForum Etiquette: How to post data/code on a forum to get the best help
Post #1508229

 Permissions