A very useful function - COALESCE

Many people know the ISNULL function, but few people know the Coalesce function. People will accidentally use the Coalesce function and find that it is more powerful than ISNULL. In fact, so far, this function is indeed very useful. This article mainly explains some of the basic uses. :


First look at Books Online's brief definition: Returns the first non-null expression in its argument

Syntax : COALESCE ( expression [ ,...n ] )

If all arguments are NULL, COALESCE returns NULL. At least one Null value should be of type NULL. Although ISNULL is equivalent to COALESCE, their behavior is different. Expressions containing ISNULL with non-null parameters are treated as NOT NULL, and expressions containing COALESCE with non-null parameters are treated as NULL. In SQL Server, to create an index on an expression that contains COALESCE with a non-null parameter, the computed column can be persisted using the PERSISTED column property, as shown in the following statement:

  
CREATE TABLE #CheckSumTest   
         (  
             ID int identity ,  
             Num int DEFAULT ( RAND() * 100 ) ,  
             RowCheckSum AS COALESCE( CHECKSUM( id , num ) , 0 ) PERSISTED PRIMARY KEY  
         );  


Let's take a look at a few more useful examples:


First, look at the usage of this function from MSDN, the coalesce function (hereinafter referred to as the function), returns a non-null value in a parameter. Such as:
SELECT  COALESCE(NULL, NULL, GETDATE())

Since both parameters are null, the value of the getdate() function is returned, which is the current time. That is, the first non-null value is returned. Since this function returns the first non-null value, there must be at least one non-null value in the parameter. If the following query is used, an error will be reported:
  
SELECT  COALESCE(NULL, NULL, NULL)  



Then look at applying the function to Pivot. The following statement is run on the AdventureWorks database:

   
SELECT  Name  
     FROM    HumanResources.Department  
     WHERE   ( GroupName= 'Executive Generaland Administration' )
 
You will get the following result:


If you want to reverse the result, you can use the following statement:

    DECLARE @DepartmentName VARCHAR(1000)  
        
     SELECT  @DepartmentName = COALESCE(@DepartmentName, '') + Name + ';'  
     FROM    HumanResources.Department  
     WHERE   ( GroupName= 'Executive Generaland Administration' )  
        
     SELECT  @DepartmentName AS DepartmentNames  


Use a function to execute multiple SQL commands:

Once you know that the function can do a twist, you should also know that it can run multiple SQL commands. And use a semicolon to distinguish separate operations. The following statement is the value of the column named Name in the Person schema:
   
DECLARE @SQL VARCHAR(MAX)   
       
     CREATE TABLE #TMP   
        (Clmn VARCHAR(500),    
         Val VARCHAR(50))   
       
     SELECT @SQL=COALESCE(@SQL,'')+CAST('INSERT INTO #TMP Select ''' + TABLE_SCHEMA + '.' + TABLE_NAME + '.'   
     + COLUMN_NAME + ''' AS Clmn, Name FROM ' + TABLE_SCHEMA + '.[' + TABLE_NAME +   
     '];' AS VARCHAR(MAX))   
     FROM INFORMATION_SCHEMA.COLUMNS    
     JOIN sysobjects B ON INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = B.NAME   
     WHERE COLUMN_NAME = 'Name'    
        AND xtype = 'U'    
        AND TABLE_SCHEMA = 'Person'   
       
     PRINT @SQL   
     EXEC(@SQL)   
       
     SELECT * FROM #TMP   
     DROP TABLE #TMP  



There is another very important function: . This is useful when you try to restore a library and find that you can't have exclusive access. Let's open multiple windows to simulate multiple connections. Then execute the following script:



  
DECLARE @SQL VARCHAR(8000)  
        
     SELECT  @SQL = COALESCE(@SQL, '') + 'Kill ' + CAST(spid AS VARCHAR(10)) + '; '  
     FROM    sys.sysprocesses  
     WHERE   DBID = DB_ID('AdventureWorks')  
        
     PRINT @SQL --EXEC(@SQL) Replace the print statement with exec to execute  

Here's the result:


you can then copy the result and kill all sessions at once.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326486146&siteId=291194637
Recommended