473,473 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Stored Proceedure

34 New Member
Is there a way to create a stored procedure where more than one type of parameter can be entered? For example, a user can enter a int type, char(4) type, or nothing in the same stored proceedure.
Nov 9 '07 #1
8 1414
Jim Doherty
897 Recognized Expert Contributor
Is there a way to create a stored procedure where more than one type of parameter can be entered? For example, a user can enter a int type, char(4) type, or nothing in the same stored proceedure.
Stored procedures are designed to take multiple parameters... or am I missing something

Jim :)
Nov 9 '07 #2
jthep
34 New Member
Sorry, erase the first question and inserting a new one =D. I have a stored procedure below that displays data from both views and tables.

Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE personAddress
  2.     @Person_Id int = NULL,
  3.     @Address_Type varchar(35) = NULL
  4. AS
  5.     DECLARE @Employee_Id_Count as int
  6.     DECLARE @Address_Type_Count as int
  7.  
  8.     SELECT @Employee_Id_Count = count(*)
  9.     FROM PERSON P
  10.     WHERE P.Person_Id = @Person_Id
  11.  
  12.     IF @Employee_Id_Count = 0
  13.     BEGIN
  14.         PRINT 'Error Msg 529141'
  15.         RAISERROR(529141, 10, 1, @Person_Id)
  16.         RETURN
  17.     END
  18.     SET NOCOUNT ON;
  19.  
  20.     SELECT @Address_Type_Count = count (*)
  21.     FROM ADDRESS_TYPE AT
  22.     WHERE Address_Type_Code = @Address_Type
  23.  
  24.     IF @Address_Type != NULL
  25.         IF @Address_Type_Count = 0
  26.         BEGIN 
  27.             PRINT 'Error Msg 529142'
  28.             RAISERROR(529142,10, 1, @Address_Type)
  29.             RETURN
  30.         END
  31.  
  32.         SELECT PA.[Employee Name] AS Name,
  33.             PA.[Street Address] AS [Street Address],
  34.             AT.Address_Type_Code AS [Type of Address Description]
  35.         FROM Personnel_Address PA JOIN ADDRESS_TYPE AT
  36.         ON PA.[Employee ID] = @Person_Id AND PA.[Address Type] = @Address_Type
  37.  
  38.     IF @Address_Type = NULL
  39.         SELECT PA.[Employee Name] AS Name,
  40.             PA.[Street Address] AS [Street Address],
  41.             PA.[Address Type] AS [Type of Address Description]
  42.         FROM Personnel_Address PA
  43.         WHERE PA.[Employee ID] = @Person_Id
I don't know if I wrote it correctly but the user could either enter Person_Id, or (Person_Id, Address_Description)

If the user entered just the Person_Id param then the procedure checks if it is valid, and if not, it prints out an error. If the Id is valid, it displays all addresses the person of that Id has (it could be office, home, primary, etc).

If the user entered two params, it checks the second parameter. If it is invalid, an error message is printed otherwise it will display the address of the description entered with the id person's info.

When I try to execute the store procedure using

Expand|Select|Wrap|Line Numbers
  1. personAddress 4, Office;
There is an error message:
Msg 245, Level 16, State 1, Procedure personAddress, Line 27
Conversion failed when converting the varchar value 'Office' to data type int.

Can someone tell me why it is giving me that error msg?

Regards
Jthep
Nov 9 '07 #3
Jim Doherty
897 Recognized Expert Contributor
Sorry, erase the first question and inserting a new one =D. I have a stored procedure below that displays data from both views and tables.

Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE personAddress
  2.     @Person_Id int = NULL,
  3.     @Address_Type varchar(35) = NULL
  4. AS
  5.     DECLARE @Employee_Id_Count as int
  6.     DECLARE @Address_Type_Count as int
  7.  
  8.     SELECT @Employee_Id_Count = count(*)
  9.     FROM PERSON P
  10.     WHERE P.Person_Id = @Person_Id
  11.  
  12.     IF @Employee_Id_Count = 0
  13.     BEGIN
  14.         PRINT 'Error Msg 529141'
  15.         RAISERROR(529141, 10, 1, @Person_Id)
  16.         RETURN
  17.     END
  18.     SET NOCOUNT ON;
  19.  
  20.     SELECT @Address_Type_Count = count (*)
  21.     FROM ADDRESS_TYPE AT
  22.     WHERE Address_Type_Code = @Address_Type
  23.  
  24.     IF @Address_Type != NULL
  25.         IF @Address_Type_Count = 0
  26.         BEGIN 
  27.             PRINT 'Error Msg 529142'
  28.             RAISERROR(529142,10, 1, @Address_Type)
  29.             RETURN
  30.         END
  31.  
  32.         SELECT PA.[Employee Name] AS Name,
  33.             PA.[Street Address] AS [Street Address],
  34.             AT.Address_Type_Code AS [Type of Address Description]
  35.         FROM Personnel_Address PA JOIN ADDRESS_TYPE AT
  36.         ON PA.[Employee ID] = @Person_Id AND PA.[Address Type] = @Address_Type
  37.  
  38.     IF @Address_Type = NULL
  39.         SELECT PA.[Employee Name] AS Name,
  40.             PA.[Street Address] AS [Street Address],
  41.             PA.[Address Type] AS [Type of Address Description]
  42.         FROM Personnel_Address PA
  43.         WHERE PA.[Employee ID] = @Person_Id
I don't know if I wrote it correctly but the user could either enter Person_Id, or (Person_Id, Address_Description)

If the user entered just the Person_Id param then the procedure checks if it is valid, and if not, it prints out an error. If the Id is valid, it displays all addresses the person of that Id has (it could be office, home, primary, etc).

If the user entered two params, it checks the second parameter. If it is invalid, an error message is printed otherwise it will display the address of the description entered with the id person's info.

When I try to execute the store procedure using

Expand|Select|Wrap|Line Numbers
  1. personAddress 4, Office;
There is an error message:
Msg 245, Level 16, State 1, Procedure personAddress, Line 27
Conversion failed when converting the varchar value 'Office' to data type int.

Can someone tell me why it is giving me that error msg?

Regards
Jthep
What is this datatype highlighted in bold in the source Personnel_Address? is it integer or string?

Expand|Select|Wrap|Line Numbers
  1. SELECT PA.[Employee Name] AS Name,
  2.             PA.[Street Address] AS [Street Address],
  3.             AT.Address_Type_Code AS [Type of Address Description]
  4.         FROM Personnel_Address PA JOIN ADDRESS_TYPE AT
  5.         ON PA.[Employee ID] = @Person_Id AND PA.[Address Type] = @Address_Type
In the absence of table structures and datatypes one can only guess but basically it can't convert a string to integer if thats what your column contains and is comparing
Jim :)
Nov 9 '07 #4
jthep
34 New Member
Personal_Address is a view. Employee_Id field in the view is a person that can either be a full time or part time employee. There are tables for each type of employee.

Expand|Select|Wrap|Line Numbers
  1. CREATE VIEW Personnel_Addresses ([Employee ID], [Employee Name], [Address Type],
  2.     [Street Address], [City], [State], [Zip Code]) AS
  3. SELECT 
  4.     P.Person_Id, case when Middle_Name_Initial is null then
  5.     (Last_Name + ', ' + First_Name) else (Last_Name + ', ' + First_Name + '  ' 
  6.     + Middle_Name_Initial + '.') end, Address_Type_Description,
  7.     case when A.Address_line_2 is null then A.Address_line_1
  8.     else (A.Address_line_1 + ', ' + A.Address_line_2) end, 
  9.     Z.Zip_City, Z.Zip_State, Z.Zip_Code
  10. FROM PERSON P JOIN PERSON_ADDRESS PA
  11. ON (P.Person_Id = PA.Person_Id) JOIN ADDRESS_TYPE AT 
  12.     ON (AT.Address_Type_Code = PA.Address_Type_Code) JOIN ADDRESS A 
  13.     ON (A.Address_Id = PA.Address_Id) JOIN ZIP Z 
  14.     ON (A.Zip_Code = Z.Zip_Code)
The following are the tables I made that are used to create this view:

Person table
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE PERSON (
  2.     Person_Id int NOT NULL,
  3.     First_Name varchar(20) NOT NULL,
  4.     Last_Name varchar(20) NOT NULL,
  5.     Middle_Name_Initial char(1) NULL,
  6.     Date_Of_Birth DateTime NOT NULL,
  7.     Employment_Status char(1) NOT NULL,
  8.     CONSTRAINT ck_employmentStats
  9.         CHECK(Employment_Status IN ('F', 'P')),
  10.     CONSTRAINT pk_person 
  11.         PRIMARY KEY (Person_Id),
  12. );
Person_Address
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE PERSON_ADDRESS (
  2.     Person_Id int NOT NULL,
  3.     Address_Id int NOT NULL,
  4.     Address_Type_Code int NOT NULL,
  5.     CONSTRAINT pk_person_address
  6.         PRIMARY KEY (Person_Id, Address_Id),
  7. );
Address_Type
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE ADDRESS_TYPE (
  2.     Address_Type_Code int NOT NULL,
  3.     Address_Type_Description varchar(35) NOT NULL,
  4.     CONSTRAINT pk_address_type_code
  5.         PRIMARY KEY (Address_Type_Code),
  6. );
Address
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE ADDRESS (
  2.     Address_Id int NOT NULL IDENTITY(1,1),
  3.     Address_line_1 varchar(25) NOT NULL,
  4.     Address_line_2 varchar(25) NULL,
  5.     Zip_Code char(9) NOT NULL,
  6.     CONSTRAINT pk_addId
  7.         PRIMARY KEY (Address_Id),
  8. );
Zip
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE ZIP (
  2.     Zip_City varchar(25) NOT NULL,
  3.     Zip_State char(2) NOT NULL,
  4.     Zip_Code char(9) NOT NULL,
  5.     CONSTRAINT pk_zipcode
  6.         PRIMARY KEY (Zip_Code),
  7. );
Since the personnel_addresses view displays the Address_Type_Description from the ADDRESS_TYPE table (defined as a varchar(25)) as the Address Type field in the view, shouldnt it work?
Nov 9 '07 #5
Jim Doherty
897 Recognized Expert Contributor
Personal_Address is a view. Employee_Id field in the view is a person that can either be a full time or part time employee. There are tables for each type of employee.

Expand|Select|Wrap|Line Numbers
  1. CREATE VIEW Personnel_Addresses ([Employee ID], [Employee Name], [Address Type],
  2.     [Street Address], [City], [State], [Zip Code]) AS
  3. SELECT 
  4.     P.Person_Id, case when Middle_Name_Initial is null then
  5.     (Last_Name + ', ' + First_Name) else (Last_Name + ', ' + First_Name + ' ' 
  6.     + Middle_Name_Initial + '.') end, Address_Type_Description,
  7.     case when A.Address_line_2 is null then A.Address_line_1
  8.     else (A.Address_line_1 + ', ' + A.Address_line_2) end, 
  9.     Z.Zip_City, Z.Zip_State, Z.Zip_Code
  10. FROM PERSON P JOIN PERSON_ADDRESS PA
  11. ON (P.Person_Id = PA.Person_Id) JOIN ADDRESS_TYPE AT 
  12.     ON (AT.Address_Type_Code = PA.Address_Type_Code) JOIN ADDRESS A 
  13.     ON (A.Address_Id = PA.Address_Id) JOIN ZIP Z 
  14.     ON (A.Zip_Code = Z.Zip_Code)
The following are the tables I made that are used to create this view:

Person table
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE PERSON (
  2.     Person_Id int NOT NULL,
  3.     First_Name varchar(20) NOT NULL,
  4.     Last_Name varchar(20) NOT NULL,
  5.     Middle_Name_Initial char(1) NULL,
  6.     Date_Of_Birth DateTime NOT NULL,
  7.     Employment_Status char(1) NOT NULL,
  8.     CONSTRAINT ck_employmentStats
  9.         CHECK(Employment_Status IN ('F', 'P')),
  10.     CONSTRAINT pk_person 
  11.         PRIMARY KEY (Person_Id),
  12. );
Person_Address
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE PERSON_ADDRESS (
  2.     Person_Id int NOT NULL,
  3.     Address_Id int NOT NULL,
  4.     Address_Type_Code int NOT NULL,
  5.     CONSTRAINT pk_person_address
  6.         PRIMARY KEY (Person_Id, Address_Id),
  7. );
Address_Type
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE ADDRESS_TYPE (
  2.     Address_Type_Code int NOT NULL,
  3.     Address_Type_Description varchar(35) NOT NULL,
  4.     CONSTRAINT pk_address_type_code
  5.         PRIMARY KEY (Address_Type_Code),
  6. );
Address
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE ADDRESS (
  2.     Address_Id int NOT NULL IDENTITY(1,1),
  3.     Address_line_1 varchar(25) NOT NULL,
  4.     Address_line_2 varchar(25) NULL,
  5.     Zip_Code char(9) NOT NULL,
  6.     CONSTRAINT pk_addId
  7.         PRIMARY KEY (Address_Id),
  8. );
Zip
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE ZIP (
  2.     Zip_City varchar(25) NOT NULL,
  3.     Zip_State char(2) NOT NULL,
  4.     Zip_Code char(9) NOT NULL,
  5.     CONSTRAINT pk_zipcode
  6.         PRIMARY KEY (Zip_Code),
  7. );
Since the personnel_addresses view displays the Address_Type_Description from the ADDRESS_TYPE table (defined as a varchar(25)) as the Address Type field in the view, shouldnt it work?


Its easy now you have posted your tables thanks.

Look at your procedure for returning the count for address type...you are comparing int against string can you see?

Expand|Select|Wrap|Line Numbers
  1.  SELECT @Address_Type_Count = count (*) 
  2. FROM ADDRESS_TYPE AT
  3. WHERE Address_Type_Code = @Address_Type
  4.  
Address type code is an integer in your table and you mistakenly comparing @Address_Type defined as an varchar(35) parameter against it. It cannot convert that. That is where your error lies

In addition you also have an error that you might wish to fix at this line

Expand|Select|Wrap|Line Numbers
  1. FROM Personnel_Address PA JOIN ADDRESS_TYPE AT
it should have 'es' on the end of Personnel_Address

Expand|Select|Wrap|Line Numbers
  1. FROM Personnel_Addresses PA JOIN ADDRESS_TYPE AT


Regards

Jim :)
Nov 9 '07 #6
jthep
34 New Member
OOHHHH that's why. I think my problem is due to the naming convention. I have attributes in one table that has the either the same name or something that's very close to another table that I end up getting all confused or mix and match them up sometimes. =D

Thanks Jim!
Nov 16 '07 #7
Jim Doherty
897 Recognized Expert Contributor
OOHHHH that's why. I think my problem is due to the naming convention. I have attributes in one table that has the either the same name or something that's very close to another table that I end up getting all confused or mix and match them up sometimes. =D

Thanks Jim!
You're very welcome..just place the box of sweets on the shared office desk on the way out :))

Jim
Nov 16 '07 #8
iburyak
1,017 Recognized Expert Top Contributor
I am nto an Excel specialist but check this out:

http://office.microsoft.com/en-us/excel/HP051995481033.aspx

Good Luck.
Nov 16 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tavish Muldoon | last post by:
What a pain trying to insert data into a table from a stored proc. My webform asks for 16 pieces of data - which then gets written to the database. I found this easier than the crap below...
1
by: John | last post by:
Hope you can give me some advise. I am wanting to build a databse driven website. I am using Access to connect to an SQL 2000 server to create tables etc. I am using ASP/ASP.Net to build my...
6
by: John Webb | last post by:
Hope someone can help. I am trying to write a stored proceedure to display sales activity by month and then sum all the columbs. The problem is that our sales year starts in April and end in...
1
by: Melissa Meyer via SQLMonster.com | last post by:
How do you set a stored proceedure for automatic execution? -- Message posted via http://www.sqlmonster.com
0
by: Varun Gupta | last post by:
Hi All, can anybody let me know the steps how to call the stored proceedure that is developed in db2 or sql, into unix shell script. Point: Stored proc. may or may not have parameters. Point:...
2
by: Problematic coder | last post by:
I have a form based application that inputs values into various oracle tables, after it has done this I want to call a stored proceedure in Oracle which is already written to do more work on this...
9
by: Problematic coder | last post by:
The intention is to call a stored proceedure which sets flags in the database, this requires no parameters and the page does not need an output from the stored proceedure, though is this is...
0
by: ig.martix | last post by:
I'm a bit new to asp. What I'm looking to doing is having three input boxes on a page. As an example I would like for the user to input a employee ID in the first input box and then wither...
4
by: Mick Walker | last post by:
Hi Everyone, I am stumped here. I have the following stored proceedure:P CREATE PROCEDURE . @SupplierSKU varchar(50), @RetVal int AS Select @Retval = count(*) from dbo.ImportLines Where =...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.