iT邦幫忙

0

[已解決]如何印出無序號的table變數其內容資料

  • 分享至 

  • xImage

目前尚未找到其他簡化方法。

如下為原本的問題內容

//-------------------------------------------------
如標題,假設有一個table變數@tempTable1,裡面有幾筆資料,若我要將資料逐筆印出,會先將@tempTable1的資料insert到@tempTable2同時賦予序號,再跑迴圈逐筆印出資料,實作方式如下,想詢問是否有更精簡的方式呢?(例如不用給予序號就能逐筆印出等)

[定義]

    declare @varField1 nvarchar(4000)
            , @varField2 nvarchar(4000)
            , @varField3 nvarchar(4000)
            , @varCount int = 0
            , @varSum int = 0
            
    declare @tempTable1 table (
		Field1 nvarchar(4000)
		, Field2 nvarchar(4000)
		, Field3 nvarchar(4000)
	)
    
	declare @tempTable2 table (
		RowId int identity(1,1)
		, Field1 nvarchar(4000)
		, Field2 nvarchar(4000)
		, Field3 nvarchar(4000)
	)
    
    --建立測試資料
	insert @tempTable1(Field1, Field2, Field3)
	values 
        ('value1-1','value1-2','value1-3')
        , ('value2-1','value2-2','value2-3')
        , ('value3-1','value3-2','value3-3')

[實作印出資料]

	delete @tempTable2

	insert @tempTable2
	select * from @tempTable1
    
    select @varSum=count(*) from @tempTable2
    
    while(@varCount < @varSum)
    begin
        set @varCount = @varCount + 1
    
        select
            @varField1 = Field1
            , @varField2 = Field2
            , @varField3 = Field3
        from @tempTable2
        where RowId = @varCount
        
        print @varField1 + ' / ' + @varField2 + ' / ' + @varField3
        
    end
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答