473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Optional values in a Stored Proc

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
Jul 20 '05 #1
2 2759
Bill Willyerd (bw*******@dshs.wa.gov) writes:
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.
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, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
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!
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Peter D.C. | last post by:
Hi I want to update data hold in several textbox controls on an asp.net form. But it seems like it is the old textbox values that is "re-updates" through a stored procedure who updates a SQL...
1
by: Mok | last post by:
Hello, I want to pull information from a table in sql server, bind it to a grid and update those values. I would like to know the best way to do it. I used the configure data adapter wizard and it...
7
by: CK | last post by:
I want the procedure to check for the existence of a paramter and if it is there, it will process these instructions, otherwise it will process these instructions. Any ideas? Thanks for your...
0
by: RickBen | last post by:
I have written a stored proc to write order details to the db. If there is additional description text, I am writting it out to another note table. i initially performed this using EXEC with...
14
by: krishna1412 | last post by:
Currently i am working in a project of report generation in MS ACCESS. The tables are in sql server 2000. I have to write stored proc in ms access. Illustration: I am having a stored proc...
1
by: brutusram | last post by:
I am trying to execute SP that has another SP in it that is being passed multiple values. I want to stay away from cursors but I cannot find another way to do this in 2005. BEGIN 1st STORED PROC...
0
by: mirandacascade | last post by:
Questions toward the bottom of the post. Situation is this: 1) Access 97 2) SQL Server 2000 3) The Access app: a) sets up pass-thru query b) .SQL property of querydef is a string, the...
1
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.