0

I couldn't find an answer to this....

I have three variables and need to switch between them in While Loop

Example:

DECLARE  
@tableHTML  NVARCHAR(MAX),  
@email nvarchar(100),  
@text1 nvarchar(100),
@text2 nvarchar(100),
@text3 nvarchar(100),
@number_suffix nvarchar(1)
SET @text1 = 'State of orders for Morfeus'
SET @text2 = 'State of orders for Fenix'
SET @text3 = 'State of orders for Perseus'
SET @number_suffix = 1
WHILE (@number_suffix < 4)
BEGIN
    print @text(@number_suffix) /*and here is the problem */
    SET @number_suffix = (@number_suffix + 1)
END

How do I append the number to variable @text please? I am using MS SQL 2008

1
  • 1
    Do you want to append number to variable name??? Why you need that, perhaps solution is more straightforward.... Commented Nov 9, 2011 at 12:07

3 Answers 3

2

Do you want to append number to variable name? This is not possible. Why you need that, perhaps solution is more straightforward....

Try out following, perhaps id does what are you looking for:

DECLARE @cities TABLE(id int IDENTITY(1,1), cityName varchar(100))
INSERT INTO @cities(cityName) VALUES ('Morfeus'), ('Fenix'), ('Morfeus')    
SELECT 'State of orders for ' + cityName
FROM @cities

Output:

State of orders for Morfeus
State of orders for Fenix
State of orders for Morfeus

To print number as well:

SELECT '#' + CAST(id AS varchar(2)) + ' State of orders for ' + cityName
FROM @cities

Output:

1 State of orders for Morfeus 
2 State of orders for Fenix 
3 State of orders for Morfeus
Sign up to request clarification or add additional context in comments.

1 Comment

Basically idea os SQL is relational data structures so you should operate with tables, record sets, not a tricky dynamic stuff
0

The question is not quite clear what your end game is but if I understand you correctly then something like this should work (note, you need to create the parse function first):

    CREATE FUNCTION [dbo].[fnParseString]
(
    @Section SMALLINT,
    @Delimiter CHAR,
    @Text VARCHAR(MAX)
)
RETURNS VARCHAR(8000)
AS

BEGIN
DECLARE @startindex NUMERIC(18,0),
     @length NUMERIC(18,0),
     @FieldPosition INT

 SET @FieldPosition = ABS(@Section) - 1
 SET @startindex = 0


 WHILE @FieldPosition != 0
 BEGIN
    SET @FieldPosition = @FieldPosition - 1
     SET @startindex = CHARINDEX(@Delimiter, @Text, @startindex + 1) 
 END     


 SET @Text = SUBSTRING(@Text, @startindex + 1, LEN(@Text) - @startindex)
 SET @Text = SUBSTRING(@Text, 0, CHARINDEX(@Delimiter, @Text))

 RETURN @Text
END
GO

DECLARE  
@tableHTML  NVARCHAR(MAX),  
@email nvarchar(100),  
@text nvarchar(100),
@number_suffix nvarchar(1)

SET @text = 'State of orders for Morfeus|State of orders for Fenix|State of orders for Perseus|'

SET @number_suffix = 1
WHILE (@number_suffix < 4)
BEGIN
    PRINT dbo.fnParseString(@number_suffix, '|', @text)

    SET @number_suffix = (@number_suffix + 1)
END

1 Comment

This may also work for my example Chris, but like you wrote I wasn't perfectly clear what my end game is and thus this is not going to work for my real script... Thank you anyway!
0

You can do it with Dynamic SQL but you'd need to reinitialize all you variables. The following will do it, but its a colossally bad idea, and you should use sll's answer instead

DECLARE  
@tableHTML  NVARCHAR(MAX),  
@email nvarchar(100),  

@number_suffix nvarchar(1),
@SQL nvarchar(max)

SET @number_suffix = 1
WHILE (@number_suffix < 4)
BEGIN
    SET @SQL = N'
    DECLARE @text1 nvarchar(100),
            @text2 nvarchar(100),
            @text3 nvarchar(100)

    SET @text1 = ' + '''' + 'State of orders for Morfeus' + ''''  +
   'SET @text2 = ' + '''' + 'State of orders for Fenix' + ''''  + 
   'SET @text3 = ' + '''' + 'State of orders for Perseus' + ''''

    +           

    'PRINT @text' + @number_suffix
    EXEC sp_executeSQL  @SQL 
    SET @number_suffix = (@number_suffix + 1)
END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.