Solved

sql statement issue with inserting with a Case statemetn

Posted on 2013-12-11
10
440 Views
Last Modified: 2013-12-11
Hi,
I'm having an issue with an insert statement.
I want to copy some records from one table to the other

when field "Action_name" is equal to credit I want to insert the negative of the amount. So if it says credit and the amount is 2, i want to insert -2.

I am using the following statement to do that, somehow it is giving me the message, incorrect syntax near '('

the code is as follows

insert into  [dbo].[tbl_visa_imports](
      
      [cc] ,
      [exp] ,
      [media_name] ,
      [trans_date] ,
      [transid] ,
      [ref_transid] ,
      (case
      when action_name = 'credit' then abs(([amount])*-1)
      else [amount] end) ,
      [action_name] ,
      [status_name] ,
      [name] ,
      [ticket],
      
      [billingid],
      [custid],
      [fail_name],

      [operator],
      [entry_mode] ,
      [USERID] ,
      [CASHDRAWERID] ,
      [DEPTABBR] ,
      [DEPTEXTNAME] ,


      [LOCABBR] ,
      [LOCID] ,
      [LOCNAME] ,
      [PATDAT] ,
      [PATID] ,
 
      [SERVICEARBAABBR] ,
      [SERVICEAREAID] ,
      [SERVICEAREANAME] ,

      [GUARACCTID] ,
      [HOSPACCTID] ,
      [WORKSTATIONID],
      [WORKSTATIONNAME],
      assigned )
      
      select       [cc] ,
      [exp] ,
      [media_name] ,
      [trans_date] ,
      [transid] ,
      [ref_transid] ,
      [amount] ,
      [action_name] ,
      [status_name] ,
      [name] ,
      [ticket],
      
      [billingid],
      [custid],
      [fail_name],

      [operator],
      [entry_mode] ,
      [USERID] ,
      [CASHDRAWERID] ,
      [DEPTABBR] ,
      [DEPTEXTNAME] ,
      
      [LOCABBR] ,
      [LOCID] ,
      [LOCNAME] ,
      [PATDAT] ,
      [PATID] ,

      [SERVICEARBAABBR] ,
      [SERVICEAREAID] ,
      [SERVICEAREANAME] ,

      [GUARACCTID] ,
      [HOSPACCTID] ,
      [WORKSTATIONID],
      [WORKSTATIONNAME],
      0
      from dbo.tbl_imports

thanks in advance
Vinnie

Addition: my main problem is in the Case Statement. If I was to take out the case statement, it works fine

   (case 
      when action_name = 'credit' then abs(([amount])*-1)
      else [amount] end) ,

Open in new window

0
Comment
Question by:damixa
  • 3
  • 3
  • 2
  • +2
10 Comments
 

Author Comment

by:damixa
Comment Utility
So to clarify, my main problem is in the case statement

   (case 
      when action_name = 'credit' then abs(([amount])*-1)
      else [amount] end) ,

Open in new window

0
 
LVL 1

Expert Comment

by:jleenotlo
Comment Utility
Take out the abs() because that would give you a positive number instead of a negative number.

(case
      when action_name = 'credit' then ([amount]*-1)
      else [amount] end)
0
 

Author Comment

by:damixa
Comment Utility
Right but I want a positive number. That way the outcome is always negative. So by adding the abb and multiplying it by neg 1 it assures me that the outcome is always the negative number.

I did try that though, and it still gave me the same error.
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
>I am using the following statement to do that, somehow it is giving me the message, incorrect syntax near '('

The CASE statement is currently in the INSERT clause, which is causing the syntax error.  It belongs in the SELECT clause that lines up with a column where that CASE block is to insert values.
0
 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 500 total points
Comment Utility
The Case statement is in the wrong part. The insert statement must have the field name you are inserting to and the case statement should be in the select statement.


Kelvin
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:damixa
Comment Utility
Kelvin, I'm so stupid. Thanks
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
>when field "Action_name" is equal to credit I want to insert the negative of the amount. So if it says credit and the amount is 2, i want to insert -2.

Replace column_name with the name of the column that you're trying to insert the expression into, guessing it's amount.
INSERT INTO tbl_visa_imports (blah, blah, column_name)
SELECT blah, blah, 
 CASE WHEN Action_Name = credit then amount * -1 ELSE amount END
FROM Imports

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
damixa - Let me know if my first comment did not meet your needs.
0
 
LVL 12

Expert Comment

by:duttcom
Comment Utility
You can put the case statement in the select statement rather than the insert.

Your insert statement would only have [amount] rather than the case statement and the case statement would be in the select statement like so-

[ref_transid] ,
CASE WHEN action_name = 'credit'
then abs(([amount])*-1)
else [amount]
END,
 [action_name]

The whole case statement is the value being selected to insert.
0
 
LVL 22

Expert Comment

by:Kelvin Sparks
Comment Utility
No problems - only recognised it as I'd spent a few hours a short while ago finding the exact same issue in my own code.

Glad I could help.


Kelvin
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now