Reference:SQL Server null+ strings, isnull and assigning query results to one character variable to form another query statement
1. The string that starts the definition is null. For example: Declare @sql
Requirement: A string that loops through a cursor to get a phone number is stitched together. But the stitching was not successful.
Declare @dhhm as varchar( -)--Defines the variable phone number if it is null if it is not assigned a value, and then the string is appended with an error .Declare @rcount as INT--Defines the number of variables!!!!!!!!!!!!!! Set @rcount=0--You must assign a value of 0 to a variable, or it is null after it cannot be ++!!!!!!!! DECLARE @mobilnumber nvarchar( -) DECLAREMyCursor1CURSOR --Defining Cursors for SelectMobilephone fromSys_userwhereFullNameinch(Select * fromF_SPLITSTR ('system, China',',')) OPENMyCursor1--Open Cursor FETCH NEXT fromMyCursor1 into @mobilnumber --print (' Cursor start lookup data ') Print('Status of foreach'+Convert(varchar( $),@ @fetch_status)) while(@ @fetch_status=0) BEGIN Set @dhhm = IsNull(@dhhm,"')+@mobilnumber+','--the IsNull method is used here to determine the value of the variable @dhhm If NULL is assigned to an empty string, so the string concatenation error is not caused.
--If you do not want to use IsNull, in @dhhm, after defining the variable, you want to assign a value to the @dhhm to NULL, such as: set @dhhm = "
Set @rcount = @rcount+1 FETCH NEXT fromMyCursor1 into @mobilnumber END --Close Cursors CLOSEMyCursor1--Freeing Resources deallocateMyCursor1Print @dhhm Print('Number of SMS'+Convert(varchar( -),@rcount))
Interception of strings in sql: http://www.cnblogs.com/yeminglong/archive/2011/10/15/2212967.html
Declare @id as varchar (+) Set @id = ' 123456, ' Select SUBSTRING (@id,0,Len(@id))
Returns the result 123456, the last comma minus the
SQL Server variable string concatenation