简体   繁体   中英

ISNULL in SQL subquery not returning anything

Below is part of a SQL triggered that uses a subquery to return a value in the column TableString. The subquery will return the first two value and the "marker" for Fname, however nothing after that (not even the semicolon). The TableString column is an nvarchar that is set to 255 characters and captures all similar data during an insert or an update.

INSERT INTO TransactionLog (TransactionDate, Operator, TableName, Action
                           , TableString, UserId)
SELECT LastChangeDate
     , 'Op'
     , @tableName
     , @action
     , CAST('sNum:' + CAST(sNumber as nvarchar(10)) + ' entType:' + EntityType 
            + ' Fname:' + ISNULL(FirstName, 'NULL') 
            + ' Lname:' + ISNULL(LastName, 'NULL') 
            + ' suff:' + ISNULL(NameSuffix, 'NULL') 
            + ' corpName:' + ISNULL(CorporateName, 'NULL' ) 
            + ' ctrlId:' + ISNULL(CAST(ControlId as nvarchar(3)), 'NULL') 
            AS nvarchar(30)) as TableString
     , LastChangeOperator
FROM deleted

Returned values in TableString:

sNum:1000024 entType:S Fname

This has nothing to do with ISNULL but rather, is the result of a truncation of data.

Pay attention to:

.. AS nvarchar(30)

That is, much more data is being omitted. (The output does not even contain "Fname:" in this case.)

As a side note, his return character count is 28 because Nvarchar(30) means, store 30 bytes and Unicode (N) is two bytes a letter, even if you are storing non-Unicode. The number in the varchar declare is not the number of characters, its the number of bytes to use. It really gets people when they use Nvarchar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM