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.
| Author |
Topic |
|
Quan69
Starting Member
5 Posts |
Posted - 2011-08-08 : 19:53:58
|
| I have a stored procedure that results in an error:Converting adult_member_no from varchar to smallint.It is odd b/c I have adult_member_no declared as smallint...Here is my stored proc:-- Create new proc for adding adult memberALTER PROCEDURE [dbo].[AddJuvenile] ( @member_no smallint OUTPUT, @firstname VARCHAR(15) = NULL, @middleinitial CHAR(1) = NULL, @lastname VARCHAR(15) = NULL, @adult_member_no smallint, @birth_date datetime = NULL )AS-- DECLARE/SET datetime variables-- Assumption: That Juvenile is from 5 years to day before 18th birthday-- @min_age and @max_age are used to verify juvenile's ageDECLARE @expr_date datetime = DATEADD(yy, 1, GetDate());DECLARE @min_age datetime = DATEADD(dd, -1825, GetDate()); -- 5 yearsDECLARE @max_age datetime = DATEADD(dd, -6569, GetDate()); -- 17 years, 11 months, 29 days-- Test for NULLS and unwanted valuesIF @firstname IS NULL BEGIN RAISERROR('First Name Can NOT be empty', 10, 1); RETURN -10 ENDIF @lastname IS NULL BEGIN RAISERROR('Last Name Can NOT be empty', 10, 1); RETURN -10 ENDIF @adult_member_no IS NULL OR @adult_member_no < 0 OR @adult_member_no > 32767 BEGIN RAISERROR('Adult Member ID must be positive of type short', 10, 1); RETURN -10 ENDIF @birth_date IS NULL OR @birth_date > @min_age OR @birth_date < @max_age BEGIN RAISERROR('City can NOT be empty', 10, 1); RETURN -10; ENDBEGIN BEGIN TRANSACTION SET NOCOUNT ON -- TODO: Check if Adult Member ID exists in adult/member tables IF EXISTS(SELECT member_no FROM [library].[dbo].[adult] WHERE member_no='adult_member_no') -- First, insert into member table to get memberID INSERT INTO member (firstname, middleinitial, lastname) VALUES (@firstname, @middleinitial, @lastname); -- Commit Rollback if an error IF @@error <> 0 BEGIN ROLLBACK TRANSACTION RETURN -@@error; END SET @member_no = scope_identity(); IF @member_no IS NULL OR @member_no < 0 OR @member_no > 32767 BEGIN RAISERROR('Member ID must be positive of type short', 10, 1); RETURN -10 END INSERT INTO juvenile (member_no, adult_member_no, birth_date) VALUES (@member_no, @adult_member_no, @birth_date); -- Commit Rollback if an error IF @@error <> 0 BEGIN ROLLBACK TRANSACTION RETURN -@@error; END ELSE BEGIN RAISERROR('Adult Member ID does NOT exist in library DB!', 10, 1); RETURN -10 END COMMIT TRANSACTION RETURN ENDThanks for helpMe |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Quan69
Starting Member
5 Posts |
Posted - 2011-08-08 : 23:18:50
|
| Ok, stupid mistake!But I still get that error...Me |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2011-08-09 : 00:44:29
|
What value is getting passed in? If you're passing a varchar that can't explicitly be converted, then there's your error.Also, the block of code starting withquote: IF @member_no IS NULL OR @member_no < 0 OR @member_no > 32767
is totally unnecessary. It isn't possible for any of these conditions to be true in this SP.AND...why in the world would you allow optional parameters only to raise a custom error if they aren't supplied? should remove the @param = null, and all of that custom error handling for missing params.Wow...remove those transactions too. I'm starting to sound like Celko, but this thing could be just a few lines, yet you've managed to make it many and hurt performance at the same time.But, try this outALTER PROCEDURE [dbo].[AddJuvenile] @member_no smallint OUTPUT, @firstname VARCHAR(15), @middleinitial CHAR(1), @lastname VARCHAR(15), @adult_member_no smallint, @birth_date datetimeASSET NOCOUNT ON-- DECLARE/SET datetime variables-- Assumption: That Juvenile is from 5 years to day before 18th birthday-- @min_age and @max_age are used to verify juvenile's ageDECLARE @expr_date datetime = DATEADD(yy, 1, GetDate());DECLARE @min_age datetime = DATEADD(dd, -1825, GetDate()); -- 5 yearsDECLARE @max_age datetime = DATEADD(dd, -6569, GetDate()); -- 17 years, 11 months, 29 daysIF @birth_date > @min_age OR @birth_date < @max_ageBEGINRAISERROR('I dont think this message should be city related', 10, 1);RETURN -10;END-- TODO: Check if Adult Member ID exists in adult/member tables IF EXISTS(SELECT member_no FROM [library].[dbo].[adult] WHERE member_no = adult_member_no)-- First, insert into member table to get memberIDINSERT INTO member (firstname, middleinitial, lastname)VALUES (@firstname, @middleinitial, @lastname);SET @member_no = scope_identity(); INSERT INTO juvenile (member_no, adult_member_no, birth_date)VALUES (@member_no, @adult_member_no, @birth_date);GO |
 |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2011-08-09 : 19:08:06
|
| If I am reading the error correctly it is complaining about 'adult_member_no'; NOT '@adult_member_no'. Is there a column in your table with that name? Is it a varchar? Would any of the values fail to convert to smallint?=======================================I have never met a man so ignorant that I couldn't learn something from him. -Galileo Galilei, physicist and astronomer (1564-1642) |
 |
|
|
Quan69
Starting Member
5 Posts |
Posted - 2011-08-11 : 00:32:58
|
| RESOLVED -- Thanks everyoneIf statements with more than a single line of code must be contained in BEGIN-END blockMe |
 |
|
|
|
|
|
|
|