473,387 Members | 1,619 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.

Stored Procedure Syntax Help

I'm enhancing a large ASP application by replacing raw SQL statements
in the code with calls to stored procedures. I've seen this
recommended in many places to increase SQL Server effieicency.
Replacing select statements is going fine but I hit a sticking point
when trying to replace an update statement.

Currently, I use this kind of statement a lot in my ASP:

sql = "update"
sql = sql & " field1 = value1"
sql = sql & ",field2 = value2"
If Len(value3) Then sql = sql & ",field3 = value3"
sql = sql & " where ...., etc, etc

The important part here is checking if "value3" has a value before
inserting it into my SQL statement. So how can I put this update
statement into a stored procedure, pass in values for value1, value2,
and value3, and leave off the value3 part of the update if value3
comes in as null.

Thanks all.
Jul 20 '05 #1
3 14340
If conditional WHERE clause predicates is what you are looking for, you can
refer to Erland's article on the topic :
http://www.algonet.se/~sommar/dyn-search.html

--
--- Anith
Jul 20 '05 #2
Hi. Try with this example:

-- creating procedure...

drop procedure XYZ
go

create procedure XYZ
@value1 varchar(8000),
@value2 varchar(8000),
@value3 varchar(8000) = null,
@sql varchar(8000) output
as
set @sql = 'update'
set @sql = @sql + ' field1 = ''' + @value1 + ''''
set @sql = @sql + ',field2 = ''' + @value2 + ''''
If Len(@value3) > 0 set @sql = @sql + ',field3 = ''' + @value3 + ''''
set @sql = @sql + ' where ....'
go

-- testing procedure...

declare @arg1 varchar(8000)
declare @arg2 varchar(8000)
declare @arg3 varchar(8000)
declare @ret varchar(8000)

set @arg1 = 'v1'
set @arg2 = 'v2'
set @arg3 = 'v3'

execute XYZ @value1=@arg1, @value2=@arg2, @sql=@ret output
print @ret
execute XYZ @value1=@arg1, @value2=@arg2, @value3=@arg3, @sql=@ret output
print @ret


rk****@kelsointeractive.com (Robert) wrote in message news:<4b**************************@posting.google. com>...
I'm enhancing a large ASP application by replacing raw SQL statements
in the code with calls to stored procedures. I've seen this
recommended in many places to increase SQL Server effieicency.
Replacing select statements is going fine but I hit a sticking point
when trying to replace an update statement.

Currently, I use this kind of statement a lot in my ASP:

sql = "update"
sql = sql & " field1 = value1"
sql = sql & ",field2 = value2"
If Len(value3) Then sql = sql & ",field3 = value3"
sql = sql & " where ...., etc, etc

The important part here is checking if "value3" has a value before
inserting it into my SQL statement. So how can I put this update
statement into a stored procedure, pass in values for value1, value2,
and value3, and leave off the value3 part of the update if value3
comes in as null.

Thanks all.

Jul 20 '05 #3
[posted and mailed, please reply in news]

Robert (rk****@kelsointeractive.com) writes:
Currently, I use this kind of statement a lot in my ASP:

sql = "update"
sql = sql & " field1 = value1"
sql = sql & ",field2 = value2"
If Len(value3) Then sql = sql & ",field3 = value3"
sql = sql & " where ...., etc, etc

The important part here is checking if "value3" has a value before
inserting it into my SQL statement. So how can I put this update
statement into a stored procedure, pass in values for value1, value2,
and value3, and leave off the value3 part of the update if value3
comes in as null.


UPDATE tbl
SET field1 = @value1,
field2 = @value2,
field3 = CASE len(@value3) > 0 AND @value IS NOT NULL
THEN @value3
ELSE field3
END
WHERE ...

No reason to even think about dynamic SQL.
--
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 #4

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

Similar topics

4
by: Toonman | last post by:
I'm trying to use a couple of variables in a stored procedure. Things work fine when I hard code the data into the variables and also work fine when I use the variable in the WHERE clause and hard...
4
by: marc | last post by:
I've been developing a stored procedure that uses a user defined function in the query portion of the procedure. However, since the end product needs to allow for dynamic table names, the UDF will...
0
by: billmiami2 | last post by:
Perhaps many of you MS Access fanatics already know this, but it seems that stored procedures and views are possible in Jet. I thought I would leave this message just in case it would help anyone....
3
by: Jack Black | last post by:
Help!! I'm trying to call a custom stored procedure from a VB.Net code-behind page in an ASP.Net application, and I keep getting an error with no real helpful info... Basically, I'm accepting a...
9
by: Frawls | last post by:
Hi I Am am having problems with a stored Procedure that i wrote. Basically whats happening is that the Stored procedure Runs fine when i EXECUTE it in SQL Query analyzer. But when i debug...
7
by: eholz1 | last post by:
Hello PHP group, Could someone help me out? I have PHP 5.2, Apache 2.0, and MySQL 5.0 running on Linux (Redhat Fedora Core 6). All that works fine. I would like to be able to "call" a stored...
5
by: Dennis | last post by:
Hi I'm trying to alter my stored procedure to take a parameter for the Database Name, but as usual the syntax is killing me. Thanks for any help Dennis ...
2
by: kxyz | last post by:
Hello everyone, I need help with a stored procedure or two. My stored procedures are supposed to check if a certain record exists. If it does exist, then I select everything from that row, as...
6
Soniad
by: Soniad | last post by:
Hello, I am excecuting a stored procedure in my ASP page , it has one out parameter (@confirm) . after executing the procedure i want to retreive this out parameter and assign it to variable...
2
by: priyamtheone | last post by:
I'm trying to create a stored procedure in MSSQL Server 2005 that'll perform the following jobs: 1) Create a login. 2) Create an user in TestDB database for the login created in step 1. 3) Assign...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.