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.
8 1414
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 :)
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. - CREATE PROCEDURE personAddress
-
@Person_Id int = NULL,
-
@Address_Type varchar(35) = NULL
-
AS
-
DECLARE @Employee_Id_Count as int
-
DECLARE @Address_Type_Count as int
-
-
SELECT @Employee_Id_Count = count(*)
-
FROM PERSON P
-
WHERE P.Person_Id = @Person_Id
-
-
IF @Employee_Id_Count = 0
-
BEGIN
-
PRINT 'Error Msg 529141'
-
RAISERROR(529141, 10, 1, @Person_Id)
-
RETURN
-
END
-
SET NOCOUNT ON;
-
-
SELECT @Address_Type_Count = count (*)
-
FROM ADDRESS_TYPE AT
-
WHERE Address_Type_Code = @Address_Type
-
-
IF @Address_Type != NULL
-
IF @Address_Type_Count = 0
-
BEGIN
-
PRINT 'Error Msg 529142'
-
RAISERROR(529142,10, 1, @Address_Type)
-
RETURN
-
END
-
-
SELECT PA.[Employee Name] AS Name,
-
PA.[Street Address] AS [Street Address],
-
AT.Address_Type_Code AS [Type of Address Description]
-
FROM Personnel_Address PA JOIN ADDRESS_TYPE AT
-
ON PA.[Employee ID] = @Person_Id AND PA.[Address Type] = @Address_Type
-
-
IF @Address_Type = NULL
-
SELECT PA.[Employee Name] AS Name,
-
PA.[Street Address] AS [Street Address],
-
PA.[Address Type] AS [Type of Address Description]
-
FROM Personnel_Address PA
-
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
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
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. - CREATE PROCEDURE personAddress
-
@Person_Id int = NULL,
-
@Address_Type varchar(35) = NULL
-
AS
-
DECLARE @Employee_Id_Count as int
-
DECLARE @Address_Type_Count as int
-
-
SELECT @Employee_Id_Count = count(*)
-
FROM PERSON P
-
WHERE P.Person_Id = @Person_Id
-
-
IF @Employee_Id_Count = 0
-
BEGIN
-
PRINT 'Error Msg 529141'
-
RAISERROR(529141, 10, 1, @Person_Id)
-
RETURN
-
END
-
SET NOCOUNT ON;
-
-
SELECT @Address_Type_Count = count (*)
-
FROM ADDRESS_TYPE AT
-
WHERE Address_Type_Code = @Address_Type
-
-
IF @Address_Type != NULL
-
IF @Address_Type_Count = 0
-
BEGIN
-
PRINT 'Error Msg 529142'
-
RAISERROR(529142,10, 1, @Address_Type)
-
RETURN
-
END
-
-
SELECT PA.[Employee Name] AS Name,
-
PA.[Street Address] AS [Street Address],
-
AT.Address_Type_Code AS [Type of Address Description]
-
FROM Personnel_Address PA JOIN ADDRESS_TYPE AT
-
ON PA.[Employee ID] = @Person_Id AND PA.[Address Type] = @Address_Type
-
-
IF @Address_Type = NULL
-
SELECT PA.[Employee Name] AS Name,
-
PA.[Street Address] AS [Street Address],
-
PA.[Address Type] AS [Type of Address Description]
-
FROM Personnel_Address PA
-
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
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? - SELECT PA.[Employee Name] AS Name,
-
PA.[Street Address] AS [Street Address],
-
AT.Address_Type_Code AS [Type of Address Description]
-
FROM Personnel_Address PA JOIN ADDRESS_TYPE AT
-
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 :)
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. - CREATE VIEW Personnel_Addresses ([Employee ID], [Employee Name], [Address Type],
-
[Street Address], [City], [State], [Zip Code]) AS
-
SELECT
-
P.Person_Id, case when Middle_Name_Initial is null then
-
(Last_Name + ', ' + First_Name) else (Last_Name + ', ' + First_Name + ' '
-
+ Middle_Name_Initial + '.') end, Address_Type_Description,
-
case when A.Address_line_2 is null then A.Address_line_1
-
else (A.Address_line_1 + ', ' + A.Address_line_2) end,
-
Z.Zip_City, Z.Zip_State, Z.Zip_Code
-
FROM PERSON P JOIN PERSON_ADDRESS PA
-
ON (P.Person_Id = PA.Person_Id) JOIN ADDRESS_TYPE AT
-
ON (AT.Address_Type_Code = PA.Address_Type_Code) JOIN ADDRESS A
-
ON (A.Address_Id = PA.Address_Id) JOIN ZIP Z
-
ON (A.Zip_Code = Z.Zip_Code)
The following are the tables I made that are used to create this view:
Person table - CREATE TABLE PERSON (
-
Person_Id int NOT NULL,
-
First_Name varchar(20) NOT NULL,
-
Last_Name varchar(20) NOT NULL,
-
Middle_Name_Initial char(1) NULL,
-
Date_Of_Birth DateTime NOT NULL,
-
Employment_Status char(1) NOT NULL,
-
CONSTRAINT ck_employmentStats
-
CHECK(Employment_Status IN ('F', 'P')),
-
CONSTRAINT pk_person
-
PRIMARY KEY (Person_Id),
-
);
Person_Address - CREATE TABLE PERSON_ADDRESS (
-
Person_Id int NOT NULL,
-
Address_Id int NOT NULL,
-
Address_Type_Code int NOT NULL,
-
CONSTRAINT pk_person_address
-
PRIMARY KEY (Person_Id, Address_Id),
-
);
Address_Type - CREATE TABLE ADDRESS_TYPE (
-
Address_Type_Code int NOT NULL,
-
Address_Type_Description varchar(35) NOT NULL,
-
CONSTRAINT pk_address_type_code
-
PRIMARY KEY (Address_Type_Code),
-
);
Address - CREATE TABLE ADDRESS (
-
Address_Id int NOT NULL IDENTITY(1,1),
-
Address_line_1 varchar(25) NOT NULL,
-
Address_line_2 varchar(25) NULL,
-
Zip_Code char(9) NOT NULL,
-
CONSTRAINT pk_addId
-
PRIMARY KEY (Address_Id),
-
);
Zip - CREATE TABLE ZIP (
-
Zip_City varchar(25) NOT NULL,
-
Zip_State char(2) NOT NULL,
-
Zip_Code char(9) NOT NULL,
-
CONSTRAINT pk_zipcode
-
PRIMARY KEY (Zip_Code),
-
);
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?
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. - CREATE VIEW Personnel_Addresses ([Employee ID], [Employee Name], [Address Type],
-
[Street Address], [City], [State], [Zip Code]) AS
-
SELECT
-
P.Person_Id, case when Middle_Name_Initial is null then
-
(Last_Name + ', ' + First_Name) else (Last_Name + ', ' + First_Name + ' '
-
+ Middle_Name_Initial + '.') end, Address_Type_Description,
-
case when A.Address_line_2 is null then A.Address_line_1
-
else (A.Address_line_1 + ', ' + A.Address_line_2) end,
-
Z.Zip_City, Z.Zip_State, Z.Zip_Code
-
FROM PERSON P JOIN PERSON_ADDRESS PA
-
ON (P.Person_Id = PA.Person_Id) JOIN ADDRESS_TYPE AT
-
ON (AT.Address_Type_Code = PA.Address_Type_Code) JOIN ADDRESS A
-
ON (A.Address_Id = PA.Address_Id) JOIN ZIP Z
-
ON (A.Zip_Code = Z.Zip_Code)
The following are the tables I made that are used to create this view:
Person table - CREATE TABLE PERSON (
-
Person_Id int NOT NULL,
-
First_Name varchar(20) NOT NULL,
-
Last_Name varchar(20) NOT NULL,
-
Middle_Name_Initial char(1) NULL,
-
Date_Of_Birth DateTime NOT NULL,
-
Employment_Status char(1) NOT NULL,
-
CONSTRAINT ck_employmentStats
-
CHECK(Employment_Status IN ('F', 'P')),
-
CONSTRAINT pk_person
-
PRIMARY KEY (Person_Id),
-
);
Person_Address - CREATE TABLE PERSON_ADDRESS (
-
Person_Id int NOT NULL,
-
Address_Id int NOT NULL,
-
Address_Type_Code int NOT NULL,
-
CONSTRAINT pk_person_address
-
PRIMARY KEY (Person_Id, Address_Id),
-
);
Address_Type - CREATE TABLE ADDRESS_TYPE (
-
Address_Type_Code int NOT NULL,
-
Address_Type_Description varchar(35) NOT NULL,
-
CONSTRAINT pk_address_type_code
-
PRIMARY KEY (Address_Type_Code),
-
);
Address - CREATE TABLE ADDRESS (
-
Address_Id int NOT NULL IDENTITY(1,1),
-
Address_line_1 varchar(25) NOT NULL,
-
Address_line_2 varchar(25) NULL,
-
Zip_Code char(9) NOT NULL,
-
CONSTRAINT pk_addId
-
PRIMARY KEY (Address_Id),
-
);
Zip - CREATE TABLE ZIP (
-
Zip_City varchar(25) NOT NULL,
-
Zip_State char(2) NOT NULL,
-
Zip_Code char(9) NOT NULL,
-
CONSTRAINT pk_zipcode
-
PRIMARY KEY (Zip_Code),
-
);
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? - SELECT @Address_Type_Count = count (*)
-
FROM ADDRESS_TYPE AT
-
WHERE Address_Type_Code = @Address_Type
-
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 - FROM Personnel_Address PA JOIN ADDRESS_TYPE AT
it should have 'es' on the end of Personnel_Address - FROM Personnel_Addresses PA JOIN ADDRESS_TYPE AT
Regards
Jim :)
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!
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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
|
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:...
| |
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...
|
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...
|
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...
|
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 =...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |