replace values in update statement

Posted on 2003-02-20
Medium Priority
Last Modified: 2012-08-13
I have an update statement that looks like this:

update facility set country_code = ? where facility_id = 1

this questionmark is evaluated by Java based on the user entered values.  Since Country_code is a string, if the user does not enter any values, java tries to put in  '' (2 single quotes)....however country_code is a foreign key so the statement will blow up...

What I need is a way to evaluate what is being put in place of the question mark....if it is anything other than '', then I want to leave it alone, if it is '', then I want to insert a NULL.

Does that makes sense?
Question by:jl1884
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 7990514
if country_code is foreign key - you have to update with values that already exist in other table ( the one you made foreign key with - let's call it FOREGN_TABLE).
If you want to allow anyway NULL values in your facility table - you can define in table FOREGN_TABLE value NULL - and it will be default value with no input.

I hope I understood you and answered the question.
LVL 18

Expert Comment

ID: 7990844
update facility
set country_code = case when @s = '' then null else @s end
where facility_id = 1

Expert Comment

ID: 7992481
/*If country doesnt exist, do not update*/
SET Country_Code = C.Country_Code
FROM Facility F
JOIN Country C On C.Country_Code = ?
WHERE F.Facility_Id = 1

/*If no rows affected return an error*/
IF @@RowCount = 0
  RaisError ('mr Java developer, please add som input validation!', 16, 1)

Accepted Solution

KatanaCS earned 800 total points
ID: 8012551
If the update statement is being assigned by the Java code, then it'll need to be evaluated in Java (as suggested by johan_brohn's RaisError).  

If the update statement is being assigned in SQL (i.e. through a stored procedure call), then you can test for validation in the procedure.  If you validate all of the parameters in SQL, you'll most likely have to use dynamic SQL to perform the actual update.
   If IsNull(@Country, '') <> '' -- Include in update statement
         Select @SQL = @SQL + ', country = ''' + @Country + ''''


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question