Solved

Change inserted value on trigger

Posted on 2014-10-24
6
97 Views
Last Modified: 2014-11-11
Hi guys,

Here is a section of my trigger...

INSERT INTO XYZ (SupplierCode, ActiveStatus, DateUpdated, UpdatedBy)
    SELECT
         i.Supplier, i.UserField2,CURRENT_TIMESTAMP, SYSTEM_USER
    FROM
         inserted as i LEFT OUTER JOIN [TriggerTablesAscent].[dbo].[SupplierStatus] tta ON i.Supplier = tta.SupplierCode

Open in new window


My ActiveStatus field is stored as bit. I have just noticed the UserField2 is a single char of Y or N. I need to convert Y into True and N into False before the insert.

How is it best to achieve this?

Thanks,
Dean.
0
Comment
Question by:deanlee17
  • 2
  • 2
  • 2
6 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40401277
Use a CASE expression like CASE I.UserField2 WHEN = 'Y' THEN 1 WHEN 'N' THEN 0 END or IIF() when your on SQL Server 2012+.
0
 

Author Comment

by:deanlee17
ID: 40401717
Would the case statement sit above INSERT INTO?
0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 total points
ID: 40401752
No, it would be inline.

If you want True and False, then...

INSERT INTO XYZ (SupplierCode, ActiveStatus, DateUpdated, UpdatedBy)
    SELECT
         i.Supplier,CASE I.UserField2 WHEN 'Y' THEN 'True' ELSE 'False' END,CURRENT_TIMESTAMP, SYSTEM_USER
    FROM
         inserted as i LEFT OUTER JOIN [TriggerTablesAscent].[dbo].[SupplierStatus] tta ON i.Supplier = tta.SupplierCode

Open in new window


If you want 1 or 0 then...

INSERT INTO XYZ (SupplierCode, ActiveStatus, DateUpdated, UpdatedBy)
    SELECT
         i.Supplier,CASE I.UserField2 WHEN 'Y' THEN 1 ELSE 0 END,CURRENT_TIMESTAMP, SYSTEM_USER
    FROM
         inserted as i LEFT OUTER JOIN [TriggerTablesAscent].[dbo].[SupplierStatus] tta ON i.Supplier = tta.SupplierCode

Open in new window


If you're going to get technical about it, then true should be -1 and false should be 0 (or not true, or everything else). What specific data types do you want? An integer or a string?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 33

Expert Comment

by:ste5an
ID: 40401755
Nope. Read the link and my post again. CASE is an expression not a statement.
0
 

Author Comment

by:deanlee17
ID: 40401779
Thanks for the feedback. Lee that worked for the case above, but should it also work for an update? My Insert works but my update fails:

  SET
        ActiveStatus = CASE i.UserField2 WHEN 'Y' THEN 'True' ELSE 'False' END,CURRENT_TIMESTAMP, SYSTEM_USER, DateUpdated = CURRENT_TIMESTAMP, UpdatedBy = SYSTEM_USER

Open in new window


The True or False is going into a bit field.

Thanks,
Dean
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40401789
Then use 1 and 0 as true and false.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

929 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

12 Experts available now in Live!

Get 1:1 Help Now