dbTalk Databases Forums  

Optional values in a Stored Proc

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Optional values in a Stored Proc in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Bill Willyerd
 
Posts: n/a

Default Optional values in a Stored Proc - 07-30-2003 , 12:20 PM






The following SP gives an error of:
Server: Msg 245, Level 16, State 1, Procedure spSelectSEICData, Line
26
Syntax error converting the varchar value '@' to a column of data type
int.

In the Procedure I am using the Select * for testing purposes.
Here is the proc.
CREATE PROCEDURE spSelectSEICData
(
@IndivNo int,
@CommType SmallInt,
@BeginDate as SmallDateTime
)
AS
Declare @SqlStr as char(1)
Set @SqlStr = ''
If ((@BeginDate <> ' ') and (@CommType <> ' '))
Begin
Set @SqlStr = '@IndivNo AND [SEIC-COMMENT-TYPE] = @CommType'
End
If ((@BeginDate <> ' ') and (@CommType = ' '))
Begin
Set @SqlStr = '@IndivNo AND [SEIC-COMMENT-DATE-MCYMD] =
@BeginDate'
End
If ((@BeginDate = ' ') and (@CommType <> ' '))
Begin
Set @SqlStr = '@IndivNo AND [SEIC-COMMENT-DATE-MCYMD] = @BeginDate
AND[SEIC-COMMENT-TYPE] = @CommType '
End
If ((@BeginDate = ' ') and (@CommType = ' '))
Begin
Set @SQlStr = '@IndivNo '
End

SELECT *
FROM SEIC
WHERE [SEIC-INDIVIDUAL-NO] = @SqlStr

GO

The optional values are the @CommType and the @BeginDate. Where did I
go wrong or is there a better way of doing this?
Thanks in advance
Bill

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Optional values in a Stored Proc - 07-30-2003 , 02:41 PM






Bill Willyerd (bwillyerd (AT) dshs (DOT) wa.gov) writes:
Quote:
The following SP gives an error of:
Server: Msg 245, Level 16, State 1, Procedure spSelectSEICData, Line
26
Syntax error converting the varchar value '@' to a column of data type
int.

In the Procedure I am using the Select * for testing purposes.
Here is the proc.
CREATE PROCEDURE spSelectSEICData
(
@IndivNo int,
@CommType SmallInt,
@BeginDate as SmallDateTime
)
AS
Declare @SqlStr as char(1)
To be char(1), you are cramming a lot of characters into it.

Quote:
SELECT *
FROM SEIC
WHERE [SEIC-INDIVIDUAL-NO] = @SqlStr
So at this point @SqlStr has the value '@', and apparently the column
you are comparing it to is an integer column, whence the error.

Judging from your code, you seem to be building parts of an SQL
statement, but while it may be obvious to you, SQL Server is
completely without chance to understand what's going on.

See here for tips on how to implemnt these kind of searches. Since
this procedure is fairly simple with only three different conditions,
I would probably go for a static solution.


--
Erland Sommarskog, SQL Server MVP, sommar (AT) algonet (DOT) se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp


Reply With Quote
  #3  
Old   
Bill Willyerd
 
Posts: n/a

Default Re: Optional values in a Stored Proc - 07-30-2003 , 03:23 PM



Thanks for pointing out that char(1). I had tried a CASE using single
characters for each possibility then switched to putting the SQL string
into the var (with out changing the datatype or size).
I did get it to work using If Else If conditional statments.

Thanks again
Bill



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.