Solved

Change inserted value on trigger

Posted on 2014-10-24
6
95 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 32

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 32

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

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

14 Experts available now in Live!

Get 1:1 Help Now