UPDATE

2011-09-22 3 views
2

내부 쿼리를 조건문을 추가 :UPDATE

UPDATE empPac 
    SET quantityLimit = allocation, 
     allocationStart = '"&allocationStart&"', 
     nextUpdate = DATEADD(mm, allocationMonths, "&allocationStart&"), 
     lastUpdate = GETDATE(), 
     quantityIssued = 0, 
     quantityShipped = 0 
    WHERE allocation IS NOT NULL AND 
      allocationMonths <> 0 AND 
      (nextUpdate <= DATEADD(mm, "&checkCondition&", GETDATE()) OR 
      nextUpdate IS NULL) AND 
      empIdent in (select empIdent 
         from employee 
         where custIdent='"&custIdent&"') 

내가 오히려 WHERE allocation IS NOT NULL을하는 것보다, 나는 그것이 조건문을 갖고 싶어되도록 SET quantityLimit = allocation에 조건문을 추가한다 싶지 같은 SET quantityLimit = ((allocation IS NULL) ? 0 : allocation)

+0

MSSQL. 내가 작성한 조건문은 PHP 약식이지만, 내가 한 일의 예를 보여줄 방법을 알지 못했습니다. – scarhand

답변

2

당신은 ISNULL() 사용할 수 있습니다 당신이 원하는 경우

SET quantityLimit = ISNULL(allocation, 0) 

동등한 기능을 다른 데이터베이스입니다 오라클 NVL()IFNULL() MySQL 용 및 SQLite에 대한


당신이 정말 그래도 사용해야하는 것은 COALESCE()입니다 코드의 이식성을 높이십시오. COALESCESQL-92 standard의 일부이며 RDBMS를 통해 널리 지원됩니다.

+0

그리고 적어도 Oracle에서는 nvl이 pl/sql 함수이고 병합이 기본적으로 구현되어 있기 때문에 coalesce를 사용하는 것이 좋습니다. http://stackoverflow.com/questions/950084/oracle-differences-between-nvl-and-coalesce –

+0

COALESCE가 ANSI 표준이 아닙니까? –

+0

@ConradFrix입니다. 나는 내 대답을 고쳤다. – NullUserException

1

어떤 데이터베이스를 사용하십니까? 예를 들어, 오라클 SQL에서 당신이 쓸 수 case when allocation is null then 0 else allocation end 또는 nvl (allocation, 0) 또는

coalesce (allocation, 0) 그리고 case syntax in MSSQL 오라클 동일하다.

0

이것은 TSQL (MSSQL) 방법 :

SET quantityLimit = isnull(allocation,0) 

대안 ...

SET quantityLimit = CASE WHEN allocation is null THEN 0 ELSE allocation END 
--This one might be handy if you wanted to check for more than just null values. Such as: 
----...CASE WHEN allocation is null THEN 0 WHEN some_other_value THEN 1 WHEN ... THEN ... ELSE allocation END 

SET quantityLimit = coalesce(allocation,0) 
--This one gives you the first non-null value it finds, given a list of places to look. Such as: 
----...coalesce(allocation,some_other_field,some_nuther_field,...,0)