Groups | Blog | Home
all groups > sql server (alternate) > july 2003 >

sql server (alternate) : Optional values in a Stored Proc


bwillyerd NO[at]SPAM dshs.wa.gov
7/30/2003 10:20:51 AM
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
Erland Sommarskog
7/30/2003 7:41:11 PM
Bill Willyerd (bwillyerd@dshs.wa.gov) writes:
[quoted text, click to view]

To be char(1), you are cramming a lot of characters into it.

[quoted text, click to view]

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@algonet.se

Books Online for SQL Server SP3 at
Bill Willyerd
7/30/2003 8:23:15 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 ***
AddThis Social Bookmark Button