2011-11-09 51 views
0

我無法找到一個答案....T-SQL的附加數字可變

我有三個變量,需要在While循環

例它們之間進行切換:

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 

如何將數字追加到變量@text? 我正在使用MS SQL 2008

+1

你想追加數變量名???爲什麼你需要這個,或許解決方案更直接...... – sll

回答

2

你想添加數字到變量名嗎?這不可能。爲什麼你需要的,也許解決方案更簡單....

試試以下,或許ID確實你在尋找:

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 

輸出:

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

要打印編號爲:

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

產量:

1 State of orders for Morfeus 
2 State of orders for Fenix 
3 State of orders for Morfeus 
+0

非常感謝。它看起來是我正在尋找的! – Robert

+0

@羅伯特:希望它有幫助 – sll

+0

基本上想法os SQL是關係數據結構,所以你應該使用表,記錄集,而不是一個棘手的動態東西 – sll

0

的問題不是很清楚你的最終目標是,但如果我理解你正確,則這樣的事情應該工作(注意,你需要首先創建解析功能):

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 
+0

這也可以用於我的例子克里斯,但就像你寫的我並不完美清楚我的最終遊戲是什麼,因此這不適用於我的真實劇本... 無論如何謝謝! – Robert

0

您可以使用動態SQL來完成,但您需要重新初始化所有變量。下面將做到這一點,但它一個colossally壞主意,你應該使用SLL的答案,而不是

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