Douglass MacLean
asked on
How can I force new lines in an nvarchar field whose content I am constructing in a stored procedure?
I am building an application that will dynamically construct stored procedures from relatively non-technical users' inputs. One of several aspects of this is to build the parameter list for the Create Procedure code. The problem I have encountered is that while the code below appears to function correctly, when the result is selected, the new line pair of characters are apparently replaced automatically with spaces.
Any suggestions gratefully received!
/* Problem trying to force new lines in an output nvarchar field */
Declare @T table(DisplaySeq int, ParamName nvarchar(128), ParamDataType nvarchar(50))
Declare @NumParams int, @ParamList nvarchar(1000), @ThisParam nvarchar(100), @NewLine nvarchar(10), @ThisSeq int
-- Test data to demonstrate the problem
Insert into @T (DisplaySeq, ParamName, ParamDataType) Values (1, '@WorkOrder#','nvarchar(10 )')
Insert into @T (DisplaySeq, ParamName, ParamDataType) Values (2, '@FromDateSampled','date')
Insert into @T (DisplaySeq, ParamName, ParamDataType) Values (3, '@ToDateSampled','date')
Insert into @T (DisplaySeq, ParamName, ParamDataType) Values (4, 'CustomerState','nvarchar( 2)')
-- Get ready to build the parameter list
Set @NewLine = CHAR(13) + CHAR(10)
Set @ParamList = ''
Set @NumParams = (Select COUNT(*) from @T)
Set @ThisSeq = 1
-- Build the parameter list
While @ThisSeq <= @NumParams
begin
Set @ThisParam = (Select ParamName + ' ' + ParamDataType From @T Where (DisplaySeq = @ThisSeq))
If @ThisSeq = 1 begin
Set @ParamList = @ParamList + @ThisParam
end
else begin
Set @ParamList = @ParamList + @NewLine + ', ' + @ThisParam
end
Set @ThisSeq = @ThisSeq + 1
end
-- Show the result. Looks like CHAR(13) and CHAR(10) have been replaced with spaces
Select @ParamList as ResultingList
ParamList-result.PNG
Any suggestions gratefully received!
/* Problem trying to force new lines in an output nvarchar field */
Declare @T table(DisplaySeq int, ParamName nvarchar(128), ParamDataType nvarchar(50))
Declare @NumParams int, @ParamList nvarchar(1000), @ThisParam nvarchar(100), @NewLine nvarchar(10), @ThisSeq int
-- Test data to demonstrate the problem
Insert into @T (DisplaySeq, ParamName, ParamDataType) Values (1, '@WorkOrder#','nvarchar(10
Insert into @T (DisplaySeq, ParamName, ParamDataType) Values (2, '@FromDateSampled','date')
Insert into @T (DisplaySeq, ParamName, ParamDataType) Values (3, '@ToDateSampled','date')
Insert into @T (DisplaySeq, ParamName, ParamDataType) Values (4, 'CustomerState','nvarchar(
-- Get ready to build the parameter list
Set @NewLine = CHAR(13) + CHAR(10)
Set @ParamList = ''
Set @NumParams = (Select COUNT(*) from @T)
Set @ThisSeq = 1
-- Build the parameter list
While @ThisSeq <= @NumParams
begin
Set @ThisParam = (Select ParamName + ' ' + ParamDataType From @T Where (DisplaySeq = @ThisSeq))
If @ThisSeq = 1 begin
Set @ParamList = @ParamList + @ThisParam
end
else begin
Set @ParamList = @ParamList + @NewLine + ', ' + @ThisParam
end
Set @ThisSeq = @ThisSeq + 1
end
-- Show the result. Looks like CHAR(13) and CHAR(10) have been replaced with spaces
Select @ParamList as ResultingList
ParamList-result.PNG
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER